From 72d2202d549fe250124f77451e7ab5f7479ddc16 Mon Sep 17 00:00:00 2001 From: Sameh Abouel-saad Date: Sun, 18 Aug 2024 14:03:48 +0300 Subject: [PATCH] Correct naming convention for ContractPaymentState API --- clients/tfchain-client-go/contract_events.go | 4 +- clients/tfchain-client-go/events.go | 2 +- .../pallet-smart-contract/src/benchmarking.rs | 2 +- .../pallet-smart-contract/src/billing.rs | 28 +++--- .../pallets/pallet-smart-contract/src/lib.rs | 2 +- .../pallet-smart-contract/src/types.rs | 88 +++++++++---------- 6 files changed, 63 insertions(+), 63 deletions(-) diff --git a/clients/tfchain-client-go/contract_events.go b/clients/tfchain-client-go/contract_events.go index 88812230d..7c10c8ac2 100644 --- a/clients/tfchain-client-go/contract_events.go +++ b/clients/tfchain-client-go/contract_events.go @@ -256,12 +256,12 @@ type ContractGracePeriodElapsed struct { Topics []types.Hash } -type ContractPaymentOverdrafted struct { +type ContractPaymentOverdrawn struct { Phase types.Phase ContractID types.U64 `json:"contract_id"` Timestamp types.U64 `json:"timestamp"` PartiallyBilledAmount types.U128 `json:"partially_billed_amount"` - OverdraftedAmount types.U128 `json:"overdrafted_amount"` + Overdraft types.U128 `json:"Overdraft"` Topics []types.Hash } diff --git a/clients/tfchain-client-go/events.go b/clients/tfchain-client-go/events.go index 2fac4c367..2538fa374 100644 --- a/clients/tfchain-client-go/events.go +++ b/clients/tfchain-client-go/events.go @@ -391,7 +391,7 @@ type EventRecords struct { SmartContractModule_NodeExtraFeeSet []NodeExtraFeeSet //nolint:stylecheck,golint SmartContractModule_RentWaived []RentWaived //nolint:stylecheck,golint SmartContractModule_ContractGracePeriodElapsed []ContractGracePeriodElapsed //nolint:stylecheck,golint - SmartContractModule_ContractPaymentOverdrafted []ContractPaymentOverdrafted //nolint:stylecheck,golint + SmartContractModule_ContractPaymentOverdrawn []ContractPaymentOverdrawn //nolint:stylecheck,golint SmartContractModule_RewardDistributed []RewardDistributed //nolint:stylecheck,golint // farm events diff --git a/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs b/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs index e40be91e0..5d8568665 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs @@ -309,7 +309,7 @@ benchmarks! { }: _(RawOrigin::Signed(farmer), contract_id) verify { let contract_payment_state = SmartContractModule::::contract_payment_state(contract_id); - assert_eq!(contract_payment_state.standard_reserved, cost); + assert_eq!(contract_payment_state.standard_reserve, cost); let contract_bill = types::ContractBill { contract_id, timestamp: SmartContractModule::::get_current_timestamp_in_secs(), diff --git a/substrate-node/pallets/pallet-smart-contract/src/billing.rs b/substrate-node/pallets/pallet-smart-contract/src/billing.rs index a03ae6aa1..092287364 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/billing.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/billing.rs @@ -273,9 +273,9 @@ impl Pallet { // Calculate the amount needed to be reserved from the user's balance // should be the total amount due for current cycle + any overdrafted amount from previous cycles let standard_amount_to_reserve = standard_amount_due - .defensive_saturating_add(contract_payment_state.standard_overdrafted); + .defensive_saturating_add(contract_payment_state.standard_overdraft); let additional_amount_to_reserve = additional_amount_due - .defensive_saturating_add(contract_payment_state.additional_overdrafted); + .defensive_saturating_add(contract_payment_state.additional_overdraft); let total_amount_to_reserve = standard_amount_to_reserve.defensive_saturating_add(additional_amount_to_reserve); @@ -315,7 +315,7 @@ impl Pallet { "Contract payment overdrafted for contract_id: {:?}, Contract state: {:?}, Total overdrafted amount: {:?}", contract.contract_id, contract.state, - contract_payment_state.get_overdrafted() + contract_payment_state.get_overdraft() ); } @@ -366,8 +366,8 @@ impl Pallet { contract_lock ); contract_payment_state.last_updated_seconds = contract_lock.lock_updated; - contract_payment_state.standard_overdrafted = contract_lock.amount_locked; - contract_payment_state.additional_overdrafted = contract_lock.extra_amount_locked; + contract_payment_state.standard_overdraft = contract_lock.amount_locked; + contract_payment_state.additional_overdraft = contract_lock.extra_amount_locked; contract_payment_state.cycles = contract_lock.cycles; let locks = pallet_balances::Pallet::::locks(&account_id); @@ -454,7 +454,7 @@ impl Pallet { e }, )?; - contract_payment_state.settle_overdrafted(); + contract_payment_state.settle_overdraft(); contract_payment_state.reserve_standard_amount(standard_amount_due); contract_payment_state.reserve_additional_amount(additional_amount_due); let contract_bill = types::ContractBill { @@ -468,7 +468,7 @@ impl Pallet { } // Increasing the overdraft in the user's account - // Emits ContractPaymentOverdrafted event + // Emits ContractPaymentOverdrawn event fn overdraft_funds( contract_payment_state: &mut types::ContractPaymentState>, standard_amount_due: BalanceOf, @@ -485,15 +485,15 @@ impl Pallet { log::error!("Error while reserving partial amount due: {:?}", e); e })?; - contract_payment_state.settle_partial_overdrafted(reservable); + contract_payment_state.settle_partial_overdraft(reservable); - Self::deposit_event(Event::ContractPaymentOverdrafted { + Self::deposit_event(Event::ContractPaymentOverdrawn { contract_id: contract.contract_id, timestamp: now, // This is the partial amount successfully reserved from the user's account in this billing cycle partially_billed_amount: reservable, // This is the total overdrafted amount for this contract since grace period started - overdrafted_amount: contract_payment_state.get_overdrafted(), + overdrafted_amount: contract_payment_state.get_overdraft(), }); Ok(().into()) } @@ -512,8 +512,8 @@ impl Pallet { let should_distribute_rewards = contract_payment_state.cycles >= T::DistributionFrequency::get() || is_deleted; if should_distribute_rewards && contract_payment_state.has_reserved_amount() { - let standard_rewards = contract_payment_state.standard_reserved; - let additional_rewards = contract_payment_state.additional_reserved; + let standard_rewards = contract_payment_state.standard_reserve; + let additional_rewards = contract_payment_state.additional_reserve; // distribute additional rewards to the farm twin let remainder = if let types::ContractData::RentContract(_) = &contract.contract_type { @@ -540,7 +540,7 @@ impl Pallet { distributed_additional_amount ); } - contract_payment_state.additional_reserved = remainder; + contract_payment_state.additional_reserve = remainder; log::info!( "Distributing standard rewards from twin {:?} with amount {:?}", @@ -560,7 +560,7 @@ impl Pallet { e })?; - contract_payment_state.standard_reserved = BalanceOf::::zero(); + contract_payment_state.standard_reserve = BalanceOf::::zero(); contract_payment_state.cycles = 0; log::info!( diff --git a/substrate-node/pallets/pallet-smart-contract/src/lib.rs b/substrate-node/pallets/pallet-smart-contract/src/lib.rs index 7e1095af9..443bac3df 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/lib.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/lib.rs @@ -349,7 +349,7 @@ pub mod pallet { grace_period: u64, }, // Overdafted incurred - ContractPaymentOverdrafted { + ContractPaymentOverdrawn { contract_id: u64, timestamp: u64, partially_billed_amount: BalanceOf, // amount billed diff --git a/substrate-node/pallets/pallet-smart-contract/src/types.rs b/substrate-node/pallets/pallet-smart-contract/src/types.rs index 3567f57b9..2ee0b4f7c 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/types.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/types.rs @@ -233,10 +233,10 @@ pub struct ContractLock { PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo, MaxEncodedLen, )] pub struct ContractPaymentState { - pub standard_reserved: BalanceOf, - pub additional_reserved: BalanceOf, - pub standard_overdrafted: BalanceOf, - pub additional_overdrafted: BalanceOf, + pub standard_reserve: BalanceOf, + pub additional_reserve: BalanceOf, + pub standard_overdraft: BalanceOf, + pub additional_overdraft: BalanceOf, pub last_updated_seconds: u64, pub cycles: u16, } @@ -265,80 +265,80 @@ where { // accumulate the standard reserved amount pub fn reserve_standard_amount(&mut self, amount: BalanceOf) { - self.standard_reserved.defensive_saturating_accrue(amount); + self.standard_reserve.defensive_saturating_accrue(amount); } // accumulate the additional reserved amount pub fn reserve_additional_amount(&mut self, amount: BalanceOf) { - self.additional_reserved.defensive_saturating_accrue(amount); + self.additional_reserve.defensive_saturating_accrue(amount); } // accumulate the standard overdrafted amount pub fn overdraft_standard_amount(&mut self, amount: BalanceOf) { - self.standard_overdrafted + self.standard_overdraft .defensive_saturating_accrue(amount); } // accumulate the additional overdrafted amount pub fn overdraft_additional_amount(&mut self, amount: BalanceOf) { - self.additional_overdrafted + self.additional_overdraft .defensive_saturating_accrue(amount); } // Method to settle the standard overdrafted amount - pub fn settle_overdrafted_standard_amount(&mut self) { - self.standard_reserved - .defensive_saturating_accrue(self.standard_overdrafted); - self.standard_overdrafted = BalanceOf::zero(); + pub fn settle_overdraft_standard_amount(&mut self) { + self.standard_reserve + .defensive_saturating_accrue(self.standard_overdraft); + self.standard_overdraft = BalanceOf::zero(); } // Method to settle the additional overdrafted amount - pub fn settle_overdrafted_additional_amount(&mut self) { - self.additional_reserved - .defensive_saturating_accrue(self.additional_overdrafted); - self.additional_overdrafted = BalanceOf::zero(); + pub fn settle_overdraft_additional_amount(&mut self) { + self.additional_reserve + .defensive_saturating_accrue(self.additional_overdraft); + self.additional_overdraft = BalanceOf::zero(); } // Method to settle both standard and additional overdrafted amounts - pub fn settle_overdrafted(&mut self) { - self.settle_overdrafted_standard_amount(); - self.settle_overdrafted_additional_amount(); + pub fn settle_overdraft(&mut self) { + self.settle_overdraft_standard_amount(); + self.settle_overdraft_additional_amount(); } - // Method to return the sum of standard_overdrafted and additional_overdrafted - pub fn get_overdrafted(&self) -> BalanceOf { - self.standard_overdrafted - .defensive_saturating_add(self.additional_overdrafted) + // Method to return the sum of standard_overdraft and additional_overdraft + pub fn get_overdraft(&self) -> BalanceOf { + self.standard_overdraft + .defensive_saturating_add(self.additional_overdraft) } - // Method to return the sum of standard_reserved_amount and additional_reserved_amount + // Method to return the sum of standard_reserve_amount and additional_reserve_amount pub fn get_reserved(&self) -> BalanceOf { - self.standard_reserved - .defensive_saturating_add(self.additional_reserved) + self.standard_reserve + .defensive_saturating_add(self.additional_reserve) } // Method to return weather the contract has reserved amount or not pub fn has_reserved_amount(&self) -> bool { - !self.standard_reserved.is_zero() || !self.additional_reserved.is_zero() + !self.standard_reserve.is_zero() || !self.additional_reserve.is_zero() } // Method to return weather the contract has overdrafted amount or not - pub fn has_overdrafted_amount(&self) -> bool { - !self.standard_overdrafted.is_zero() || !self.additional_overdrafted.is_zero() + pub fn has_overdraft(&self) -> bool { + !self.standard_overdraft.is_zero() || !self.additional_overdraft.is_zero() } // Method to settle partial overdrafted amount - pub fn settle_partial_overdrafted(&mut self, amount: BalanceOf) { + pub fn settle_partial_overdraft(&mut self, amount: BalanceOf) { let mut remaining_amount = amount; // Settle additional overdraft first if remaining_amount > BalanceOf::zero() { - if remaining_amount >= self.additional_overdrafted { - remaining_amount.defensive_saturating_reduce(self.additional_overdrafted); - self.additional_reserved - .defensive_saturating_accrue(self.additional_overdrafted); - self.additional_overdrafted = BalanceOf::zero(); + if remaining_amount >= self.additional_overdraft { + remaining_amount.defensive_saturating_reduce(self.additional_overdraft); + self.additional_reserve + .defensive_saturating_accrue(self.additional_overdraft); + self.additional_overdraft = BalanceOf::zero(); } else { - self.additional_overdrafted + self.additional_overdraft .defensive_saturating_reduce(remaining_amount); - self.additional_reserved + self.additional_reserve .defensive_saturating_accrue(remaining_amount); remaining_amount = BalanceOf::zero(); } @@ -346,15 +346,15 @@ where // Settle standard overdraft with any remaining amount if remaining_amount > BalanceOf::zero() { - if remaining_amount >= self.standard_overdrafted { - remaining_amount.defensive_saturating_reduce(self.standard_overdrafted); - self.standard_reserved - .defensive_saturating_accrue(self.standard_overdrafted); - self.standard_overdrafted = BalanceOf::zero(); + if remaining_amount >= self.standard_overdraft { + remaining_amount.defensive_saturating_reduce(self.standard_overdraft); + self.standard_reserve + .defensive_saturating_accrue(self.standard_overdraft); + self.standard_overdraft = BalanceOf::zero(); } else { - self.standard_overdrafted + self.standard_overdraft .defensive_saturating_reduce(remaining_amount); - self.standard_reserved + self.standard_reserve .defensive_saturating_accrue(remaining_amount); } }