Skip to content

feat(contracts/market): verify withdraw cooldown respects LastDepositTime (#565) - #628

Merged
Mimah97 merged 1 commit into
Vatix-Protocol:devfrom
davismas23:feat/565-withdraw-cooldown-last-deposit-time
Jul 28, 2026
Merged

feat(contracts/market): verify withdraw cooldown respects LastDepositTime (#565)#628
Mimah97 merged 1 commit into
Vatix-Protocol:devfrom
davismas23:feat/565-withdraw-cooldown-last-deposit-time

Conversation

@davismas23

Copy link
Copy Markdown
Contributor

Summary

Closes #565

Documents and adds targeted unit tests confirming the withdrawal cooldown behaviour introduced in issue #413.

Problem

The WITHDRAW_COOLDOWN_SECONDS (3 600 s / 1 hour) guard in withdraw_unused_collateral was reading LastDepositTime correctly, but:

  • There were no dedicated unit tests proving the early-withdraw rejection and the post-cooldown success paths.
  • The module documentation had no explanation of the cooldown contract, leaving the deposit → timestamp → cooldown relationship implicit.

Without explicit test coverage, a future refactor could accidentally drop the check and CI would not catch it.

Changes

contracts/market/src/withdraw.rs

Documentation — new ## Withdrawal cooldown (#413 / #565) section in the module-level doc comment:

  • Explains the guard purpose (deter deposit/withdraw round-trips)
  • Shows the full data-flow: deposit_collateral → set_last_deposit_time → withdraw check
  • Documents the no-deposit-record edge case

Tests — three new unit tests in the existing #[cfg(test)] mod tests block:

Test What it verifies
test_withdraw_blocked_within_cooldown Returns WithdrawCooldownActive when elapsed = 0 < 3 600
test_withdraw_allowed_after_cooldown Succeeds when elapsed = 3 600 >= 3 600 (exact boundary)
test_withdraw_no_deposit_record_bypasses_cooldown No LastDepositTime → cooldown is not triggered

Core implementation (unchanged)

// 3. Enforce cooldown: user must wait WITHDRAW_COOLDOWN_SECONDS after their last deposit.
if let Some(last_deposit_time) = storage::get_last_deposit_time(&env, market_id, &user) {
    let elapsed = env.ledger().timestamp().saturating_sub(last_deposit_time);
    if elapsed < WITHDRAW_COOLDOWN_SECONDS {
        return Err(ContractError::WithdrawCooldownActive);
    }
}

Acceptance criteria

  • Early withdraw (0 s elapsed) fails with WithdrawCooldownActive
  • Withdrawal at exactly the boundary (3 600 s elapsed) succeeds
  • User with no deposit record is not blocked by cooldown

Notes

…Time (Vatix-Protocol#565)

The WITHDRAW_COOLDOWN_SECONDS (3 600 s) guard has been present in
withdraw_unused_collateral since issue Vatix-Protocol#413, but had no dedicated unit-test
coverage and its interaction with LastDepositTime was not documented.

This commit:

1. Adds a clear module-level doc section (## Withdrawal cooldown) to
   withdraw.rs describing the full deposit → timestamp → cooldown flow,
   including the data-flow diagram and the edge-case when no deposit has
   yet been recorded.

2. Adds three focused unit tests inside the existing #[cfg(test)] mod tests
   block that confirm the acceptance criteria for issue Vatix-Protocol#565:

   • test_withdraw_blocked_within_cooldown
     Sets LastDepositTime to the current ledger timestamp and asserts that
     an immediate withdrawal returns WithdrawCooldownActive.

   • test_withdraw_allowed_after_cooldown
     Sets LastDepositTime to (now − WITHDRAW_COOLDOWN_SECONDS), placing
     the user exactly at the boundary where elapsed >= cooldown, and
     asserts the withdrawal succeeds.

   • test_withdraw_no_deposit_record_bypasses_cooldown
     Asserts that a user with no LastDepositTime key does NOT receive
     WithdrawCooldownActive — the cooldown only fires after a deposit.

The core implementation in withdraw_unused_collateral is unchanged:
  if let Some(last_deposit_time) = storage::get_last_deposit_time(...)
      elapsed = timestamp.saturating_sub(last_deposit_time)
      if elapsed < WITHDRAW_COOLDOWN_SECONDS → Err(WithdrawCooldownActive)

Closes Vatix-Protocol#565
@Mimah97
Mimah97 merged commit 3d27fd7 into Vatix-Protocol:dev Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Withdraw cooldown respects LastDepositTime

2 participants