npm package: @omnibtc/omniswap-aptos-sdk
Description: OmniSwap SDK for Aptos
Latest version: 1.3.7

# aptos-amm-sdk

`omni-amm-sdk` provides the the following TypeScript interface:
- swap in aptos amm
- add and remove liquid

## USAGE
### Install SDK
```
yarn add @omnibtc/omniswap-aptos-sdk
```
### Query All Support Coins
```
import { App,CoinListClient,CONFIGS } from '@omnibtc/omniswap-aptos-sdk';
import { AptosAccount, AptosClient, HexString } from 'aptos';

const netConf = CONFIGS.testnet;
const client = new AptosClient("https://fullnode.testnet.aptoslabs.com/v1");
const app = new App(client);

(async()=>{
    const coinListClient = await CoinListClient.load(app);
    const coinlist = await coinListClient.getCoinInfoList()
    console.log(`${JSON.stringify(coinlist)}`);
})()
```

### Query Lp coinlist

```
import { swap, CONFIGS, App, CoinListClient, OmniPoolProvider,OmniWalletClient } from '@omnibtc/omniswap-aptos-sdk';
import { AptosAccount, HexString, AptosClient } from 'aptos';
import { u64, sendPayloadTxAndLog, AptosLocalCache, parseTypeTagOrThrow, StructTag } from '@manahippo/move-to-ts';
import { LpToken } from '@omnibtc/omniswap-aptos-sdk/dist/omniwallet/type';
const client = new AptosClient('https://fullnode.testnet.aptoslabs.com/v1');
// user account
const account = new AptosAccount(
  new HexString('0x563c4efc61a2483ae15206a826ae8eed6e8afab831cc7ac68a3e9d3db17eab4e').toUint8Array()
);
console.log('Account Address: ' + account.address());
const netConf = CONFIGS.testnet;
const app = new App(client);

async function getLpList() {
  const wallet = await OmniWalletClient.createWalletClinet(
    netConf,
    app,
    // 用户钱包地址
    new HexString('0x4bdd4154da192cc59aee78697d7d70a90ea5150ab73ac85cdf15ad14d937dd38')
  );
  const lpCoins = await wallet.getLpCoinsList();
  console.log(`lpCoins: ${JSON.stringify(lpCoins)}`);
  const lpAptTestUsdc: LpToken = lpCoins.find((coin) => {
    if (coin.lpName.includes('AptosCoin') && coin.lpName.includes('TestUSDC')) {
      return true;
    }
  });
  console.log(`lpAptTestUsdc: ${JSON.stringify(lpAptTestUsdc)}`);
}
```

### Query All LiquidPools
```
import { App,CoinListClient } from '@omnibtc/omniswap-aptos-sdk';
import { AptosAccount, AptosClient, HexString } from 'aptos';
import { CONFIGS } from '@omnibtc/omniswap-aptos-sdk';

const netConf = CONFIGS.testnet;
const client = new AptosClient("https://fullnode.testnet.aptoslabs.com/v1");
const app = new App(client);

(async()=>{
    const coinListClient = await CoinListClient.load(app);
    const omniProvider = new OmniPoolProvider(app, netConf, coinListClient);
    const poollist = await omniProvider.loadPoolList();
    poollist.forEach(async (pool) => {
      await pool.reloadState(app);
      // calculate quote
      const quote = await pool.getQuote(1, true);
      console.log(`quote: ${JSON.stringify(quote)}`);
    });

})()
```
### Add liquidity
```
import { swap, CONFIGS, App, CoinListClient, OmniPoolProvider } from '@omnibtc/omniswap-aptos-sdk';
import { AptosAccount, HexString, AptosClient } from 'aptos';
import { u64, sendPayloadTxAndLog, AptosLocalCache, parseTypeTagOrThrow, StructTag } from '@manahippo/move-to-ts';
const client = new AptosClient('https://fullnode.testnet.aptoslabs.com/v1');
// user account
const account = new AptosAccount(
  new HexString('0x563c4efc61a2483ae15206a826ae8eed6e8afab831cc7ac68a3e9d3db17eab4e').toUint8Array()
);
const netConf = CONFIGS.testnet;
const app = new App(client);

(async () => {
  //const wallet = OmniWalletClient.createInTwoCalls(netConf, app, account.address());
  const X_ = parseTypeTagOrThrow('0x1::aptos_coin::AptosCoin');
  const Y_ = parseTypeTagOrThrow(
    '0xcb0b45f3b49a6ab957facd2029ee0cd6720bb12907877d2f499946a7fd8f8344::testnet_coins::TestUSDC'
  );
  // Adding liquidity requires judging whether the two tokens are sorted
  const isOrder = swap.Interface.is_order_(new AptosLocalCache(), [X_, Y_]);
  console.log(`isOrder: ${isOrder}`);
  // pool resource address
  const coinListClient = await CoinListClient.load(app);
  const omniProvider = new OmniPoolProvider(app, netConf, coinListClient);
  const pool = await omniProvider.loadPool(X_ as StructTag, Y_ as StructTag);
  console.log(`${JSON.stringify(pool.xCoinInfo.symbol.str())} - ${pool.yCoinInfo.symbol.str()}`);
  console.log(`${JSON.stringify(pool.xCoinInfo.decimals.value)} - ${pool.yCoinInfo.decimals.value}`);
  await pool.reloadState(app);
  // input X_ amount， calculate the amount of Y_ that can be obtained
  const coin_x_val = 100000000;
  const coin_y_quote = pool.calculateOutputLiquid(coin_x_val);
  console.log(`coin_x_val: ${coin_x_val}  ${coin_y_quote.outputSymbol} coin_y_val: ${coin_y_quote.outputUiAmt}`);
  const max_gas_ = 10000;
  const payload = swap.Interface.buildPayload_add_liquidity(
    u64(coin_x_val),
    u64(0),
    u64(coin_y_quote.outputUiAmt),
    u64(0),
    [X_, Y_]
  );
  await sendPayloadTxAndLog(client, account, payload, { maxGasAmount: max_gas_ });
})();

```

## CLI
```
git clone https://github.com/OmniBTC/omniswap-aptos-sdk.git
yarn 
yarn build-cli
```
List commands
```
Usage: yarn cli [options] [command]
Options:
  -c, --config <path>                           path to your aptos config.yml (generated with "aptos init")
  -p, --profile <PROFILE>                       aptos config profile to use (default:"default")
  -h, --help                                    display help for command

Commands:
  interface:add-liquidity <TYPE_X> <TYPE_Y> <coin_x_val> <coin_x_val_min> <coin_y_val> <coin_y_val_min> [max_gas]  Add liquidity to the pool
  interface:initialize-swap <controller> <beneficiary> [max_gas]                                                   Initialize swap (admin-only)
  interface:register-pool <TYPE_X> <TYPE_Y> [max_gas]                                                              Register a new liquidity pool for
                                                                                                                   'X'/'Y' pair
  interface:remove-liquidity <TYPE_X> <TYPE_Y> <lp_val> <min_x_out_val> <min_y_out_val> [max_gas]                  Remove liquidity to the pool
  interface:swap <TYPE_X> <TYPE_Y> <coin_val> <coin_out_min_val> [max_gas]                                         Swap 'X' for 'Y'
  list-pools                                                                                                       List all registered liquidity pools
  help [command]                                                                                                   display help for command
```