Something.cool
  • Getting started
    • Why Something.cool?
    • 1st stage - Bonding curve
    • 2nd stage - CoolDEX trading
  • Zero-fee coins
    • What is Zero-fee coin?
    • How to launch Zero-fee coin
  • Community coins
    • What is Community coin?
    • Community contribution / How it works?
      • Holders
      • Liquidity providers
      • Burning
    • How to launch Сommunity coin
  • Features
    • Coin Explore
      • New Pairs
      • Coin Vision
    • Spaces
    • Rewards
  • Community
    • Referral program
    • X
    • Media kit
  • Developer Resources
  • Platform Architecture
    • Technical Introduction
    • Overview
    • Components
  • SC Bonding Curve
    • Overview
    • Functions
      • Create Token
      • Buy Token
      • Sell Token
      • Migrate
      • Admin Set Properties
    • Price Calculation
    • Fees
  • CoolDEX
    • Overview
    • Functions
      • Initialize Pool
      • Swap Base In
      • Swap Base Out
      • Deposit
      • Withdraw
      • Withdraw PnL
    • Price Calculation
    • Fees
  • Token Types
    • Token Types
  • Migration Process
    • Migration Process
  • Rewards Claiming
    • Overview
    • Authentication
    • Endpoints
    • Errors
    • Example
  • Reference
    • SC Bonding Curve IDL
    • CoolDEX IDL
    • Log Structure
    • DEVNET EXAMPLES
  • INTERNAL API
    • Overview
    • Register
  • Example: Registering a User via Internal API (JavaScript)
Powered by GitBook
On this page
  • Instruction
  • Parameters
  • Account Setup
  • Swap Direction Setup
  • Function Logic
  • Direction-Specific Logic
  • Coin to PC (Sell)
  • PC to Coin (Buy)
  • Notes
  • Example Usage
  1. CoolDEX
  2. Functions

Swap Base In

Swaps tokens by specifying the input amount.

Instruction

SwapInstructionBaseIn {
    amount_in: u64,
    minimum_amount_out: u64,
}

Parameters

Parameter
Type
Description

amount_in

u64

Amount of input token to swap

minimum_amount_out

u64

Minimum acceptable output token amount (slippage protection)

Account Setup

#
Account
Description
Derivation

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

  1. Verifies the transaction is signed by the token owner

  2. Checks that the AMM is in a valid state for swapping

  3. Determines swap direction (Coin2PC or PC2Coin) based on input and output token accounts

  4. Calculates fee based on token type (SC Bonding Curve or non-SC Bonding Curve)

  5. For SC Bonding Curve tokens, calculates fee distribution among:

    • Platform fee

    • Community contribution (community fund)

    • Token burning

  6. Calculates output amount based on constant product formula

  7. Verifies output amount meets minimum specified (slippage check)

  8. Executes the swap with appropriate fee handling based on token type and swap direction

  9. Updates AMM state data with swap information

Direction-Specific Logic

Coin to PC (Sell)

When selling tokens for SOL:

  1. Burns tokens if token is a SC Bonding Curve token with burn rate

  2. Transfers portion of tokens to Community contribution account if applicable

  3. Deposits remaining tokens to the AMM coin vault

  4. Transfers platform fee in SOL to platform tax account if applicable

  5. Transfers Community contribution in SOL if applicable

  6. Transfers remaining SOL to the user destination account

PC to Coin (Buy)

When buying tokens with SOL:

  1. Transfers platform fee in SOL to platform tax account if applicable

  2. Transfers Community contribution in SOL if applicable

  3. Deposits remaining SOL to the AMM PC vault

  4. Burns tokens if token is a SC Bonding Curve token with burn rate

  5. Transfers portion of tokens to Community contribution account if applicable

  6. 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

// Find market ID (usually available after migration)
// const marketId = new PublicKey("...");

// Derive AMM accounts
const [ammAuthority] = PublicKey.findProgramAddressSync(
  [Buffer.from("amm authority")],
  COOLDEX_PROGRAM_ID
)

const [ammId] = PublicKey.findProgramAddressSync(
  [
    COOLDEX_PROGRAM_ID.toBuffer(),
    marketId.toBuffer(),
    Buffer.from("amm_associated_seed"),
  ],
  COOLDEX_PROGRAM_ID
)

const [ammCoinVault] = PublicKey.findProgramAddressSync(
  [
    COOLDEX_PROGRAM_ID.toBuffer(),
    marketId.toBuffer(),
    Buffer.from("coin_vault_associated_seed"),
  ],
  COOLDEX_PROGRAM_ID
)

const [ammPcVault] = PublicKey.findProgramAddressSync(
  [
    COOLDEX_PROGRAM_ID.toBuffer(),
    marketId.toBuffer(),
    Buffer.from("pc_vault_associated_seed"),
  ],
  COOLDEX_PROGRAM_ID
)

// User token accounts
const userTokenAccount = getAssociatedTokenAddressSync(tokenMint, wallet.publicKey, false)

const userWsolAccount = getAssociatedTokenAddressSync(
  NATIVE_MINT,
  wallet.publicKey,
  false
)

// Community contribution storage accounts
const commContribStorageWsol = await PublicKey.createWithSeed(
  tokenMint,
  "w",
  TOKEN_PROGRAM_ID
)

const commContribStorageToken = await PublicKey.createWithSeed(
  tokenMint,
  "t",
  TOKEN_PROGRAM_ID
)

// Determine source and destination based on swap direction
const isSelling = true // Change based on swap direction
const [userSource, userDestination] = isSelling
  ? [userTokenAccount, userWsolAccount]
  : [userWsolAccount, userTokenAccount]

// Create instruction data
const data = Buffer.alloc(17)
data.writeUInt8(9, 0) // SwapBaseIn instruction
data.writeBigUInt64LE(amountIn, 1) // Input amount
data.writeBigUInt64LE(minimumAmountOut, 9) // Minimum output

const swapBaseInIx = new TransactionInstruction({
  programId: COOLDEX_PROGRAM_ID,
  keys: [
    { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: ammId, isSigner: false, isWritable: true },
    { pubkey: ammAuthority, isSigner: false, isWritable: false },
    { pubkey: ammCoinVault, isSigner: false, isWritable: true },
    { pubkey: ammPcVault, isSigner: false, isWritable: true },
    { pubkey: userSource, isSigner: false, isWritable: true },
    { pubkey: userDestination, isSigner: false, isWritable: true },
    { pubkey: wallet.publicKey, isSigner: true, isWritable: false },
    { pubkey: tokenMint, isSigner: false, isWritable: true },
    { pubkey: AMM_FEE_DESTINATION, isSigner: false, isWritable: true },
    { pubkey: commContribStorageWsol, isSigner: false, isWritable: true },
    { pubkey: commContribStorageToken, isSigner: false, isWritable: true },
  ],
  data: data,
})
PreviousInitialize PoolNextSwap Base Out

Last updated 2 months ago