Skip to content

Commit 912511f

Browse files
author
Edward (Mike's bot)
committed
refactor(token-swap): split state.rs into state/ directory
Match the convention used by the majority of non-trivial Anchor examples in this repo (escrow, anchor-program-example, carnival, close-account, cutils, etc.): one struct per file under a state/ module, with mod.rs re-exporting the contents. state/mod.rs - re-exports Config and PoolConfig state/config.rs - Config struct (program-level singleton) state/pool_config.rs - PoolConfig struct (per-pool identity record) Pure structural refactor. No behaviour, field, or doc changes. All downstream imports (`use crate::state::{Config, PoolConfig}`) continue to work via the glob re-export. Build clean, 18/18 integration tests pass, no new warnings.
1 parent a7d3212 commit 912511f

3 files changed

Lines changed: 39 additions & 33 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use anchor_lang::prelude::*;
2+
3+
/// Shared configuration for the AMM (admin + trading fee).
4+
///
5+
/// `Config` is a singleton: one account per deployed program, seeded by the
6+
/// fixed byte string `b"config"`. This mirrors how real DEXs are deployed in
7+
/// practice (e.g. Phoenix and Raydium ship one program per market/AMM, so the
8+
/// program-level config is global by construction). Parameterising the config
9+
/// by an `id` was leftover complexity from the original example; removing it
10+
/// makes the on-chain layout simpler and matches realistic deployment.
11+
#[account]
12+
#[derive(Default, InitSpace)]
13+
pub struct Config {
14+
/// Account that has admin authority over the AMM.
15+
pub admin: Pubkey,
16+
17+
/// The trading fee taken on each swap, in basis points (out of 10_000).
18+
///
19+
/// This is the *total* fee charged on a swap. It is split between LPs and
20+
/// the admin according to `admin_share_bps`.
21+
pub fee: u16,
22+
23+
/// Fraction of the trading fee that goes to the admin, in basis points
24+
/// (out of 10_000). The remainder goes to LPs (it stays in the pool
25+
/// reserves and grows the LP-claimable balance).
26+
///
27+
/// Modelled on Uniswap V2 / Raydium: the AMM operator takes a slice of
28+
/// every fee, LPs keep the rest. Set in `create_config`; fixed for the
29+
/// lifetime of the program. Must be `< 10_000`.
30+
pub admin_share_bps: u16,
31+
32+
/// Canonical bump for this PDA.
33+
pub bump: u8,
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod config;
2+
pub mod pool_config;
3+
4+
pub use config::*;
5+
pub use pool_config::*;

tokens/token-swap/anchor/programs/token-swap/src/state.rs renamed to tokens/token-swap/anchor/programs/token-swap/src/state/pool_config.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,5 @@
11
use anchor_lang::prelude::*;
22

3-
/// Shared configuration for the AMM (admin + trading fee).
4-
///
5-
/// `Config` is a singleton: one account per deployed program, seeded by the
6-
/// fixed byte string `b"config"`. This mirrors how real DEXs are deployed in
7-
/// practice (e.g. Phoenix and Raydium ship one program per market/AMM, so the
8-
/// program-level config is global by construction). Parameterising the config
9-
/// by an `id` was leftover complexity from the original example; removing it
10-
/// makes the on-chain layout simpler and matches realistic deployment.
11-
#[account]
12-
#[derive(Default, InitSpace)]
13-
pub struct Config {
14-
/// Account that has admin authority over the AMM.
15-
pub admin: Pubkey,
16-
17-
/// The trading fee taken on each swap, in basis points (out of 10_000).
18-
///
19-
/// This is the *total* fee charged on a swap. It is split between LPs and
20-
/// the admin according to `admin_share_bps`.
21-
pub fee: u16,
22-
23-
/// Fraction of the trading fee that goes to the admin, in basis points
24-
/// (out of 10_000). The remainder goes to LPs (it stays in the pool
25-
/// reserves and grows the LP-claimable balance).
26-
///
27-
/// Modelled on Uniswap V2 / Raydium: the AMM operator takes a slice of
28-
/// every fee, LPs keep the rest. Set in `create_config`; fixed for the
29-
/// lifetime of the program. Must be `< 10_000`.
30-
pub admin_share_bps: u16,
31-
32-
/// Canonical bump for this PDA.
33-
pub bump: u8,
34-
}
35-
363
/// Per-pool configuration / identity record.
374
///
385
/// Holds the metadata that identifies a single pool: which `Config` it belongs

0 commit comments

Comments
 (0)