fix(circle): add payout cooldown and zero net payout guard - #415
Open
fridaypetra55-afk wants to merge 1 commit into
Open
Conversation
…ocor-tech#115, cocor-tech#117) ## Summary Closes cocor-tech#115 — configurable payout cooldown between rounds Closes cocor-tech#117 — zero net payout guard before token transfer ## Changes ### packages/common/src/types.rs - Add `payout_cooldown_seconds: u64` field to `CircleConfig` (additive, defaults to 0 to preserve backward compatibility with existing circles) ### packages/circle/src/types.rs - Add `payout_cooldown_seconds: u64` and `last_payout_timestamp: u64` to the `Circle` struct - Add `LastPayoutTimestamp` variant to `DataKey` enum - Add `PayoutCooldownActive = 40` to `CircleError` enum ### packages/circle/src/contract.rs - Restore full contract implementation from commit f0d9b39 (replaced by a stub in dd8750f) - `init`: populate `payout_cooldown_seconds` from config; `last_payout_timestamp` initialises to 0 - `trigger_payout` (cocor-tech#115): enforce cooldown guard — if `payout_cooldown_seconds > 0` and a prior payout has occurred (`last_payout_timestamp > 0`), reject with `PayoutCooldownActive` until `now >= last_payout_timestamp + cooldown` - `trigger_payout` (cocor-tech#117): the `net <= 0 → ZeroPayoutAmount` guard was already present in f0d9b39 and is preserved here; no zero-value transfers or misleading payout events are emitted - `trigger_payout`: write `circle.last_payout_timestamp = now` after every successful payout to seed future cooldown checks - `get_status` default struct updated with the two new fields (both 0) ### Test files - `packages/circle/src/test.rs`: add `payout_cooldown_seconds: 0` to both `create_config` helpers; add 8 new tests covering happy paths, blocked-within-window, passes-after-window, zero-cooldown, field defaults, zero-net rejection, and reasonable/zero-fee success - `packages/circle/src/tests/test_migration.rs`: replace broken stub with two storage-versioning tests for the new fields - `packages/circle/src/tests/test_integration.rs`, `test_stress.rs`: add `payout_cooldown_seconds` to `CircleConfig` literals - Create missing test stub files referenced by `tests/mod.rs` - `packages/circle-factory/src/test.rs`: add `payout_cooldown_seconds` to `CircleConfig` literal in `sample_config` ### packages/treasury/src/contract.rs - Remove pre-existing compile error: `env.has_contract()` does not exist in the Soroban SDK; removed the dead guard (non-functional check) ## Test results 54 passed; 0 failed (circle package)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #115
Closes #117
Problem
#115 —
trigger_payoutcould be called back-to-back with no minimum interval between rounds, allowing rapid repeat calls and potential race conditions.#117 — When the fee is high enough that the net payout rounds down to zero (e.g., 100% fee_bps), the contract would transfer zero tokens and emit a misleading
PayoutExecutedevent, wasting gas and corrupting the audit trail.Changes
packages/common/src/types.rspayout_cooldown_seconds: u64toCircleConfig(additive field; defaults to0= no cooldown, fully backward-compatible)packages/circle/src/types.rspayout_cooldown_seconds: u64andlast_payout_timestamp: u64to theCirclestructLastPayoutTimestampto theDataKeyenumPayoutCooldownActive = 40toCircleErrorpackages/circle/src/contract.rsf0d9b39) which was accidentally overwritten with a stub indd8750ftrigger_payout: enforce cooldown guard before resolving payout type. Ifpayout_cooldown_seconds > 0and a previous payout has been recorded (last_payout_timestamp > 0), reject withPayoutCooldownActiveuntilnow >= last_payout_timestamp + cooldown_seconds. First-ever payout on a circle (last_payout_timestamp == 0) always succeeds.trigger_payout: writecircle.last_payout_timestamp = nowafter every successful payouttrigger_payout: the existingnet <= 0 → ZeroPayoutAmountguard is preserved/surfacedinit: copypayout_cooldown_secondsfromCircleConfig;last_payout_timestampinitialises to0get_statusdefault struct updated with the two new fieldsTests (10 new tests, all 54 pass)
ZeroPayoutAmount; reasonable fee and zero fee succeedOther files
test_integration.rs,test_stress.rs,circle-factory/src/test.rs: addpayout_cooldown_secondstoCircleConfigliteralstests/mod.rs(pre-existing compile blocker)packages/treasury/src/contract.rs: remove pre-existing compile error (env.has_contract()does not exist in the Soroban SDK)Test Results