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 updates internal balance tracking after tokens have been transferred to the treasury contract address. The actual token transfer must occur via the token contract's transfer function before calling this method. This separation allows the treasury to track protocol fees collected from various sources.
env - The execution environmentasset - 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 (not initialized, invalid amount, or overflow)fn deposit(
env: soroban_sdk::Env,
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,
) -> soroban_sdk::Map
Get the admin address.
env - The execution environmentAddress - Admin addressfn get_admin(env: soroban_sdk::Env) -> soroban_sdk::Address