feat(creator-keys): add protocol treasury withdrawal function#533
Merged
Chucks1093 merged 3 commits intoJun 29, 2026
Merged
Conversation
…layerorg#517) **Summary** Protocol fees accumulate in the contract with no way to extract them. This commit adds a complete treasury accounting layer and an admin-gated withdrawal function so accumulated fees can be moved to a multisig or treasury address on demand. **Changes** `ContractError` - Append `InsufficientTreasuryBalance = 28` at the end of the enum (ABI-safe per the existing ordering convention). `DataKey` - Append `TreasuryBalance` variant to track the protocol treasury separately from key-payment state and dividend accumulators. `constants::storage` - Expose `TREASURY_BALANCE` constant for consistent key access. Fee accumulation (`buy_key` / `accrue_sell_protocol_fee`) - Both paths now call `credit_treasury_balance` with the computed protocol-fee amount alongside the existing `ProtocolFeeRecipientBalance` credit. The two accumulators are independent: one is for the fee-recipient address to claim; the other is the admin-controlled treasury pool. `get_treasury_balance() -> i128` - Pure view that returns the current withdrawable treasury balance. Returns 0 before any fees have accrued. `withdraw_treasury(admin, amount, recipient) -> Result<i128, ContractError>` - Requires admin auth and `assert_is_admin` guard (Unauthorized). - Rejects zero/negative amounts (NotPositiveAmount). - Rejects amounts exceeding the current balance (InsufficientTreasuryBalance). - Decrements treasury balance atomically. - Partial and full (leaves 0) withdrawals are both supported. - Emits `TreasuryWithdrawalEvent { amount, recipient, remaining_balance, ledger }`. `events.rs` - `TREASURY_WITHDRAWAL_EVENT_NAME = "treas_out"` - `TREASURY_WITHDRAWAL_DATA_FIELDS` stability constant. - `TreasuryWithdrawalEvent` contracttype struct (append-only field order). - `treasury_withdrawal_event_topics(recipient)` helper. **Tests** (`tests/treasury_withdrawal.rs` — 13 integration tests) - `get_treasury_balance_returns_zero_before_any_trades` - `get_treasury_balance_increases_after_buy_key` - `get_treasury_balance_accumulates_across_multiple_buys` - `withdraw_treasury_succeeds_for_admin_partial` - `withdraw_treasury_succeeds_full_withdrawal_leaves_zero` - `withdraw_treasury_emits_correct_event` - `withdraw_treasury_rejects_non_admin` - `withdraw_treasury_rejects_zero_amount` - `withdraw_treasury_rejects_negative_amount` - `withdraw_treasury_rejects_over_withdrawal` - `withdraw_treasury_rejects_over_withdrawal_on_empty_balance` - `withdraw_treasury_multiple_partial_withdrawals_track_correctly` - `treasury_balance_is_independent_of_protocol_fee_recipient_balance` Closes accesslayerorg#517
Contributor
Author
|
gm, can you kindly review. all ci s passed |
Contributor
|
Nice implementation |
Closed
6 tasks
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 #517
Protocol fees accumulate in the contract but there was no way to move them out. This PR adds a complete treasury accounting layer and an admin-gated withdrawal function so accumulated fees can be extracted to a multisig or treasury address on demand.
Changes
ContractErrorInsufficientTreasuryBalance = 28(ABI-safe: added at end per existing ordering convention)DataKeyTreasuryBalancevariant — tracks protocol treasury separately from key-payment escrow and dividend accumulatorsFee accumulation (
buy_key/accrue_sell_protocol_fee)credit_treasury_balancewith the protocol-fee amount alongside the existingProtocolFeeRecipientBalancecreditget_treasury_balance() -> i1280before any fees accruewithdraw_treasury(admin, amount, recipient) -> Result<i128, ContractError>admin.require_auth()+assert_is_adminguard →Unauthorizedon non-admin callamount <= 0→NotPositiveAmountamount > current_balance→InsufficientTreasuryBalanceTreasuryWithdrawalEvent { amount, recipient, remaining_balance, ledger }events.rsTREASURY_WITHDRAWAL_EVENT_NAME = "treas_out"TREASURY_WITHDRAWAL_DATA_FIELDSfor indexer stabilityTreasuryWithdrawalEventcontracttype struct (append-only field ordering)treasury_withdrawal_event_topics(recipient)helperTest plan
tests/treasury_withdrawal.rs— 13 integration tests, all passing:get_treasury_balancereturns zero before any tradesget_treasury_balanceincreases afterbuy_keyget_treasury_balanceaccumulates across multiple buysTreasuryWithdrawalEventfields are correctUnauthorizedNotPositiveAmountNotPositiveAmountInsufficientTreasuryBalanceInsufficientTreasuryBalanceProtocolFeeRecipientBalanceAll 55 existing unit tests and all prior integration tests continue to pass.