Skip to content

Commit

Permalink
Correct naming convention for ContractPaymentState API
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed Aug 18, 2024
1 parent 348cfb9 commit 72d2202
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
4 changes: 2 additions & 2 deletions clients/tfchain-client-go/contract_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion clients/tfchain-client-go/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ benchmarks! {
}: _(RawOrigin::Signed(farmer), contract_id)
verify {
let contract_payment_state = SmartContractModule::<T>::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::<T>::get_current_timestamp_in_secs(),
Expand Down
28 changes: 14 additions & 14 deletions substrate-node/pallets/pallet-smart-contract/src/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ impl<T: Config> Pallet<T> {
// 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);

Expand Down Expand Up @@ -315,7 +315,7 @@ impl<T: Config> Pallet<T> {
"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()
);
}

Expand Down Expand Up @@ -366,8 +366,8 @@ impl<T: Config> Pallet<T> {
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::<T>::locks(&account_id);
Expand Down Expand Up @@ -454,7 +454,7 @@ impl<T: Config> Pallet<T> {
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 {
Expand All @@ -468,7 +468,7 @@ impl<T: Config> Pallet<T> {
}

// Increasing the overdraft in the user's account
// Emits ContractPaymentOverdrafted event
// Emits ContractPaymentOverdrawn event
fn overdraft_funds(
contract_payment_state: &mut types::ContractPaymentState<BalanceOf<T>>,
standard_amount_due: BalanceOf<T>,
Expand All @@ -485,15 +485,15 @@ impl<T: Config> Pallet<T> {
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())
}
Expand All @@ -512,8 +512,8 @@ impl<T: Config> Pallet<T> {
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 {
Expand All @@ -540,7 +540,7 @@ impl<T: Config> Pallet<T> {
distributed_additional_amount
);
}
contract_payment_state.additional_reserved = remainder;
contract_payment_state.additional_reserve = remainder;

log::info!(
"Distributing standard rewards from twin {:?} with amount {:?}",
Expand All @@ -560,7 +560,7 @@ impl<T: Config> Pallet<T> {
e
})?;

contract_payment_state.standard_reserved = BalanceOf::<T>::zero();
contract_payment_state.standard_reserve = BalanceOf::<T>::zero();
contract_payment_state.cycles = 0;

log::info!(
Expand Down
2 changes: 1 addition & 1 deletion substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ pub mod pallet {
grace_period: u64,
},
// Overdafted incurred
ContractPaymentOverdrafted {
ContractPaymentOverdrawn {
contract_id: u64,
timestamp: u64,
partially_billed_amount: BalanceOf<T>, // amount billed
Expand Down
88 changes: 44 additions & 44 deletions substrate-node/pallets/pallet-smart-contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ pub struct ContractLock<BalanceOf> {
PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, Debug, TypeInfo, MaxEncodedLen,
)]
pub struct ContractPaymentState<BalanceOf> {
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,
}
Expand Down Expand Up @@ -265,96 +265,96 @@ 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();
}
}

// 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);
}
}
Expand Down

0 comments on commit 72d2202

Please sign in to comment.