buy

Swaps quote → base in a constant-product (x·y=k) pool. Supports exact-in and exact-out with slippage protection. Applies protocol (and optional creator) fees in quote tokens and emits a structured trade log (log_trade_internal) for indexers.


Arguments

pub struct BuyArgs {
    /// Quote tokens to spend (exact-in) or max-in (exact-out) — in quote mint’s base units
    pub quote_amount: u64,
    /// Minimum base tokens to receive (exact-in) or exact base out (exact-out)
    pub base_out: u64,
    /// true => exact-in, false => exact-out
    pub is_exact_in: bool,
}

Semantics

  • Exact-in (is_exact_in = true): spend up to quote_amount; must receive base_out base tokens.

  • Exact-out (is_exact_in = false): receive exactly base_out base tokens; must spend quote_amount quote tokens.


Required Accounts

Constants available on-chain:

  • TRADERSDEX_CURVE_PROGRAM, SOL_MINT, MASTER_ADMIN

  • FEE_ACCOUNTS: fixed allowlist of protocol fee recipients

#
Name
Writable
Seeds / Constraints

0

config

["global_config"] (PDA)

1

pool

["pool", base_mint, quote_mint] (PDA); pool.state == 1 (LIVE); pool.base_mint == base_mint; pool.quote_mint == quote_mint

2

base_mint

Owner == SPL Token program

3

quote_mint

Owner == SPL Token program

4

base_vault

["vault", pool, base_mint] (PDA); key == pool.base_vault; owner == pool; mint == base_mint

5

quote_vault

["vault", pool, quote_mint] (PDA); key == pool.quote_vault; owner == pool; mint == quote_mint

6

user

7

user_base_account

mint == base_mint, owner == user

8

user_quote_account

mint == quote_mint, owner == user

9

protocol_fee_recipient

Must equal one of FEE_ACCOUNTS

10

protocol_fee_recipient_token_account

ATA(quote_mint, protocol_fee_recipient)

11

token_program

Must equal SPL Token program ID

12

associated_token_program

13

system_program

14

tradersdex_swap

Must equal this Swap program ID (crate::ID)

15

event_authority

["event_authority"] (PDA)

16

token_creator

Present if pool has creator fees OPTIONAL

17

token_creator_fee_ata

ATA(quote_mint, token_creator); OPTIONAL


Behavior (high level)

  • Pricing: constant-product x·y=k. Larger trades move price more (slippage).

  • Fees (quote side): protocol (and optional creator) fees are taken in quote.

    • protocol_fee_recipient_token_account receives protocol fees.

    • If creator fees apply and token_creator is provided, token_creator_fee_ata receives creator fees (if it is not init then no creator fee).

Last updated