Skip to content

Commit

Permalink
test(revolut_test): Added _get_next_timestamp_key_tests, slight refac…
Browse files Browse the repository at this point in the history
…tor of _get_next_timestamp_key function

* added tests

* changed _get_next_timestamp_key to return 0 on 0 input

* _get_next_timestamp_key is more readable

 [CONTRACTS] implement _get_next_timestamp_key tests keep-starknet-strange#88
  • Loading branch information
leohscl committed Oct 4, 2024
1 parent 313ef40 commit 6f1ca07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
12 changes: 9 additions & 3 deletions contracts/src/contracts/ramps/revolut/revolut.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub mod RevolutRamp {
//

#[generate_trait]
impl InternalImpl of InternalTrait {
pub impl InternalImpl of InternalTrait {
fn _get_available_liquidity(self: @ContractState, liquidity_key: LiquidityKey) -> u256 {
let mut amount = self.liquidity.read(liquidity_key);
let current_timestamp = get_block_timestamp();
Expand All @@ -362,8 +362,14 @@ pub mod RevolutRamp {
}

fn _get_next_timestamp_key(self: @ContractState, after: u64) -> u64 {
// minus 1 in order to return `after` if it's already a valid key timestamp.
after - 1 + LOCK_DURATION_STEP - ((after - 1) % LOCK_DURATION_STEP)
if after.is_zero() {
0
} else {
// minus 1 in order to return `after` if it's already a valid key timestamp.
let increment_step = (after - 1) / LOCK_DURATION_STEP + 1;
// returns a multiple of LOCK_DURATION_STEP
LOCK_DURATION_STEP * increment_step
}
}
}
}
30 changes: 23 additions & 7 deletions contracts/src/contracts/ramps/revolut/revolut_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use snforge_std::{
use zkramp::contracts::ramps::revolut::interface::{ZKRampABIDispatcher, ZKRampABIDispatcherTrait, LiquidityKey};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp::{
Event, LiquidityAdded, LiquidityRetrieved, LiquidityLocked, LiquidityShareRequested, LiquidityShareWithdrawn,
MINIMUM_LOCK_DURATION
InternalImpl as RevolutRampInternalImpl, MINIMUM_LOCK_DURATION, LOCK_DURATION_STEP
};
use zkramp::contracts::ramps::revolut::revolut::RevolutRamp;
use zkramp::tests::constants;
use zkramp::tests::utils;

Expand Down Expand Up @@ -1167,19 +1168,34 @@ fn test_withdraw_liquidity_twice() {
// _get_next_timestamp_key
//

// #[test]
#[test]
fn test__get_next_timestamp_key_basic() {
panic!("Not implemented yet");
// setup
let state = RevolutRamp::contract_state_for_testing();
// test a value between 0 and LOCK_DURATION_STEP
let after = 10;
// should be rounded to the next threshold
assert_eq!(state._get_next_timestamp_key(:after), LOCK_DURATION_STEP);
}

// #[test]
#[test]
fn test__get_next_timestamp_key_for_timestamp_key() {
panic!("Not implemented yet");
// setup
let state = RevolutRamp::contract_state_for_testing();
// test a multiple of LOCK_DURATION_STEP
let after = MINIMUM_LOCK_DURATION;
// should return the same value
assert_eq!(state._get_next_timestamp_key(:after), MINIMUM_LOCK_DURATION);
}

// #[test]
#[test]
fn test__get_next_timestamp_key_from_zero() {
panic!("Not implemented yet");
// setup
let state = RevolutRamp::contract_state_for_testing();
// test with 0
let after = 0;
// should return the same value
assert_eq!(state._get_next_timestamp_key(:after), 0);
}

// #[test]
Expand Down

0 comments on commit 6f1ca07

Please sign in to comment.