# Developer Manual && API

## How to create a pool in Koichi Swap

### Constant Product Pool

1. Go to Liquidity Tag.
2. Choose the token pair you would like to create.
3. Click `Confirm Create Pool` and wait for the transaction to be confirmed.

![](https://698317248-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7SZTEqNHhBL2YlmFgvka%2Fuploads%2FI5ZeEDnidwrQYCWkqZym%2Fimage.png?alt=media\&token=2ee84159-2138-4bd8-9434-5e1c48bf727a)

###

### Stable Pool and Meta Stable Pool

Please [contact](https://koichiswap.gitbook.io/koichiswap/contact) the development team. The stable pool must be permissioned due to adversaries may wrongly use the "curve" function to keep their token price.

## How to add a new token to the token list

1. Go to [Koichi Swap Tokenlists](https://github.com/Koichi-Swap/koichi-swap-tokenlists) in Github.
2. Create an issue with the following info:
   * Chain ID.
   * Token address in the corresponding chain.
   * Token logo 200x200 PNG.

An example:

```
{
    "chainId": 1029,
    "address": "cfx:acg158kvr8zanb1bs048ryb6rtrhr283ma70vz70tx",
    "name": "Wrapped Conflux",
    "symbol": "WCFX",
    "decimals": 18,
    "logoURI": "https://raw.githubusercontent.com/Koichi-Swap/koichi-swap-tokenlists/main/logos/cfx-acg158kvr8zanb1bs048ryb6rtrhr283ma70vz70tx/logo.png"
}
```

## Access API/ABI and examples

### Koichi Swap Router

#### ABI

<https://confluxscan.io/address/cfx:acb4uke6jwp09rmxwc4e5znzgbads099b63v41830v>

#### Interface

```
interface IKoichiSwap {
   enum PoolSpecialization {
    CONSTANT_PRODUCT,
    STABLE_SWAP,
    META_STABLE_SWAP,
    EXTERNAL_CONSTANT_PRODUCT,
    EXTERNAL_STABLE_SWAP,
    EXTERNAL_META_STABLE_SWAP
  }

  function createPool(
    address factory,
    address[] memory tokens,
    bytes memory userData
  ) external returns (bytes32 poolId);

  function addLiquidity(
    bytes32 poolId,
    address recipient,
    address[] memory tokens,
    uint256[] memory amounts,
    bytes memory userData,
    uint256 deadline
  ) external payable returns (uint256);

  function removeLiquidity(
    bytes32 poolId,
    address recipient,
    address[] memory tokens,
    bytes memory userData,
    uint256 deadline
  ) external returns (uint256[] memory);

  function querySwap(
    uint256 amountIn,
    bytes32[] memory pools,
    address[] memory paths
  ) external view returns (uint256);

  function swap(
    uint256 amountIn,
    uint256 limit,
    bytes32[] memory pools,
    address[] memory paths,
    address recipient,
    uint256 deadline
  ) external payable returns (uint256);

  struct BatchSwapStep {
    bytes32 poolId;
    bytes32 nextPoolId;
    uint112 amount;
    uint112 limit;
    uint8 indexIn;
    uint8 indexOut;
  }

  function batchSwap(
    BatchSwapStep[] memory swaps,
    address[] memory tokens,
    address recipient,
    uint256 deadline
  ) external payable returns (uint256[] memory);
}
```

### Koichi Swap Base Pool

#### Interface

```
interface IPool {
  function totalTokens() external view returns (uint256);

  function tokens(uint256 index) external view returns (address);

  function reserves(uint256 index) external view returns (uint256);

  function querysSwap(
    address tokenIn,
    address tokenOut,
    uint256 amountIn
  ) external view returns (uint256 amountOut);

  function onAddLiquidity(address recipient, bytes memory userData) external returns (uint256 liquidity);

  function onRemoveLiquidity(
    address sender,
    address recipient,
    bytes memory userData
  ) external returns (uint256[] memory amounts);

  function onSwap(
    address tokenIn,
    address tokenOut,
    address recipient
  ) external returns (uint256 amountOut);
}
```

### Constant Product Pool Factory

#### ABI

<https://confluxscan.io/address/cfx:acacwu53up60gg2jtjpn01u07sdh94aksakk9bb8xf>

#### Interface

```
interface IConstantProductPoolFactory {
  function getPool(address tokenA, address tokenB) external view returns (bytes32);
  
  function allPools(uint256 index) external view returns (bytes32);
  
  function poolLength() external view returns (uint256);
}
```

### Constant Product Pool

#### ABI

<https://confluxscan.io/address/cfx:achnazus63vuj1jdfkk4e07vgvsxxsp1eyztdug6e2>

#### Interface

```
interface IConstantProductPool is IPool {
  function getTradeInfo() external view returns (uint112 _reserve0, uint112 _reserve1, uint32 _swapFeePercentage);
}
```

#### Examples

1. query token price from constant product pool

```
(uint256 reserve0, uint256 reserve1, ) = IConstantProductPool(pool).getTradeInfo();
uint256 price0 = reserve1.mul(1e18).div(reserve0);
uint256 price1 = reserve0.mul(1e18).div(reserve1);
```

### Stable Pool Factory

#### ABI

<https://confluxscan.io/address/cfx:acdt6a8stb9p4teku9n8wsrxc04g055892p5jk18kz>

#### Interface

```
interface IStablePoolFactory {
  // bytes32 poolHash = keccak256(abi.encode(tokens));
  function getPool(bytes32 poolHash) external view returns (bytes32);
  
  function allPools(uint256 index) external view returns (bytes32);
  
  function poolLength() external view returns (uint256);
}
```

### Stable Pool

#### ABI

<https://confluxscan.io/address/cfx:achtcc8vpzw34krmmv7ev14dvapfv5gxey8z1p1hys>

#### Interface

```
interface IStablePool is IPool {
  function scales(uint256 index) external view returns (uint256);
  
  function swapFeePercentage() external view returns (uint256);
  
  function getA() external view returns (uint256);
  
  function getAPrecise() external view returns (uint256);
  
  function getVirtualPrice() external view returns (uint256);
}
```
