> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vanna.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Margin

> Create accounts, manage collateral and debt, and route external actions.

1. Resolve the trader's existing account using `MarginAccountService.discoverExistingAccount`. Account discovery in this app includes Registry storage and fallback reads; do not invent a Registry getter.
2. If needed, call AccountManager `create_account(trader_address)` with trader authorization.
3. Resolve the exact token and pool. Deposit using `deposit_collateral_tokens(smart_account, symbol, amount)` in the signature order shown in the reference.
4. Borrow only after inspecting account health, spendable balances, pool cash, utilization, permissions, and fee. Use the current builder for atomic same-asset or cross-asset operations.
5. For external strategies, call typed `exec` with the registered target, action, token addresses, protocol-specific amount units, and minima.
6. Repay with `repay(amount_wad, symbol, smart_account)` from SmartAccount-held tokens. Unwind external assets or transfer collateral in first if required.
7. To exit the account, settle recognized debt, verify `has_debt`, and call `close_account` after the activation ledger.

Treat each operation as independently fallible. AccountManager applies authorization, per-account reentrancy locks, configuration checks, and risk checks, while controller routing and the live post-exec gate have their own policies.

For UI integrations, reuse `lib/margin-utils.ts` and the wallet adapter. Its normalization keeps BLUSDC/USDC aliases within the Blend token identity, separate from AQUSDC and SOUSDC. Read [AccountManager](https://docs.vanna.finance/stellar/developers/contracts/account-manager.md) and [Transaction Flow](https://docs.vanna.finance/stellar/developers/sdk/transaction-flow.md) before encoding calls.

## Source reference

* `Vanna_Finance_Backend/apps/stellar/lib/margin-utils.ts`
* `Protocol_V1_Soroban_testnet/contracts/AccountManagerContract/src/account_manager.rs`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.