XchainJS
  • Overview
  • Installation
  • Examples instructions
  • Clients
    • xchain-evm
      • xchain-avax
        • How to Use
      • xchain-arbitrum
        • How to Use
      • xchain-bsc
        • How to Use
      • xchain-ethereum
        • How to Use
    • xchain-utxo
      • xchain-bitcoin
        • How to use
      • xchain-bitcoincash
        • How to Use
      • xchain-dash
      • xchain-doge
        • How to Use
      • xchain-litecoin
        • How to Use
    • xchain-cosmos-sdk
      • xchain-cosmos
        • How to Use
      • xchain-kujira
        • How to Use
      • xchain-mayachain
        • How to Use
      • xchain-thorchain
        • How to Use
    • xchain-binance
      • How to Use
    • xchain-solana
      • How to use
      • Examples
        • Generate address
        • Get balances
        • Get token balance
        • Prepare transaction
        • Make transaction
        • Make token transaction
  • Wallet
  • Protocols
    • THORChain
      • xchain-thorchain-amm
        • How to Use
        • Make swap using THORChain
        • Handle liquidity and savers
        • Open and close loans
      • xchain-thorchain-query
        • How to Use
        • Check transaction status
        • Estimate a swap
      • xchain-midgard
        • How to Use
      • xchain-thornode
        • How to Use
      • xchain-midgard-query
        • How to Use
    • MAYAProtocol
      • xchain-mayachain-amm
        • How to Use
        • Make swap using MAYAChain
      • xchain-mayachain-query
        • How to Use
      • xchain-mayamidgard
        • How to Use
      • xchain-mayanode
        • How to Use
      • xchain-mayamidgard-query
        • How to Use
  • Aggregator
  • Providers
    • xchain-utxo-providers
      • How it Works
    • xchain-evm-providers
      • How it Works
  • Others
    • xchain-crypto
      • How it Works
      • How to Use
    • xchain-util
      • How it Works
      • How to Use
  • Contributors
  • Documentation maintenance
Powered by GitBook
On this page
  • Installation
  • Peer Dependencies
  • Example
  1. Protocols
  2. THORChain
  3. xchain-thorchain-amm

How to Use

Previousxchain-thorchain-ammNextMake swap using THORChain

Last updated 1 year ago

Installation

yarn add @xchainjs/xchain-thorchain-amm

Peer Dependencies

You can visit the package repository to get the updated peer dependencies by looking at the "peerDependencies" object in the "package.json" file.

Example

Swap from BNB -> RUNE

// Imports
import { AssetRuneNative, } from '@xchainjs/xchain-util'
import { assetAmount, assetFromString, assetToBase, Chain  } from '@xchainjs/xchain-util'
import { CryptoAmount, EstimateSwapParams, Wallet, Midgard, SwapEstimate, ThorchainAMM } from '@xchainjs/xchain-thorchain-amm'
import BigNumber from 'bignumber.js'

// Swap from BUSD to RUNE
const doSwap = async () => {
  let phrase = "phrase"
  const mainnetWallet = new Wallet(Network.Mainnet, phrase|| 'you forgot to set the phrase')
  const swapParams = {
    input: new CryptoAmount(assetToBase(assetAmount(1)), BUSD),
    destinationAsset: AssetRuneNative,
    destinationAddress: mainnetWallet.clients[Chain.THORChain].getAddress(),
    slipLimit: new BigNumber(0.03),
  }
  try {
    const outPutCanSwap = await thorchainQueryMainnet.estimateSwap(estimateSwapParams)
    print(outPutCanSwap)
    if (outPutCanSwap.txEstimate.canSwap) {
      const output = await mainetThorchainAmm.doSwap(mainnetWallet, estimateSwapParams)
      console.log(`Tx hash: ${output.hash},\n Tx url: ${output.url}\n WaitTime: ${output.waitTimeSeconds}`)
      expect(output).toBeTruthy()
    }
  } catch (error: any) {
    console.log(error.message)
  }
}
//Outputs
{
  memo: '=:THOR.RUNE:xxx:53281999',
  expiry: 2022-09-22T13:14:10.941Z,
  toAddress: 'bnb13fjncau2d3sw9nrvtpg67mh80gulu25v5msznc',
  txEstimate: {
    input: '$ 1',
    totalFees: {
      inboundFee: 'ᚱ 0.01863509',
      swapFee: 'ᚱ 0.00000008',
      outboundFee: 'ᚱ 0.06',
      affiliateFee: 'ᚱ 0'
    },
    slipPercentage: '0.00000012685928643813',
    netOutput: 'ᚱ 0.53281571',
    netOutputDecimals: 8,
    waitTimeSeconds: '12',
    canSwap: true,
    errors: []
  }
}
Tx hash: "Hash will be here",
Tx url: "URL will be here"
xchain-thorchain-amm