SmartAccount is a per-user deployed contract — each margin account is a separate on-chain instance. It stores all collateral balances, records which assets have been borrowed, and executes external protocol calls on behalf of its owner.
Important: SmartAccount functions cannot be called directly by users. All calls must originate from the registered AccountManager. Every function begins with account_manager.require_auth().
Individual SmartAccount addresses are determined at account creation time. Use
Registry.get_accounts(trader_address) or listen for AccountCreationEvent to find a user’s SmartAccount address.Account State
activate_account
IsAccountActive = true. Called by AccountManager immediately after deploying or reusing a SmartAccount.
deactivate_account
IsAccountActive = false. Called by AccountManager when the account is closed and pushed to the inactive pool.
has_debt
true if any borrowed tokens are listed in BorrowedTokensList.
set_has_debt
HasDebt flag. Called by AccountManager when the debt list transitions from empty to non-empty or vice versa.
Collateral Management
get_all_collateral_tokens
add_collateral_token
symbol to CollateralTokensList if not already present. Called by AccountManager on first deposit of a new token type.
get_collateral_token_balance
symbol held in this account, in WAD format.
set_collateral_token_balance
symbol. Called by AccountManager after deposit or withdrawal.
remove_collateral_token_balance
amount (in native decimals) of symbol to trader, subtracts from stored CollateralBalanceWAD, and removes the token from CollateralTokensList if balance reaches zero.
sweep_to
to_address. Called in two scenarios:
close_account()— sweeps to ownerliquidate()— sweeps remaining collateral (after debt repayment) to trader
For tracking tokens (BLEND_XLM, AQ_XLM_USDC, etc.): the internal balance record is zeroed but the underlying position in the external protocol (Blend pool, Aquarius pool) is not unwound. The external position remains.
Debt Management
get_all_borrowed_tokens
add_borrowed_token
symbol to BorrowedTokensList. Called by AccountManager after a successful borrow.
remove_borrowed_token
symbol from BorrowedTokensList. Called when debt is fully repaid.
get_borrowed_token_debt
symbol.
remove_borrowed_token_balance
amount of symbol debt to the LendingPool. Called during repayment or liquidation.
External Protocol Execution
execute
(success: bool, token_delta: i128) where token_delta is the net token change (positive = received, negative = sent). AccountManager uses this delta to mint or burn TrackingTokens.
Action Types
Blend Deposit
- Reads current b-token positions from Blend
- Authorizes native XLM transfer to Blend pool
- Calls
BlendPool.submit([{request_type: 0, address: xlm_address, amount: 1000_0000000}]) - Reads updated positions
token_delta = positions_after - positions_before(b-tokens received)- AccountManager mints
TrackingToken(BLEND_XLM)for the SmartAccount
Blend Withdraw
Aquarius Add Liquidity
- Fetches pool reserves
- Computes optimal amounts (see Math Reference)
- Authorizes token transfers to pool
- Calls
Pool.deposit(desired_a, desired_b, min_a, min_b) token_delta= LP tokens received- AccountManager mints
TrackingToken(AQ_XLM_USDC)
Aquarius Remove Liquidity
Soroswap Swap
- Reads pair reserves
- Computes output via constant-product formula (0.3% fee)
- Authorizes XLM transfer to pair
- Calls
Pair.swap(amount_out, 0, smart_account) token_delta= USDC received
Storage Layout
| Key | Type | Description |
|---|---|---|
CollateralTokensList | Vec<Symbol> | All symbols with non-zero collateral |
CollateralBalanceWAD(symbol) | U256 | Collateral balance in WAD |
BorrowedTokensList | Vec<Symbol> | All symbols with outstanding debt |
HasDebt | bool | True if any debt exists |
IsAccountActive | bool | Whether account is active |
OwnerAddress | Address | Wallet that owns this account |
AccountManager | Address | Only address authorized to call this contract |
RegistryContract | Address | Registry for resolving peer addresses |
Events
Error Codes
Related
- AccountManager — the only authorized caller of SmartAccount
- Tracking Tokens — how external positions are represented
- Risk Engine — reads SmartAccount state to compute health factor
- Architecture — full call graph showing how SmartAccount fits in the system

