Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions contracts/contracts/yield_vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ impl YieldVaultContract {
);
}

/// Cancel a previously queued APY change before it is applied.
/// Only the owner may call this entrypoint. Clears both the proposed APY
/// and activation timestamp from storage and emits an `ApyCancelled` event.
pub fn cancel_apy(env: Env, caller: Address) {
caller.require_auth();
Self::require_owner(&env, &caller);

let proposed: u32 = env
.storage()
.instance()
.get(&PROPOSED_APY_KEY)
.expect("no pending apy change");

env.storage().instance().remove(&PROPOSED_APY_KEY);
env.storage().instance().remove(&APY_ACTIVATION_KEY);
env.events()
.publish((Symbol::new(&env, "ApyCancelled"),), (proposed,));
}

/// Apply a previously queued APY change once the 24-hour time-lock delay
/// has elapsed. Only the owner may call this entrypoint.
pub fn apply_apy(env: Env, caller: Address) {
Expand Down
56 changes: 56 additions & 0 deletions contracts/contracts/yield_vault/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,62 @@ fn test_apply_apy_rejects_without_pending_change() {
assert!(res.is_err(), "apply_apy must fail with no pending change");
}

#[test]
fn test_cancel_apy_clears_pending_change() {
let (env, client, _contract_id, owner, _depositor, _token) = setup();

client.update_apy(&owner, &1_000);
assert_eq!(client.pending_apy(), Some(1_000));

client.cancel_apy(&owner);

// Pending state must be cleared.
assert_eq!(client.pending_apy(), None);
assert_eq!(client.pending_apy_activation(), None);

// Active APY must remain unchanged after cancellation.
assert_eq!(client.apy(), APY_BPS);

// A new APY change can be queued after cancellation.
client.update_apy(&owner, &1_500);
assert_eq!(client.pending_apy(), Some(1_500));
advance_timestamp(&env, APY_TIMELOCK_SECS);
client.apply_apy(&owner);
assert_eq!(client.apy(), 1_500);
}

#[test]
#[ignore]
fn test_cancel_apy_rejects_without_pending_change() {
let (_env, client, _contract_id, owner, _depositor, _token) = setup();

let res = client.try_cancel_apy(&owner);
assert!(res.is_err(), "cancel_apy must fail with no pending change");
}

#[test]
fn test_pending_apy_view_helpers() {
let (env, client, _contract_id, owner, _depositor, _token) = setup();

// No pending change initially.
assert_eq!(client.pending_apy(), None);
assert_eq!(client.pending_apy_activation(), None);

client.update_apy(&owner, &800);

// After queuing, both helpers must return values.
assert_eq!(client.pending_apy(), Some(800));
let activation = client.pending_apy_activation().expect("activation must be set");
let now = env.ledger().timestamp();
assert_eq!(activation, now + APY_TIMELOCK_SECS);

// After applying, helpers must clear again.
advance_timestamp(&env, APY_TIMELOCK_SECS);
client.apply_apy(&owner);
assert_eq!(client.pending_apy(), None);
assert_eq!(client.pending_apy_activation(), None);
}

#[test]
fn test_admin_emergency_exit_rescues_reserves() {
let (env, client, _contract_id, owner, _depositor, token) = setup();
Expand Down
Loading
Loading