Skip to content

Commit

Permalink
final fix
Browse files Browse the repository at this point in the history
  • Loading branch information
baitcode committed Jan 9, 2025
1 parent fafba15 commit b55b320
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
17 changes: 8 additions & 9 deletions src/staker.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait IStaker<TContractState> {
) -> u128;

// Gets the cumulative staked amount * per second staked for the given timestamp and account.
fn get_cumulative_seconds_per_total_staked_at(self: @TContractState, timestamp: u64) -> Option<UFixedPoint124x128>;
fn get_cumulative_seconds_per_total_staked_at(self: @TContractState, timestamp: u64) -> UFixedPoint124x128;

}

Expand Down Expand Up @@ -368,10 +368,10 @@ use super::super::staker_log::LogOperations;
self.get_average_delegated(delegate, now - period, now)
}

fn get_cumulative_seconds_per_total_staked_at(self: @ContractState, timestamp: u64) -> Option<UFixedPoint124x128> {
fn get_cumulative_seconds_per_total_staked_at(self: @ContractState, timestamp: u64) -> UFixedPoint124x128 {
if let Option::Some((log_record, idx)) = self.staking_log.find_in_change_log(timestamp) {

let total_staked = if (idx == self.staking_log.len()) {
let total_staked = if (idx == self.staking_log.len() - 1) {
// if last rescord found
self.total_staked.read()
} else {
Expand All @@ -381,7 +381,7 @@ use super::super::staker_log::LogOperations;
let divisor = next_log_record.cumulative_seconds_per_total_staked - log_record.cumulative_seconds_per_total_staked;

if divisor.is_zero() {
return Option::None;
return 0_u64.into();
}

let total_staked_fp = div_u64_by_fixed_point(
Expand All @@ -393,7 +393,6 @@ use super::super::staker_log::LogOperations;
total_staked_fp.round()
};


let seconds_diff = (timestamp - log_record.timestamp) / 1000;

let staked_seconds: UFixedPoint124x128 = if total_staked == 0 {
Expand All @@ -402,10 +401,10 @@ use super::super::staker_log::LogOperations;
div_u64_by_u128(seconds_diff, total_staked)
};

return Option::Some(log_record.cumulative_seconds_per_total_staked + staked_seconds);
} else {
return Option::Some(0_u64.into());
}
return log_record.cumulative_seconds_per_total_staked + staked_seconds;
}

return 0_u64.into();
}
}
}
29 changes: 14 additions & 15 deletions src/staker_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ mod staker_staked_seconds_per_total_staked_calculation {
#[test]
fn test_should_return_0_if_no_data_found() {
let (staker, _) = setup(10000);
assert(staker.get_cumulative_seconds_per_total_staked_at(0) == 0_u64.into(), 'At 0 should be 0');
assert(staker.get_cumulative_seconds_per_total_staked_at(1000) == 0_u64.into(), 'At 1000 should be 0');
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(0), 0, 0_u128.into());
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(1000), 0, 0_u128.into());
}


Expand Down Expand Up @@ -480,25 +480,24 @@ mod staker_staked_seconds_per_total_staked_calculation {
token.approve(staker.contract_address, 7);
staker.stake(delegatee); // Will transfer 7 token to contract account and setup delegatee

assert(staker.get_cumulative_seconds_per_total_staked_at(0) == 0_u64.into(), 'At 0 should be 0');
assert(staker.get_cumulative_seconds_per_total_staked_at(500) == 0_u64.into(), 'At 500 should be 0');
assert(staker.get_cumulative_seconds_per_total_staked_at(999) == 0_u64.into(), 'At 999 should be 0');
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(0), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(500), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(999), 0, 0_u128);

assert_fp(staker.get_cumulative_seconds_per_total_staked_at(1000), 0, 0x80000000000000000000000000000000_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(2000), 1, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(3000), 1, 0x80000000000000000000000000000000_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(4000), 2, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(5000), 2, 0x80000000000000000000000000000000_u128);

// // NOTE: After 5s value stops changing as nothing is staked. @Moody is that a desired behaviour?
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(6000), 2, 0x80000000000000000000000000000000_u128);
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(7000), 2, 0x80000000000000000000000000000000_u128);
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(8000), 2, 0x80000000000000000000000000000000_u128);
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(9000), 2, 0x80000000000000000000000000000000_u128);
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(10000), 2, 0x80000000000000000000000000000000_u128);
// // 7 were staked here
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(17000), 3, 0x80000000000000000000000000000000_u128);
// assert_fp(staker.get_cumulative_seconds_per_total_staked_at(24000), 4, 0x80000000000000000000000000000000_u128);
// here value is undefined as nothing was staked.
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(5000), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(6000), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(7000), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(8000), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(9000), 0, 0_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(10000), 2, 0x80000000000000000000000000000000_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(17000), 3, 0x80000000000000000000000000000000_u128);
assert_fp(staker.get_cumulative_seconds_per_total_staked_at(24000), 4, 0x80000000000000000000000000000000_u128);
}

}

0 comments on commit b55b320

Please sign in to comment.