@@ -8,7 +8,9 @@ use frame_support::{dispatch::DispatchErrorWithPostInfo, traits::Get};
8
8
use pallet_tfgrid:: types as pallet_tfgrid_types;
9
9
use sp_runtime:: { Percent , SaturatedConversion } ;
10
10
use substrate_fixed:: types:: U64F64 ;
11
- use tfchain_support:: { resources:: Resources , types:: NodeCertification } ;
11
+ use tfchain_support:: {
12
+ constants:: time:: SECS_PER_HOUR , resources:: Resources , types:: NodeCertification ,
13
+ } ;
12
14
13
15
impl < T : Config > Contract < T > {
14
16
pub fn get_billing_info ( & self ) -> ContractBillingInformation {
@@ -40,8 +42,12 @@ impl<T: Config> Contract<T> {
40
42
let total_cost_tft_64 = calculate_cost_in_tft_from_musd :: < T > ( total_cost) ?;
41
43
42
44
// Calculate the amount due and discount received based on the total_cost amount due
43
- let ( amount_due, discount_received) =
44
- calculate_discount :: < T > ( total_cost_tft_64, balance, certification_type) ;
45
+ let ( amount_due, discount_received) = calculate_discount :: < T > (
46
+ total_cost_tft_64,
47
+ seconds_elapsed,
48
+ balance,
49
+ certification_type,
50
+ ) ;
45
51
46
52
return Ok ( ( amount_due, discount_received) ) ;
47
53
}
@@ -229,6 +235,7 @@ pub(crate) fn calculate_cu(cru: U64F64, mru: U64F64) -> U64F64 {
229
235
// (default, bronze, silver, gold or none)
230
236
pub fn calculate_discount < T : Config > (
231
237
amount_due : u64 ,
238
+ seconds_elapsed : u64 ,
232
239
balance : BalanceOf < T > ,
233
240
certification_type : NodeCertification ,
234
241
) -> ( BalanceOf < T > , types:: DiscountLevel ) {
@@ -239,13 +246,15 @@ pub fn calculate_discount<T: Config>(
239
246
) ;
240
247
}
241
248
242
- let balance_as_u128: u128 = balance. saturated_into :: < u128 > ( ) ;
243
-
244
249
// calculate amount due on a monthly basis
245
- // we bill every one hour so we can infer the amount due monthly (30 days ish)
246
- let amount_due_monthly = amount_due * 24 * 30 ;
250
+ // first get the normalized amount per hour
251
+ let amount_due_hourly = U64F64 :: from_num ( amount_due) * U64F64 :: from_num ( seconds_elapsed)
252
+ / U64F64 :: from_num ( SECS_PER_HOUR ) ;
253
+ // then we can infer the amount due monthly (30 days ish)
254
+ let amount_due_monthly = ( amount_due_hourly * 24 * 30 ) . round ( ) . to_num :: < u64 > ( ) ;
247
255
248
256
// see how many months a user can pay for this deployment given his balance
257
+ let balance_as_u128: u128 = balance. saturated_into :: < u128 > ( ) ;
249
258
let discount_level = U64F64 :: from_num ( balance_as_u128) / U64F64 :: from_num ( amount_due_monthly) ;
250
259
251
260
// predefined discount levels
0 commit comments