Skip to content

Commit a49fcdb

Browse files
fix: locking of tokens (#294)
1 parent 22087ef commit a49fcdb

File tree

1 file changed

+10
-12
lines changed
  • substrate-node/pallets/pallet-smart-contract/src

1 file changed

+10
-12
lines changed

substrate-node/pallets/pallet-smart-contract/src/lib.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,12 @@ impl<T: Config> Module<T> {
657657
// Calculate amount of seconds elapsed based on the contract lock struct
658658
let mut contract_lock = ContractLock::<T>::get(contract.contract_id);
659659

660+
let now = <timestamp::Module<T>>::get().saturated_into::<u64>() / 1000;
661+
660662
// this will set the seconds elapsed to the default billing cycle duration in seconds
661663
// if there is no contract lock object yet. A contract lock object will be created later in this function
662664
// https://github.com/threefoldtech/tfchain/issues/261
663665
if contract_lock.lock_updated != 0 {
664-
let now = <timestamp::Module<T>>::get().saturated_into::<u64>() / 1000;
665666
seconds_elapsed = now.checked_sub(contract_lock.lock_updated).unwrap_or(0);
666667
}
667668

@@ -679,26 +680,24 @@ impl<T: Config> Module<T> {
679680
amount_due = usable_balance;
680681
}
681682

682-
// Refetch usable balance
683-
let usable_balance = Self::get_usable_balance(&twin.account_id);
684-
// Get twin free balance
685-
let free_balance = <T as Config>::Currency::free_balance(&twin.account_id);
686-
// Locked balance = free balance - usable balance
687-
let locked_balance = free_balance - usable_balance;
683+
684+
let new_amount_locked = contract_lock.amount_locked + amount_due;
688685

689686
// Update lock for contract and ContractLock in storage
690687
<T as Config>::Currency::extend_lock(
691688
contract.contract_id.to_be_bytes(),
692689
&twin.account_id,
693-
locked_balance + amount_due.into(),
690+
new_amount_locked.into(),
694691
WithdrawReasons::RESERVE,
695692
);
696693
// increment cycles billed
697-
contract_lock.lock_updated = <timestamp::Module<T>>::get().saturated_into::<u64>() / 1000;
694+
contract_lock.lock_updated = now;
698695
contract_lock.cycles += 1;
699-
contract_lock.amount_locked = contract_lock.amount_locked + amount_due;
696+
contract_lock.amount_locked = new_amount_locked;
700697
ContractLock::<T>::insert(contract.contract_id, &contract_lock);
701698

699+
println!("amount locked: {:?}", contract_lock.amount_locked);
700+
702701
let is_canceled = matches!(contract.state, types::ContractState::Deleted(_));
703702
// When the cultivation rewards are ready to be distributed or we have to decomission the contract (due to out of funds) or it's canceled by the user
704703
// Unlock all reserved balance and distribute
@@ -720,8 +719,7 @@ impl<T: Config> Module<T> {
720719
Err(err) => debug::info!("error while distributing cultivation rewards {:?}", err),
721720
};
722721
// Reset values
723-
contract_lock.lock_updated =
724-
<timestamp::Module<T>>::get().saturated_into::<u64>() / 1000;
722+
contract_lock.lock_updated = now;
725723
contract_lock.amount_locked = BalanceOf::<T>::saturated_from(0 as u128);
726724
contract_lock.cycles = 0;
727725
ContractLock::<T>::insert(contract.contract_id, &contract_lock);

0 commit comments

Comments
 (0)