Swap Base In
Swaps tokens by specifying the input amount.
Instruction
Parameters
amount_in
u64
Amount of input token to swap
minimum_amount_out
u64
Minimum acceptable output token amount (slippage protection)
Account Setup
1
Token program
SPL Token program
TOKEN_PROGRAM_ID
2
AMM info
AMM account
[program.toBuffer(), marketId.toBuffer(), "amm_associated_seed"]
3
AMM authority
AMM authority PDA
[Buffer.from("amm authority")]
4
AMM coin vault
Token vault
[program.toBuffer(), marketId.toBuffer(), "coin_vault_associated_seed"]
5
AMM PC vault
SOL vault
[program.toBuffer(), marketId.toBuffer(), "pc_vault_associated_seed"]
6
User source
Source token account
Depends on swap direction
7
User destination
Destination token account
Depends on swap direction
8
Source owner
Transaction signer (User wallet)
-
9
Coin mint
Token mint address
Provided by user
10
Platform tax WSOL account
Platform fee account
Platform WSOL address (see Reference)
11
Comm contribution WSOL account
Community fund WSOL
Associated account for fee destination
12
Comm contribution token account
Community fund token
Associated account for fee destination
Swap Direction Setup
For selling tokens for SOL (Coin2PC):
User source = User's token account:
getAssociatedTokenAddress(tokenMint, userWallet, false)
User destination = User's SOL account:
getAssociatedTokenAddress(NATIVE_MINT, userWallet, false)
For buying tokens with SOL (PC2Coin):
User source = User's SOL account:
getAssociatedTokenAddress(NATIVE_MINT, userWallet, false)
User destination = User's token account:
getAssociatedTokenAddress(tokenMint, userWallet, false)
Function Logic
Verifies the transaction is signed by the token owner
Checks that the AMM is in a valid state for swapping
Determines swap direction (Coin2PC or PC2Coin) based on input and output token accounts
Calculates fee based on token type (SC Bonding Curve or non-SC Bonding Curve)
For SC Bonding Curve tokens, calculates fee distribution among:
Platform fee
Community contribution (community fund)
Token burning
Calculates output amount based on constant product formula
Verifies output amount meets minimum specified (slippage check)
Executes the swap with appropriate fee handling based on token type and swap direction
Updates AMM state data with swap information
Direction-Specific Logic
Coin to PC (Sell)
When selling tokens for SOL:
Burns tokens if token is a SC Bonding Curve token with burn rate
Transfers portion of tokens to Community contribution account if applicable
Deposits remaining tokens to the AMM coin vault
Transfers platform fee in SOL to platform tax account if applicable
Transfers Community contribution in SOL if applicable
Transfers remaining SOL to the user destination account
PC to Coin (Buy)
When buying tokens with SOL:
Transfers platform fee in SOL to platform tax account if applicable
Transfers Community contribution in SOL if applicable
Deposits remaining SOL to the AMM PC vault
Burns tokens if token is a SC Bonding Curve token with burn rate
Transfers portion of tokens to Community contribution account if applicable
Transfers remaining tokens to the user destination account
Notes
Fee calculation differs based on token type and the token's configuration
For SC Bonding Curve tokens, fees are distributed according to the token's specified parameters
Burns are executed immediately during the swap transaction
State data is updated to track swap volumes and accumulate fees
Example Usage
Last updated