Get started with XATA SDK

Prerequisites

yarn add @xatadev/sdk
  • ethers.js
  • yarn

1. SDK Basics

// 1. Import the modules and the ConveyorPair ABIimport { Currency, CurrencyAmount,  Pair, Token, ChainId, Trade, Route, Percent, TradeType } from ‘@xatadev/sdk’import { Contract } from ‘ethers’;import { abi as pairAbi } from ‘@xata/sdk/abis/ConveyorV2.json’// 2. Instantiate the Tokensconst TokenA = new Token(ChainId.MATIC, ‘0x2791bca1f2de4661ed88a30c99a7a9449aa84174’, 6) // USDC on Maticconst TokenB = new Token(ChainId.MATIC, ‘0x8f3cf7ad23cd3cadbd9735aff958023239c6a063’, 18) // DAI on MATIC// 3. Fetch the Pair addressconst pair_address = Pair.getAddress(TokenA, TokenB);// 4. Fetch the reserves, then the sort the tokensconst pairContract = new Contract(pair_address, pairAbi, web3)const [ reserve0, reserve1 ] = await pairContract.getReserves();const [ token0, token1 ] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]// 5. Instantiate the Pairconst pair = new Pair(CurrencyAmount.fromRawAmount(token0, reserve0.toString()), CurrencyAmount.fromRawAmount(token1, reserve1.toString()));

1.1 Calculate token price

const amountARaw = ‘10’; // 10 USDCconst amountA = CurrencyAmount.fromRawAmount(tokenA, amountARaw);const amountB = pair.priceOf(tokenA).quote(amountA);

1.2 Swap

const route = new Route(pair, [tokenA, tokenB]);
const trade = new Trade(route, amountIn, TradeType.EXACT_INPUT)const slippage = new Percent(‘5’, ‘1000’); // 0.5%const amountOutMin = trade.minimumAmountOut(slippage)
const trade = new Trade(route, amountIn, TradeType.EXACT_OUTPUT)const slippage = new Percent(‘5’, ‘1000’); // 0.5%const amountOutMin = trade.maxAmountInput(slippage)

1.3 Remove Liquidity —

const totalSupplyRaw = await pairContract.totalSupply();const liquidityRaw = await pairContract.balanceOf(<your address>); // max liquidityconst totalSupply = CurrencyAmount.fromRawAmount(pairContract, totalSupplyRaw);const liquidity = CurrencyAmount.fromRawAmount(pairContract, liquidityRaw);const amountA = await pair.getLiquidityValue(tokenA, totalSupply, liquidity)const amountB = await pair.getLiquidityValue(tokenB, totalSupply, liquidity)

2. XATA-API

2.1 Setup

// Import the API module from the SDKimport { Xata } from ‘@xatadev/sdk’// Instantiate the module. Then, provide a Web3 provider and the address of the fee token.const web3 = window.ethereum; // Metamaskconst usdc = ‘0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48’;const xataApi = new Xata(web3, usdc);
const usdt = ‘0xdac17f958d2ee523a2206206994597c13d831ec7’;await xataApi.setFeeToken(usdt);

2.2 Fee Token Calculation

  1. calculateFee() parameters:
  • Chain ID
  • Token address
  • Token decimals
  • Gas fee
  • Native token symbol. See here
  • Chain ID
  • Token address
  • Token decimals
  • Gas fee
// 1. Import the functionsimport { calculateFee, calculateFeeThenConvert, ChainId  } from ‘@xatadev/sdk’;import { BigNumber } from ‘ethers’;// 2. Calculate the gas fee using ethers BigNumberconst gaslimit = BigNumber.from(‘100000’); // some gas limitconst gasPrice = BigNumber.from(‘10000’); // some gas priceconst gasFee = gaslimit.mul(gasPrice);// 3.1. Calculate the fee amount using calculateFee...const bscNative = ‘bnb’const bsc_usdt = ‘0x55d398326f99059ff775485246999027b3197955’ // 18 decimalsconst maxTokenFee = await calculateFee(ChainId.BSC, bsc_usdt, 18, gasFee, bscNative)// 3.2. ... or using calculateFeeThenConvertconst polygon_usdt = ‘0xc2132d05d31c914a87c6611c10748aeb04b58e8f’ // 6 decimalsconst maxTokenFee: await calculateFeeThenConvert(chainId.MATIC, polygon_usdt, 6, gasFee)

When to use calculateFee vs calculateFeeThenConvert?

  • Binance Smart Chain
  • Ethereum Mainnet

2.3 Add Liquidity—

  • tokenA
  • tokenB
  • amountADesired
  • amountBDesired
  • amountAMin
  • amountBMin
  • user
  • deadline
  • gasLimit — Optional
  • gasPrice — Optional

2.4 Swap

Exact Tokens For Tokens —

  • amountIn
  • amountOutMin
  • path
  • user
  • gasLimit — Optional
  • gasPrice — Optional

Tokens For Exact Tokens —

  • amountOut
  • amountInMax
  • path
  • user
  • gasLimit — Optional
  • gasPrice — Optional

2.5 Remove Liquidity —

  • Pair address
  • Owner
  • Spender
  • Value
  • Nonce
  • Deadline
  • tokenA
  • tokenB
  • liquidity
  • amountAMin
  • amountBMin
  • user
  • deadline
  • sig — Signature object
  • gasLimit — Optional
  • gasPrice — Optional

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store