# Initialize Pool

Creates a new liquidity pool on the CoolDEX.

## Instruction

```rust
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

```javascript
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

```javascript
// 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,
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.something.cool/cooldex/functions/initialize-pool.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
