Skip to content

Commit

Permalink
fix(iota-types/timelock): add additional checks to the is_timelocked_…
Browse files Browse the repository at this point in the history
…vested_reward function (#5485)

* fix(iota-types/timelock): add additional checks to the is_timelocked_vested_reward function

* Fix review comments

* Fix comment

* tests fix

* fmt

---------

Co-authored-by: Mirko Zichichi <[email protected]>
  • Loading branch information
Dkwcs and miker83z authored Feb 21, 2025
1 parent e055165 commit 167848c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 8 additions & 2 deletions crates/iota-types/src/timelock/timelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ pub fn is_timelocked_vested_reward(
}

/// Checks if an output is a vested reward, if it has a specific ID prefix,
/// and if it contains a timelock unlock condition.
/// and if it contains a timelock unlock condition,
/// and if an output has no native tokens,
/// and if an output has only 2 unlock conditions and their address.
pub fn is_vested_reward(output_id: OutputId, basic_output: &BasicOutput) -> bool {
let has_vesting_prefix = output_id.to_string().starts_with(VESTED_REWARD_ID_PREFIX);

has_vesting_prefix && basic_output.unlock_conditions().timelock().is_some()
has_vesting_prefix
&& basic_output.unlock_conditions().timelock().is_some()
&& basic_output.native_tokens().len() == 0
&& basic_output.unlock_conditions().len() == 2
&& basic_output.unlock_conditions().address().is_some()
}

/// Creates a `TimeLock<Balance<IOTA>>` from a Stardust-based Basic Output
Expand Down
7 changes: 2 additions & 5 deletions crates/iota-types/src/unit_tests/timelock/timelock_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ fn timelock_from_stardust_extra_unlock_condition() {

let err = try_from_stardust(output_id, &output, 100).unwrap_err();

assert!(matches!(
err,
VestedRewardError::UnlockConditionsNumberMismatch
));
assert!(matches!(err, VestedRewardError::NotVestedReward));
}

#[test]
Expand All @@ -272,5 +269,5 @@ fn timelock_from_stardust_with_native_tokens() {

let err = try_from_stardust(output_id, &output, 100).unwrap_err();

assert!(matches!(err, VestedRewardError::NativeTokensNotSupported));
assert!(matches!(err, VestedRewardError::NotVestedReward));
}

0 comments on commit 167848c

Please sign in to comment.