SDK Swap

// Base code block
import { GenSdk } from '@gen-io/gen-sdk';
const genSdk = new GenSdk()
const { api, swapSdk, config } =genSdk

Available public chain

eth,ropsten,rinkeby,bsc,solana,polygon,avax,fantom,arbitrum,terra,xdai,boba,ont,tron,heco,okex,optimism,harmony,dot,neo,aurora,cronos,moonriver,bsctest

connectWallet

Each chain can use a different wallet, which needs to be found in config.chains.chainList[n].wallets

 
let { wallet, chain } = await swapSdk.connectWallet({
 chain: 'bsc',
 walletName: 'MetaMask'
})

getGas

let swapData = await swapSdk.swapQuote({
    chain: 'bsc',
    dex: 'openOcean',
    inTokenAddress: '0x55d398326f99059ff775485246999027b3197955',
    outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
    amount: '0.1',
    gasPrice: gasPrice.data.gasPrice,
    slippage: 10,
    account: '0x9548f567Aa2bf71a6691B634F9808346C804c0D0',
})
const result = await swapSdk.getGas(swapData);

getBalance

let result =await swapSdk.getBalance({
    account: swapSdk.wallet.address,
    chain: swapSdk.chain.key,
    tokenAddressOrSymbol: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
    decimals: 18,
})

getAllowance

let result =await swapSdk.getAllowance({
    account: '0x9548f567Aa2bf71a6691B634F9808346C804c0D0',
    chain: 'bsc',
    tokenAddress: '0x55d398326f99059fF775485246999027B3197955',
    approveContract: '0x6352a56caadc4f1e25cd6c75970fa768a3304e64',
    decimals: 18
})

getChain

let activeWallet = await swapSdk.getWallet()

getWallet

let activeChain = await swapSdk.getChain()

swapQuote

dex: 1inch,matcha、openocean、jupiter


let swapData = await swapSdk.swapQuote({
    chain: 'bsc',
    dex: 'openOcean',
    inTokenAddress: '0x55d398326f99059ff775485246999027b3197955',
    outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
    amount: '0.1',
    gasPrice: 5,
    slippage: 10,
    account: '0x9548f567Aa2bf71a6691B634F9808346C804c0D0',
})

approve

approve requires checking whether the connectWallet


let approve = await swapSdk.approve({
  chain: 'ropsten',
  tokenAddress: '0x01BE23585060835E02B77ef475b0Cc51aA1e0709',
  contractAddress: '0x40d3b2f06f198d2b789b823cdbecd1db78090d74',
  amount: 2 * (10 ** 18),
})
if (!approve.code) {
  approve.on('error', (error) => {
    console.log(error)
  })
    .on('transactionHash', (hash) => {
      console.log(hash)
    })
    .on('receipt', (data) => {
      console.log(data)
    })
    .on('success', (data) => {
      console.log(data)
    })
} else {
  console.log(approve.message)
}
      

swap

swap requires checking whether the connectWallet

let swapData = await swapSdk.swapQuote({
  chain: 'bsc',
  dex: 'openOcean',
  inTokenAddress: '0x55d398326f99059ff775485246999027b3197955',
  outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
  amount: '0.1',
  gasPrice: 50,
  slippage: 3,
  account: 'The address of your wallet',
})
swapSdk.swap(swapData.data)
  .on('error', (error) => {
    // error
  })
  .on('transactionHash', (hash) => {
    // transactionHash
  })
  .on('receipt', (data) => {
     // receipt
  })
  .on('success', (data) => {
     // success
  })

Last updated