Skip to content

Commit

Permalink
feat: full service contract changes extracted from #495
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter committed Nov 21, 2022
1 parent 5005532 commit 6be2007
Show file tree
Hide file tree
Showing 4 changed files with 1,224 additions and 6 deletions.
32 changes: 31 additions & 1 deletion substrate-node/pallets/pallet-smart-contract/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::pallet;
use crate::pallet::BalanceOf;
use crate::pallet::Error;
use crate::types;
use crate::types::{Contract, ContractBillingInformation};
use crate::types::{Contract, ContractBillingInformation, ServiceContract, ServiceContractBill};
use crate::Config;
use frame_support::dispatch::DispatchErrorWithPostInfo;
use pallet_tfgrid::types as pallet_tfgrid_types;
Expand Down Expand Up @@ -88,6 +88,36 @@ impl<T: Config> Contract<T> {
}
}

impl<T: Config> ServiceContract<T> {
pub fn calculate_bill_cost_tft(
&self,
service_bill: ServiceContractBill,
) -> Result<BalanceOf<T>, DispatchErrorWithPostInfo> {
// Calculate the cost in mUSD for service contract bill
let total_cost = self.calculate_bill_cost(service_bill);

if total_cost == 0 {
return Ok(BalanceOf::<T>::saturated_from(0 as u128));
}

// Calculate the cost in TFT for service contract
let total_cost_tft_64 = calculate_cost_in_tft_from_musd::<T>(total_cost)?;

// convert to balance object
let amount_due: BalanceOf<T> = BalanceOf::<T>::saturated_from(total_cost_tft_64);

return Ok(amount_due);
}

pub fn calculate_bill_cost(&self, service_bill: ServiceContractBill) -> u64 {
// bill user for service usage for elpased usage (window) in seconds
let contract_cost = U64F64::from_num(self.base_fee)
* U64F64::from_num(service_bill.window) / 3600
+ U64F64::from_num(service_bill.variable_amount);
contract_cost.round().to_num::<u64>()
}
}

// Calculates the total cost of a node contract.
pub fn calculate_resources_cost<T: Config>(
resources: &Resources,
Expand Down
Loading

0 comments on commit 6be2007

Please sign in to comment.