Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Worked Example: Portfolio Rebalancing Vault

This guide walks through examples/portfolio_vault, a multi-asset index vault that mints portfolio shares, tracks oracle prices, and executes simplified oracle-priced rebalancing swaps.

Sourceexamples/portfolio_vault
Cratesfixtures · assert · core · generators
Tests30+ across 4 files
Runcargo test -p soroban-testkit-example-portfolio-vault

Contract model

PortfolioVault stores target asset weights (basis points), oracle prices, and user share balances. Deposits mint shares proportional to contributed value; withdrawals burn shares pro-rata across vault assets; rebalance executes an oracle-priced swap between two vault assets with slippage protection.

Four-file test split

FileCrateFocus
tests/fixtures.rssoroban-testkit-fixturesDeployment, deposits, withdrawals, oracle updates, rebalance setup
tests/assert.rssoroban-testkit-assertAuth/errors and invalid-input rejection
tests/budget_storage.rssoroban-testkit-coreBudget snapshots, storage snapshots, value preservation checks
tests/proptest.rssoroban-testkit-generators + proptestProperty invariants for deposits, withdrawals, rebalance value/tolerance

Fixtures

tests/common/mod.rs deploys three Stellar asset contracts, registers the vault, initializes 50/30/20 target weights, and sets initial oracle prices.

Assert

Auth and error tests verify admin-only price updates/rebalance, invalid inputs, and insufficient share withdrawals.

Core budget/storage

Budget tests capture BudgetSnapshot::last_invocation for deposit/withdraw/ rebalance. Storage tests inspect vault snapshots and verify share balance cleanup and value preservation across oracle-priced rebalance.

Property tests

Proptest cases verify minted shares are positive, withdrawals cannot exceed shares, oracle-priced rebalance preserves total value, and single-depositor share accounting is consistent.

Running it

cargo test -p soroban-testkit-example-portfolio-vault
cargo clippy -p soroban-testkit-example-portfolio-vault --all-targets -- -D warnings