sell
Swaps base → quote in a constant-product (x·y = k) pool. Supports exact-in and exact-out with slippage protection. Protocol (and optional creator) fees are taken in quote tokens and routed to their destinations. Emits a structured trade log (log_trade_internal
) for indexers.
Arguments
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct SellArgs {
/// Base tokens to sell (exact-in) or max-in (exact-out)
pub base_amount: u64,
/// Minimum quote tokens to receive (exact-in) or exact quote out (exact-out)
pub quote_out: u64,
/// true => exact-in, false => exact-out
pub is_exact_in: bool,
}
Semantics
Exact-in (
is_exact_in = true
): sell exactlybase_amount
; must receive ≥quote_out
quote tokens (after fees).Exact-out (
is_exact_in = false
): receive exactlyquote_out
quote tokens (after fees); must sell ≤base_amount
base tokens.
Required Accounts
Fee recipient must be one of the allowlisted
FEE_ACCOUNTS
constants defined on-chain.
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 be in 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
17
token_creator_fee_ata
✅
ATA(quote_mint, token_creator)
; may be created if missing
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