@@ -2,12 +2,11 @@ use crate::pallet;
2
2
use crate :: pallet:: BalanceOf ;
3
3
use crate :: pallet:: Error ;
4
4
use crate :: types;
5
- use crate :: types:: { Contract , ContractBillingInformation } ;
5
+ use crate :: types:: { Contract , ContractBillingInformation , ServiceContract , ServiceContractBill } ;
6
6
use crate :: Config ;
7
- use frame_support:: dispatch:: DispatchErrorWithPostInfo ;
7
+ use frame_support:: { dispatch:: DispatchErrorWithPostInfo , traits :: Get } ;
8
8
use pallet_tfgrid:: types as pallet_tfgrid_types;
9
- use sp_runtime:: Percent ;
10
- use sp_runtime:: SaturatedConversion ;
9
+ use sp_runtime:: { Percent , SaturatedConversion } ;
11
10
use substrate_fixed:: types:: U64F64 ;
12
11
use tfchain_support:: { resources:: Resources , types:: NodeCertification } ;
13
12
@@ -101,7 +100,8 @@ impl<T: Config> Contract<T> {
101
100
// Calculate total cost for a name contract
102
101
types:: ContractData :: NameContract ( _) => {
103
102
// bill user for name usage for number of seconds elapsed
104
- let total_cost_u64f64 = ( U64F64 :: from_num ( pricing_policy. unique_name . value ) / 3600 )
103
+ let total_cost_u64f64 = ( U64F64 :: from_num ( pricing_policy. unique_name . value )
104
+ / U64F64 :: from_num ( T :: BillingReferencePeriod :: get ( ) ) )
105
105
* U64F64 :: from_num ( seconds_elapsed) ;
106
106
total_cost_u64f64. to_num :: < u64 > ( )
107
107
}
@@ -111,6 +111,36 @@ impl<T: Config> Contract<T> {
111
111
}
112
112
}
113
113
114
+ impl ServiceContract {
115
+ pub fn calculate_bill_cost_tft < T : Config > (
116
+ & self ,
117
+ service_bill : ServiceContractBill ,
118
+ ) -> Result < BalanceOf < T > , DispatchErrorWithPostInfo > {
119
+ // Calculate the cost in mUSD for service contract bill
120
+ let total_cost = self . calculate_bill_cost :: < T > ( service_bill) ;
121
+
122
+ if total_cost == 0 {
123
+ return Ok ( BalanceOf :: < T > :: saturated_from ( 0 as u128 ) ) ;
124
+ }
125
+
126
+ // Calculate the cost in TFT for service contract
127
+ let total_cost_tft_64 = calculate_cost_in_tft_from_musd :: < T > ( total_cost) ?;
128
+
129
+ // convert to balance object
130
+ let amount_due: BalanceOf < T > = BalanceOf :: < T > :: saturated_from ( total_cost_tft_64) ;
131
+
132
+ return Ok ( amount_due) ;
133
+ }
134
+
135
+ pub fn calculate_bill_cost < T : Config > ( & self , service_bill : ServiceContractBill ) -> u64 {
136
+ // bill user for service usage for elpased usage (window) in seconds
137
+ let contract_cost = U64F64 :: from_num ( self . base_fee ) * U64F64 :: from_num ( service_bill. window )
138
+ / U64F64 :: from_num ( T :: BillingReferencePeriod :: get ( ) )
139
+ + U64F64 :: from_num ( service_bill. variable_amount ) ;
140
+ contract_cost. round ( ) . to_num :: < u64 > ( )
141
+ }
142
+ }
143
+
114
144
// Calculates the total cost of a node contract.
115
145
pub fn calculate_resources_cost < T : Config > (
116
146
resources : Resources ,
@@ -130,14 +160,16 @@ pub fn calculate_resources_cost<T: Config>(
130
160
let su_used = hru / 1200 + sru / 200 ;
131
161
// the pricing policy su cost value is expressed in 1 hours or 3600 seconds.
132
162
// we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed.
133
- let su_cost = ( U64F64 :: from_num ( pricing_policy. su . value ) / 3600 )
163
+ let su_cost = ( U64F64 :: from_num ( pricing_policy. su . value )
164
+ / U64F64 :: from_num ( T :: BillingReferencePeriod :: get ( ) ) )
134
165
* U64F64 :: from_num ( seconds_elapsed)
135
166
* su_used;
136
167
log:: debug!( "su cost: {:?}" , su_cost) ;
137
168
138
169
let cu = calculate_cu ( cru, mru) ;
139
170
140
- let cu_cost = ( U64F64 :: from_num ( pricing_policy. cu . value ) / 3600 )
171
+ let cu_cost = ( U64F64 :: from_num ( pricing_policy. cu . value )
172
+ / U64F64 :: from_num ( T :: BillingReferencePeriod :: get ( ) ) )
141
173
* U64F64 :: from_num ( seconds_elapsed)
142
174
* cu;
143
175
log:: debug!( "cu cost: {:?}" , cu_cost) ;
@@ -146,7 +178,8 @@ pub fn calculate_resources_cost<T: Config>(
146
178
147
179
if ipu > 0 {
148
180
let total_ip_cost = U64F64 :: from_num ( ipu)
149
- * ( U64F64 :: from_num ( pricing_policy. ipu . value ) / 3600 )
181
+ * ( U64F64 :: from_num ( pricing_policy. ipu . value )
182
+ / U64F64 :: from_num ( T :: BillingReferencePeriod :: get ( ) ) )
150
183
* U64F64 :: from_num ( seconds_elapsed) ;
151
184
log:: debug!( "ip cost: {:?}" , total_ip_cost) ;
152
185
total_cost += total_ip_cost;
0 commit comments