# Deposit

Adds liquidity to a pool on the CoolDEX.

## Instruction

```rust
DepositInstruction {
    max_coin_amount: u64,
    max_pc_amount: u64,
    base_side: u64,
    other_amount_min: Option<u64>,
}
```

### Parameters

| Parameter          | Type          | Description                                                              |
| ------------------ | ------------- | ------------------------------------------------------------------------ |
| `max_coin_amount`  | `u64`         | Maximum amount of coin token to deposit                                  |
| `max_pc_amount`    | `u64`         | Maximum amount of PC token to deposit                                    |
| `base_side`        | `u64`         | Indicates which token is the base for the deposit (0 for coin, 1 for PC) |
| `other_amount_min` | `Option<u64>` | Minimum acceptable amount of the other token (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 LP mint      | LP token mint                    | `[program.toBuffer(), marketId.toBuffer(), "lp_mint_associated_seed"]`    |
| 5  | AMM coin vault   | Token vault                      | `[program.toBuffer(), marketId.toBuffer(), "coin_vault_associated_seed"]` |
| 6  | AMM PC vault     | SOL vault                        | `[program.toBuffer(), marketId.toBuffer(), "pc_vault_associated_seed"]`   |
| 7  | User source coin | User's token account             | `getAssociatedTokenAddress(tokenMint, userWallet, false)`                 |
| 8  | User source PC   | User's SOL account               | `getAssociatedTokenAddress(NATIVE_MINT, userWallet, false)`               |
| 9  | User dest LP     | User's LP token account          | `getAssociatedTokenAddress(lpMint, userWallet, false)`                    |
| 10 | Source owner     | Transaction signer (User wallet) | -                                                                         |

### Function Logic

1. Verifies the transaction is signed by the source owner
2. Checks that the AMM is in a valid state for deposits
3. Verifies that the deposit amounts are not zero
4. Calculates the deposit amounts based on the current pool ratio:
   * If base\_side is 0 (coin): Calculates PC amount based on provided coin amount
   * If base\_side is 1 (PC): Calculates coin amount based on provided PC amount
5. Verifies amounts are within specified limits and match minimum requirements
6. Calculates LP tokens to mint based on the proportion of assets being added
7. Transfers coin and PC tokens from user to respective vaults
8. Mints LP tokens to the user
9. Updates the AMM's LP token supply

### LP Token Calculation

The amount of LP tokens minted is calculated using the proportion of assets being added to the pool:

For base\_side = 0 (coin base):

```
mint_lp_amount = (deduct_coin_amount / total_coin_without_take_pnl) * amm.lp_amount
```

For base\_side = 1 (PC base):

```
mint_lp_amount = (deduct_pc_amount / total_pc_without_take_pnl) * amm.lp_amount
```

Where:

* `total_coin_without_take_pnl` is the total coin amount in the pool
* `total_pc_without_take_pnl` is the total PC amount in the pool
* `amm.lp_amount` is the current total supply of LP tokens

## Notes

* Deposits must maintain the current ratio of assets in the pool
* Using base\_side allows specifying which token amount is fixed
* The other token amount is calculated to maintain the pool ratio
* LP tokens represent a proportional share of the pool
* Slippage protection can be used to set minimum acceptable amounts

### Example Usage

```javascript
// Derive AMM accounts (similar to swap example)
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 [lpMint] = PublicKey.findProgramAddressSync(
  [
    COOLDEX_PROGRAM_ID.toBuffer(),
    marketId.toBuffer(),
    Buffer.from("lp_mint_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
)

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

// Create instruction data
const data = Buffer.alloc(25)
data.writeUInt8(3, 0) // Deposit instruction
data.writeBigUInt64LE(maxCoinAmount, 1) // Max coin amount
data.writeBigUInt64LE(maxPcAmount, 9) // Max PC amount
data.writeBigUInt64LE(baseSide, 17) // Base side (0=coin, 1=PC)
// Include otherAmountMin if provided

const depositIx = 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: lpMint, isSigner: false, isWritable: true },
    { pubkey: ammCoinVault, isSigner: false, isWritable: true },
    { pubkey: ammPcVault, isSigner: false, isWritable: true },
    { pubkey: userTokenAccount, isSigner: false, isWritable: true },
    { pubkey: userWsolAccount, isSigner: false, isWritable: true },
    { pubkey: userLpAccount, isSigner: false, isWritable: true },
    { pubkey: wallet.publicKey, isSigner: true, isWritable: false },
  ],
  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/deposit.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.
