# Panda Pool

Abstract base contract implementing bonding curve mechanics.

#### Functions

**Trading**

**`buyTokens`**

```solidity
function buyTokens(
    uint256 amountIn,
    uint256 minAmountOut,
    address to
) external nonReentrant returns (uint256 amountOut, uint256 fee)
```

Purchase tokens with the base token.

**Parameters:**

* `amountIn`: Amount of base tokens to spend
* `minAmountOut`: Minimum tokens to receive
* `to`: Recipient address

**Returns:**

* `amountOut`: Amount of tokens received
* `fee`: Fee paid in base tokens

**Errors:**

* `PandaPool: INSUFFICIENT_OUTPUT_AMOUNT` - When amountOut is less than minAmountOut
* `PandaPool: TRADE_BELOW_MIN` - When trade size is below minimum threshold
* `PandaPool: INVALID_TO` - When recipient address is zero
* `PandaPool: GRADUATED` - When pool has already graduated

**`buyTokensWithBera`**

```solidity
function buyTokensWithBera(
    uint256 minAmountOut,
    address to
) external payable returns (uint256 amountOut, uint256 fee)
```

Purchase tokens using native BERA.

**Parameters:**

* `minAmountOut`: Minimum tokens to receive
* `to`: Recipient address

**Returns:**

* `amountOut`: Amount of tokens received
* `fee`: Fee paid in BERA

**Errors:**

* `PandaPool: NOT_BERA_PAIR` - When base token is not WBERA
* `PandaPool: INSUFFICIENT_OUTPUT_AMOUNT` - When amountOut is less than minAmountOut
* `PandaPool: INVALID_TO` - When recipient address is zero
* `PandaPool: GRADUATED` - When pool has already graduated

**`buyAllTokens`**

```solidity
function buyAllTokens(
    address to
) external returns (uint256 amountOut, uint256 fee)
```

Purchase all remaining tokens in the pool.

**Parameters:**

* `to`: Recipient address

**Returns:**

* `amountOut`: Amount of tokens received
* `fee`: Fee paid in base tokens

**Errors:**

* `PandaPool: INVALID_TO` - When recipient address is zero
* `PandaPool: INSUFFICIENT_LIQUIDITY` - When not enough liquidity for trade
* `PandaPool: GRADUATED` - When pool has already graduated

**`sellTokens`**

```solidity
function sellTokens(
    uint256 amountIn,
    uint256 minAmountOut,
    address to
) external nonReentrant returns (uint256 amountOut, uint256 fee)
```

Sell tokens for the base token.

**Parameters:**

* `amountIn`: Amount of tokens to sell
* `minAmountOut`: Minimum base tokens to receive
* `to`: Recipient address

**Returns:**

* `amountOut`: Amount of base tokens received
* `fee`: Fee paid in base tokens

**Errors:**

* `PandaPool: INSUFFICIENT_BUYING` - When selling more than bought
* `PandaPool: INSUFFICIENT_OUTPUT_AMOUNT` - When amountOut is less than minAmountOut
* `PandaPool: INVALID_TO` - When recipient address is zero
* `PandaPool: GRADUATED` - When pool has already graduated

**`sellTokensForBera`**

```solidity
function sellTokensForBera(
    uint256 amountIn,
    uint256 minAmountOut,
    address to
) external returns (uint256 amountOut, uint256 fee)
```

Sell tokens for native BERA.

**Parameters:**

* `amountIn`: Amount of tokens to sell
* `minAmountOut`: Minimum amount of BERA to receive
* `to`: Recipient address for BERA

**Returns:**

* `amountOut`: Amount of BERA received
* `fee`: Fee paid in BERA

**Errors:**

* `PandaPool: NOT_BERA_PAIR` - When base token is not WBERA
* `PandaPool: INSUFFICIENT_BUYING` - When selling more than bought
* `PandaPool: INSUFFICIENT_OUTPUT_AMOUNT` - When amountOut is less than minAmountOut
* `PandaPool: INVALID_TO` - When recipient address is zero

#### Price Discovery Functions

**`getAmountOutBuy`**

```solidity
function getAmountOutBuy(
    uint256 amountIn
) public view returns (
    uint256 amountOut,
    uint256 fee,
    uint256 sqrtP_new
)
```

Calculate expected output for buying tokens.

**Returns:**

* `amountOut`: Expected amount of tokens to receive
* `fee`: Expected fee in base tokens
* `sqrtP_new`: New square root price after the trade

**Errors:**

* `PandaPool: TRADE_BELOW_MIN` - When trade size is below minimum threshold
* `PandaPool: INSUFFICIENT_LIQUIDITY` - When not enough liquidity for trade
* `PandaPool: GRADUATED` - When pool has already graduated

**`getAmountOutSell`**

