@@ -657,11 +657,12 @@ impl<T: Config> Module<T> {
657
657
// Calculate amount of seconds elapsed based on the contract lock struct
658
658
let mut contract_lock = ContractLock :: < T > :: get ( contract. contract_id ) ;
659
659
660
+ let now = <timestamp:: Module < T > >:: get ( ) . saturated_into :: < u64 > ( ) / 1000 ;
661
+
660
662
// this will set the seconds elapsed to the default billing cycle duration in seconds
661
663
// if there is no contract lock object yet. A contract lock object will be created later in this function
662
664
// https://github.com/threefoldtech/tfchain/issues/261
663
665
if contract_lock. lock_updated != 0 {
664
- let now = <timestamp:: Module < T > >:: get ( ) . saturated_into :: < u64 > ( ) / 1000 ;
665
666
seconds_elapsed = now. checked_sub ( contract_lock. lock_updated ) . unwrap_or ( 0 ) ;
666
667
}
667
668
@@ -679,26 +680,24 @@ impl<T: Config> Module<T> {
679
680
amount_due = usable_balance;
680
681
}
681
682
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;
688
685
689
686
// Update lock for contract and ContractLock in storage
690
687
<T as Config >:: Currency :: extend_lock (
691
688
contract. contract_id . to_be_bytes ( ) ,
692
689
& twin. account_id ,
693
- locked_balance + amount_due . into ( ) ,
690
+ new_amount_locked . into ( ) ,
694
691
WithdrawReasons :: RESERVE ,
695
692
) ;
696
693
// increment cycles billed
697
- contract_lock. lock_updated = <timestamp :: Module < T > > :: get ( ) . saturated_into :: < u64 > ( ) / 1000 ;
694
+ contract_lock. lock_updated = now ;
698
695
contract_lock. cycles += 1 ;
699
- contract_lock. amount_locked = contract_lock . amount_locked + amount_due ;
696
+ contract_lock. amount_locked = new_amount_locked ;
700
697
ContractLock :: < T > :: insert ( contract. contract_id , & contract_lock) ;
701
698
699
+ println ! ( "amount locked: {:?}" , contract_lock. amount_locked) ;
700
+
702
701
let is_canceled = matches ! ( contract. state, types:: ContractState :: Deleted ( _) ) ;
703
702
// 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
704
703
// Unlock all reserved balance and distribute
@@ -720,8 +719,7 @@ impl<T: Config> Module<T> {
720
719
Err ( err) => debug:: info!( "error while distributing cultivation rewards {:?}" , err) ,
721
720
} ;
722
721
// Reset values
723
- contract_lock. lock_updated =
724
- <timestamp:: Module < T > >:: get ( ) . saturated_into :: < u64 > ( ) / 1000 ;
722
+ contract_lock. lock_updated = now;
725
723
contract_lock. amount_locked = BalanceOf :: < T > :: saturated_from ( 0 as u128 ) ;
726
724
contract_lock. cycles = 0 ;
727
725
ContractLock :: < T > :: insert ( contract. contract_id , & contract_lock) ;
0 commit comments