> For the complete documentation index, see [llms.txt](https://cooking-city-launchpad.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cooking-city-launchpad.gitbook.io/docs/api-documentation/token-trading.md).

# Token Trading

### Trading Process

#### 1. Get Quotes

**Jupiter Quote**

```javascript
const quoteParams = {
  inputMint: isBuy
    ? "So11111111111111111111111111111111111111112"
    : tokenAddress,
  outputMint: isBuy
    ? tokenAddress
    : "So11111111111111111111111111111111111111112",
  amount: amountInLamports,
  slippageBps: 500, // 5% slippage
};
```

**Meteora Quote**

```javascript
const client = new DynamicBondingCurveClient(connection);
const quote = await client.pool.swapQuote({
  virtualPool: poolState,
  config: configState,
  swapBaseForQuote: !isBuy,
  amountIn: new BN(amount),
  slippageBps: 500,
});
```

#### 2. Execute Trade

**Buy Tokens**

**Parameters**:

```javascript
const swapParams = {
  provider: walletProvider,
  userPublicKey: "user_address",
  tokenAddress: "token_address",
  amount: 1.0, // SOL amount
  isBuy: true,
  slippage: 0.05, // 5% slippage
};
```

**Sell Tokens**

**Parameters**:

```javascript
const swapParams = {
  provider: walletProvider,
  userPublicKey: "user_address",
  tokenAddress: "token_address",
  amount: 1000000, // token amount
  isBuy: false,
  slippage: 0.05,
};
```

### Routing Logic

#### Internal Trading (Meteora DLMM)

* Token not graduated (status !== "graduated")
* Not migrated to Meteora (no migrateAmmId)
* Uses Dynamic Bonding Curve

#### External Trading (Jupiter)

* Token graduated (status === "graduated")
* Migrated to Meteora (has migrateAmmId)
* Aggregates optimal prices from multiple DEXs

### Balance Queries

#### SOL Balance

**Function**: Query user SOL balance

```javascript
const solBalance = await getSolBalance(userPublicKey);
```

#### Token Balance

**Function**: Query user specific token balance

```javascript
const tokenBalance = await getTokenPreviousBalance(userPublicKey, tokenAddress);
```

### Transaction Status

#### Transaction Confirmation

```javascript
const confirmation = await connection.confirmTransaction(
  signature,
  "confirmed"
);
if (confirmation.value.err) {
  throw new Error("Transaction failed");
}
```

#### Error Handling

* **Insufficient Balance**: Check SOL and token balances
* **Excessive Slippage**: Adjust slippage settings or get new quotes
* **Network Congestion**: Increase priority fees
* **Pool Does Not Exist**: Confirm token address is correct

### Key Points

* Automatically detects trading path (internal/external)
* Supports custom slippage settings
* Real-time balance queries
* Transaction status monitoring
* Error retry mechanism


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://cooking-city-launchpad.gitbook.io/docs/api-documentation/token-trading.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
