Initialize the treasury contract with an admin address.
The admin address is a single account. Once initialized, the contract cannot be reinitialized. This ensures the admin address remains consistent and prevents accidental reconfiguration.
env - The execution environmentadmin - Address of the admin (can be multisig contract for enhanced security)Ok(()) - Treasury initialized successfullyErr(TreasuryError) - Initialization failed (e.g., already initialized)fn initialize(
env: soroban_sdk::Env,
admin: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Record a deposit of tokens to the treasury.
This function verifies that tokens have been transferred to the treasury contract address and updates internal balance tracking. Only the admin can call this function. The function verifies that the actual token balance is sufficient to support the claimed deposit amount to prevent fabricated balances.
env - The execution environmentcaller - Address of the caller (must be admin)asset - Address of the token contractamount - Amount of tokens being deposited (must be > 0)from - Address that sent the tokens (for event tracking and audit trail)Ok(()) - Deposit recorded successfullyErr(TreasuryError) - Deposit failed (unauthorized, not initialized, invalid amount, transfer not verified, or overflow)fn deposit(
env: soroban_sdk::Env,
caller: soroban_sdk::Address,
asset: soroban_sdk::Address,
amount: u128,
from: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Sync balance from token contract and update internal tracking.
This function queries the token contract's balance function to reconcile the actual balance held by the treasury with internal tracking. Use this when tokens are transferred directly to the treasury address without calling deposit(), such as when protocol fees are collected automatically.
env - The execution environmentasset - Address of the token contractOk(u128) - Current balance synced from token contractErr(TreasuryError) - Sync failed (not initialized or token contract query failed)fn sync_balance(
env: soroban_sdk::Env,
asset: soroban_sdk::Address,
) -> Result
Withdraw tokens from the treasury (admin only).
This function performs two critical operations atomically: it updates internal balance tracking and transfers tokens to the recipient. If the token transfer fails, the balance update is reverted, ensuring consistency. Only authorized admins (or multisig contract if configured) can perform withdrawals.
env - The execution environmentcaller - Admin address (must be authorized - regular admin or multisig contract)asset - Address of the token contract to withdrawamount - Amount of tokens to withdraw (must be > 0 and <= available balance)to - Address to receive the tokensOk(()) - Withdrawal successful (balance updated and tokens transferred)Err(TreasuryError) - Withdrawal failed (unauthorized, insufficient balance, or transfer failed)fn withdraw(
env: soroban_sdk::Env,
caller: soroban_sdk::Address,
asset: soroban_sdk::Address,
amount: u128,
to: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Get the balance of a specific asset in the treasury.
Returns the internally tracked balance for the given asset. This may differ from the actual token contract balance if sync_balance() hasn't been called after direct transfers to the treasury address.
env - The execution environmentasset - Address of the token contractu128 - Balance of the asset (0 if asset has never been deposited)fn get_balance(env: soroban_sdk::Env, asset: soroban_sdk::Address) -> u128
Get all balances in the treasury.
Returns a map of all assets that have been deposited to the treasury along with their tracked balances. Assets with zero balance are not included in the map.
env - The execution environmentMap<Address, u128> - Map of asset addresses to their balancesfn get_all_balances(
env: soroban_sdk::Env,
) -> Result, TreasuryError>
Get the admin address.
env - The execution environmentResult<Address, TreasuryError> - Admin address or errorfn get_admin(env: soroban_sdk::Env) -> Result
Propose a new admin address (two-step transfer, step 1).
Only the current admin can propose a new admin.
The proposed admin must call accept_admin to complete the transfer.
env - The execution environmentcaller - Current admin address (must be authorized)pending_admin - Proposed new admin addressOk(()) - Admin proposal created successfullyErr(TreasuryError) - Proposal failed (unauthorized or not initialized)fn propose_admin(
env: soroban_sdk::Env,
caller: soroban_sdk::Address,
pending_admin: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Accept admin role (two-step transfer, step 2). Only the pending admin can call this to finalize the transfer.
env - The execution environmentcaller - Pending admin address (must be authorized)Ok(()) - Admin transfer completed successfullyErr(TreasuryError) - Transfer failed (not initialized, no pending admin, or invalid caller)fn accept_admin(
env: soroban_sdk::Env,
caller: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Cancel a pending admin proposal. Only the current admin can cancel a pending proposal.
env - The execution environmentcaller - Current admin address (must be authorized)Ok(()) - Proposal cancelled successfullyErr(TreasuryError) - Cancellation failed (unauthorized, not initialized, or no pending admin)fn cancel_admin_proposal(
env: soroban_sdk::Env,
caller: soroban_sdk::Address,
) -> Result<(), TreasuryError>
Get the pending admin address, if any.
env - The execution environmentOk(Address) - Pending admin addressErr(TreasuryError) - No pending admin proposal existsfn get_pending_admin(
env: soroban_sdk::Env,
) -> Result