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
  • Function Logic
  • Notes
  • Helper Function for Computing CoolDEX Addresses
  • Example Usage
  1. CoolDEX
  2. Functions

Initialize Pool

Creates a new liquidity pool on the CoolDEX.

Instruction

InitializeInstruction2 {
    nonce: u8,
    open_time: u64,
    init_pc_amount: u64,
    init_coin_amount: u64,
    fee_numerator: u16,
    burn_bp: u16,
}

Parameters

Parameter
Type
Description

nonce

u8

Nonce for PDAs

open_time

u64

Timestamp when the pool opens for trading

init_pc_amount

u64

Initial amount of quote token (typically SOL)

init_coin_amount

u64

Initial amount of base token

fee_numerator

u16

Fee numerator for the pool

burn_bp

u16

Portion of fees allocated to burning (in basis points)

Account Setup

#
Account
Description
Derivation

1

Token program

SPL Token program

TOKEN_PROGRAM_ID

2

Associated token account program

ATA program

ASSOCIATED_TOKEN_PROGRAM_ID

3

System program

System program

SystemProgram.programId

4

Rent sysvar

Rent program

SYSVAR_RENT_PUBKEY

5

AMM info

AMM account

coolDexSeed("amm_associated_seed")

6

AMM authority

AMM authority PDA

[Buffer.from("amm authority")]

7

AMM LP mint

LP token mint

coolDexSeed("lp_mint_associated_seed")

8

AMM coin mint

Coin token mint

Provided by user

9

AMM PC mint

Quote token mint (SOL)

NATIVE_MINT

10

AMM coin vault

Coin token vault

coolDexSeed("coin_vault_associated_seed")

11

AMM PC vault

Quote token (SOL) vault

coolDexSeed("pc_vault_associated_seed")

12

AMM config

AMM configuration account

[Buffer.from("amm_config_account_seed")]

13

Create fee destination

Platform fee account

Fixed address

14

Market info

Market account

Provided by user

15

User wallet

Transaction signer

User's wallet

16

User token coin

User's coin token account

getAssociatedTokenAddress(coinMint, userWallet, false)

17

User token PC

User's quote token (SOL) account

getAssociatedTokenAddress(NATIVE_MINT, userWallet, false)

18

User token LP

User's LP token account

getAssociatedTokenAddress(lpMint, userWallet, false)

Function Logic

  1. Verifies the AMM config account is valid

  2. Verifies the signer is the SC Bonding Curve deployer

  3. Checks that the coin mint is not SOL (native mint)

  4. Checks that the PC mint is SOL (native mint)

  5. Creates target orders account

  6. Creates LP mint account with the same decimals as the coin token

  7. Creates coin vault and PC vault accounts

  8. Creates AMM account

  9. Creates user's associated token account for LP tokens

  10. Transfers initial coin and PC amounts from user to the vaults

  11. Calculates initial liquidity based on the geometric mean of both deposits

  12. Mints LP tokens to the user

  13. Initializes the AMM with specified parameters

  14. Sets the initial pool state and parameters

Notes

  • Only the SC Bonding Curve deployer can initialize pools

  • The PC (quote) token must be the native SOL token

  • The coin (base) token cannot be the native SOL token

  • Initial LP tokens are minted to the user wallet minus a minimum amount kept in the pool

  • The pool status is determined by the open_time parameter:

    • If open_time > current time: WaitingTrade status

    • Otherwise: SwapOnly status

Helper Function for Computing CoolDEX Addresses

function computeCoolDexAddresses(marketId) {
  const coolDexSeed = (suffix) =>
    PublicKey.findProgramAddressSync(
      [COOLDEX_PROGRAM_ID.toBuffer(), marketId.toBuffer(), Buffer.from(suffix)],
      COOLDEX_PROGRAM_ID
    )[0]
  return {
    ammId: coolDexSeed("amm_associated_seed"),
    poolCoinAccount: coolDexSeed("coin_vault_associated_seed"),
    poolQuoteAccount: coolDexSeed("pc_vault_associated_seed"),
    lpMint: coolDexSeed("lp_mint_associated_seed"),
  }
}

Example Usage

// Generate a new market ID
const marketId = Keypair.generate().publicKey

// Derive all AMM accounts using the helper function
const {
  ammId,
  poolCoinAccount,
  poolQuoteAccount,
  lpMint,
  targetOrdersAccount,
  ammOpenOrders,
} = computeCoolDexAddresses(marketId)

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

// Derive AMM config
const [ammConfig] = PublicKey.findProgramAddressSync(
  [Buffer.from("amm_config_account_seed")],
  COOLDEX_PROGRAM_ID
)

// User token accounts
const userCoinAccount = getAssociatedTokenAddressSync(coinMint, wallet.publicKey, false)

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

const userLpAccount = getAssociatedTokenAddressSync(lpMint, wallet.publicKey, false)

// Create instruction data
const data = Buffer.alloc(30)
data.writeUInt8(16, 0) // Initialize2 instruction
data.writeUInt8(nonce, 1) // Nonce
data.writeBigUInt64LE(BigInt(openTime), 2) // Open time
data.writeBigUInt64LE(BigInt(initPcAmount), 10) // Initial PC amount
data.writeBigUInt64LE(BigInt(initCoinAmount), 18) // Initial coin amount
data.writeUInt16LE(feeNumerator, 26) // Fee numerator
data.writeUInt16LE(burnBp, 28) // Burn basis points

// Create transaction instruction with all accounts
const initialize2Ix = new TransactionInstruction({
  programId: COOLDEX_PROGRAM_ID,
  keys: [
    { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
    { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
    { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
    { pubkey: ammId, isSigner: false, isWritable: true },
    { pubkey: ammAuthority, isSigner: false, isWritable: false },
    { pubkey: lpMint, isSigner: false, isWritable: true },
    { pubkey: coinMint, isSigner: false, isWritable: false },
    { pubkey: NATIVE_MINT, isSigner: false, isWritable: false },
    { pubkey: poolCoinAccount, isSigner: false, isWritable: true },
    { pubkey: poolQuoteAccount, isSigner: false, isWritable: true },
    { pubkey: ammConfig, isSigner: false, isWritable: false },
    { pubkey: AMM_FEE_DESTINATION, isSigner: false, isWritable: true },
    { pubkey: marketId, isSigner: false, isWritable: true },
    { pubkey: wallet.publicKey, isSigner: true, isWritable: true },
    { pubkey: userCoinAccount, isSigner: false, isWritable: true },
    { pubkey: userWsolAccount, isSigner: false, isWritable: true },
    { pubkey: userLpAccount, isSigner: false, isWritable: true },
  ],
  data: data,
})
PreviousFunctionsNextSwap Base In

Last updated 3 days ago