Skip to content

Commit 691f96b

Browse files
committed
Add defi/vault-strategy: manager-run investment vault with Pyth oracle and rebalancing
Adds a new example under defi/vault-strategy/anchor demonstrating a manager-custodial investment vault on Solana. ## What this adds ### vault-strategy program - `initialize_strategy` — creates the Strategy PDA, share mint, and vault ATAs for USDC/TSLAx/NVDAx; registers Pyth price feed pubkeys - `deposit` — accepts USDC, prices shares against Pyth-computed NAV, mints proportional LP shares to the depositor - `invest` — manager-only CPI to the swap router to convert USDC into basket tokens (TSLAx or NVDAx) - `rebalance` — manager-only atomic two-leg CPI: sell one basket token for USDC then buy the other, with slippage protection on both legs - `collect_fees` — permissionless; accrues the annual management fee by minting new shares to the manager (dilution model) - `withdraw` — burns shares, returns proportional in-kind slice of every vault asset (USDC + TSLAx + NVDAx) to the user ### mock-swap-router program (test fixture) - Stores per-asset `usdc_per_token` exchange rates in `AssetRate` PDAs - `swap_usdc_for_asset` — receives USDC, mints basket tokens (used by invest) - `swap_asset_for_usdc` — burns basket tokens, releases USDC (used by rebalance) - `router_authority` PDA holds mint authority for all basket token mints ### Oracle (Pyth) NAV is computed from Pyth `PriceUpdateV2` accounts. Feed pubkeys are stored in the Strategy at creation. Prices older than 60 seconds are rejected. Raw byte offsets are used to read price data, avoiding the borsh 0.10/1.x incompatibility between the Pyth SDK and Anchor 1.0. ### Financial math No floats; u128 intermediates; multiply-before-divide; all arithmetic via checked_* methods; transfer_checked for all token moves. ### Tests (LiteSVM, 8 tests) Covers all instructions including NAV-based deposit pricing, fee accrual via mock clock advancement, in-kind withdrawal, rebalance, and slippage rejection. Mock Pyth PriceUpdateV2 accounts injected directly into LiteSVM. ### Also adds - defi/ workspace members in root Cargo.toml - Vault Strategy entry in root README.md https://claude.ai/code/session_01XUvydGu8rPSbM1AxLJ2GpA
1 parent 5540080 commit 691f96b

33 files changed

Lines changed: 3028 additions & 0 deletions

Cargo.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ members = [
4848
"basics/transfer-sol/anchor/programs/*",
4949
"basics/transfer-sol/asm",
5050

51+
# defi
52+
"defi/vault-strategy/anchor/programs/vault-strategy",
53+
"defi/vault-strategy/anchor/programs/mock-swap-router",
54+
5155
# tokens
5256
"tokens/token-extensions/mint-close-authority/native/program",
5357
"tokens/token-extensions/non-transferable/native/program",

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ Read offchain price data [onchain](https://solana.com/docs/terminology#onchain)
4141

4242
[⚓ Anchor](./oracles/pyth/anchor) [💫 Quasar](./oracles/pyth/quasar)
4343

44+
### Vault Strategy
45+
46+
Manager-run investment vault — deposit USDC, manager allocates across a basket of assets (TSLAx, NVDAx), withdraw proportional in-kind assets. Demonstrates share minting, NAV-based pricing, management fee accrual, and CPI to a swap router.
47+
48+
[⚓ Anchor](./defi/vault-strategy/anchor)
49+
4450
## Basics
4551

4652
### Hello Solana
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.anchor
2+
.DS_Store
3+
target
4+
**/*.rs.bk
5+
node_modules
6+
test-ledger
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[toolchain]
2+
solana_version = "3.1.8"
3+
4+
[features]
5+
resolution = true
6+
skip-lint = false
7+
8+
[programs.localnet]
9+
vault_strategy = "VLT5W7bqhRN4nCdRpXm8UfHRxZd9EuZGqiSAkGHQfGh"
10+
mock_swap_router = "SWPR8Rk3aq3DrDGLdaANq7xCMnXoUFUJWJJmCWxc8Jm"
11+
12+
[provider]
13+
cluster = "Localnet"
14+
wallet = "~/.config/solana/id.json"
15+
16+
[scripts]
17+
test = "cargo test"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
resolver = "2"
6+
7+
[profile.release]
8+
overflow-checks = true
9+
lto = "fat"
10+
codegen-units = 1
11+
12+
[profile.release.build-override]
13+
opt-level = 3
14+
incremental = false
15+
codegen-units = 1

0 commit comments

Comments
 (0)