```solidity
function getAmountOutSell(
    uint256 amountIn
) public view returns (
    uint256 amountOut,
    uint256 fee,
    uint256 sqrtP_new
)
```

Calculate expected output for selling tokens.

**Parameters:**

* `amountIn`: Amount of tokens to sell

**Returns:**

* `amountOut`: Expected amount of base tokens to receive
* `fee`: Expected fee in base tokens
* `sqrtP_new`: New square root price after the trade

**Errors:**

* `PandaPool: TRADE_BELOW_MIN` - When trade size is below minimum threshold
* `PandaPool: INSUFFICIENT_LIQUIDITY` - When not enough liquidity for trade
* `PandaPool: GRADUATED` - When pool has already graduated

**`getAmountInBuy`**

```solidity
function getAmountInBuy(
    uint256 amountOut
) public view returns (
    uint256 amountIn,
    uint256 fee,
    uint256 sqrtP_new
)
```

Calculate required input for desired token output.

**Parameters:**

* `amountOut`: Desired amount of tokens to receive

**Returns:**

* `amountIn`: Required amount of base tokens to spend
* `fee`: Expected fee in base tokens
* `sqrtP_new`: New square root price after the trade

**Errors:**

* `PandaPool: TRADE_BELOW_MIN` - When trade size is below minimum threshold
* `PandaPool: INSUFFICIENT_LIQUIDITY` - When not enough tokens available
* `PandaPool: GRADUATED` - When pool has already graduated

**`getAmountInBuyRemainingTokens`**

```solidity
getAmountInBuyRemainingTokens() 
public view returns (uint256 amountIn)
```

Calculate amount needed to buy all remaining tokens.

**Returns:**

* `amountIn`: Amount of base tokens needed to buy all remaining tokens

#### Balance & Vesting Functions

**`totalBalanceOf`**

```solidity
totalBalanceOf(
    address user
) external view returns (uint256)
```

Get total balance including unvested tokens.

**Parameters:**

* `user`: Address to check balance for

**Returns:**

* Total balance including both vested and unvested tokens

**`vestedBalanceOf`**

```solidity
 vestedBalanceOf(
    address user
) external view returns (uint256)
```

Get available/vested balance.

**Parameters:**

* `user`: Address to check vested balance for

**Returns:**

* Currently vested token balance

**`claimableTokens`**

```solidity
claimableTokens(
    address user
) public view returns (uint256)
```

Get amount of tokens available to claim.

**Parameters:**

* `user`: Address to check claimable tokens for

**Returns:**

* Amount of tokens currently available to claim

**Errors:**

* `PandaPool: VESTING_OFF` - When vesting is not enabled
* `PandaPool: NOT_GRADUATED` - When pool hasn't graduated yet

**`claimTokens`**

```solidity
claimTokens(
    address user
) external returns (uint256)
```

Claim vested tokens.

**Parameters:**

* `user`: Address to claim tokens for

**Returns:**

* Amount of tokens claimed

**Errors:**

* `PandaPool: VESTING_OFF` - When vesting is not enabled
* `PandaPool: NO_CLAIMABLE` - When no tokens are available to claim

#### Pool State Functions

**`remainingTokensInPool`**

```solidity
remainingTokensInPool()
public view returns (uint256)
```

Get remaining tokens available in pool.

**Returns:**

* Amount of tokens remaining in the pool

**`viewExcessTokens`**

```solidity
viewExcessTokens()
public view returns (
    uint256 excessPandaTokens,
    uint256 excessBaseTokens
)
```

View excess tokens in contract.

**Returns:**

* `excessPandaTokens`: Amount of excess Panda tokens
* `excessBaseTokens`: Amount of excess base tokens

**`collectExcessTokens`**

```solidity
collectExcessTokens()
external
```

Transfer excess tokens to treasury.

**Emits:** `ExcessCollected`

#### Events

```solidity
PoolInitialized(
    address pandaToken,
    address baseToken,
    uint256 sqrtPa,
    uint256 sqrtPb,
    uint256 vestingPeriod,
    address deployer,
    bytes data
)
```

Emitted when pool is initialized.

```solidity
Swap(
    address indexed sender,
    uint amount0In,
    uint amount1In,
    uint amount0Out,
    uint amount1Out,
    address indexed to
)
```

Emitted on each swap operation.

```solidity
Sync(
    uint256 pandaReserve,
    uint256 baseReserve,
    uint256 sqrtPrice
)
```

Emitted when pool reserves are synchronized.

```solidity
TokensClaimed(
    address indexed user,
    uint256 amount
)
```

Emitted when tokens are claimed from vesting.

```solidity
ExcessCollected(
    uint256 excessPandaTokens,
    uint256 excessBaseTokens
)
```

Emitted when excess tokens are collected.


---

# 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://documentation.kodiak.finance/developers/panda/smart-contract-reference/panda-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.
