Skip to content
    doSwap@thorchain
    index.ts
    tsconfig.json
    Packager files
    package.json
    yarn.lock
    Config files
    .replit
    replit.nix
    import cosmosclient from '@cosmos-client/core'
    import { Network } from '@xchainjs/xchain-client'
    import { Midgard, MidgardCache, MidgardQuery } from '@xchainjs/xchain-midgard-query'
    import { THORChain } from '@xchainjs/xchain-thorchain'
    import { ThorchainAMM, Wallet, AmmEstimateSwapParams } from '@xchainjs/xchain-thorchain-amm'
    import {
    ThorchainCache,
    ThorchainQuery,
    Thornode,
    TxDetails
    } from '@xchainjs/xchain-thorchain-query'
    import { assetAmount, assetFromStringEx, assetToBase, assetToString, register9Rheader, delay, CryptoAmount } from '@xchainjs/xchain-util'
    import axios from 'axios'


    register9Rheader(axios)
    register9Rheader(cosmosclient.config.globalAxios)

    function printTx(txDetails: TxDetails, input: CryptoAmount) {
    const expanded = {
    memo: txDetails.memo,
    expiry: txDetails.expiry,
    toAddress: txDetails.toAddress,
    txEstimate: {
    input: input.formatedAssetString(),
    totalFees: {
    asset: assetToString(txDetails.txEstimate.totalFees.asset),
    outboundFee: txDetails.txEstimate.totalFees.outboundFee.formatedAssetString(),
    affiliateFee: txDetails.txEstimate.totalFees.affiliateFee.formatedAssetString(),
    },
    slipBasisPoints: txDetails.txEstimate.slipBasisPoints.toFixed(),
    netOutput: txDetails.txEstimate.netOutput.formatedAssetString(),
    outboundDelaySeconds: txDetails.txEstimate.outboundDelaySeconds,
    canSwap: txDetails.txEstimate.canSwap,
    errors: txDetails.txEstimate.errors,
    },
    }
    console.log(expanded)
    }

    const delayedLog = async (message: string, delayMs: number) => {
    const startTime = new Date().getTime()
    const endTime = startTime + delayMs
    let remainingTime = delayMs

    while (remainingTime > 0) {
    const elapsedMs = delayMs - remainingTime
    const remainingSeconds = Math.ceil(remainingTime / 1000)
    const elapsedSeconds = Math.floor(elapsedMs / 1000)
    const progress = Math.floor((elapsedMs / delayMs) * 100)

    console.log(`${message} (${elapsedSeconds}s/${remainingSeconds}s ${progress}%)`)

    await delay(500)
    remainingTime = endTime - new Date().getTime()
    }

    console.log(`${message} (Done!)`)
    }


    /**
    * From asset to asset with no Affiliate address on testnet
    */
    const doSwap = async (tcAmm: ThorchainAMM, wallet: Wallet) => {
    try {
    const amount = 10
    const fromAsset = assetFromStringEx(`THOR.RUNE`)
    const toAsset = assetFromStringEx(`BNB.BNB`)

    const toChain = toAsset.synth ? THORChain : toAsset.chain
    const destinationAddress = wallet.clients[toChain].getAddress()

    const streamingInterval = 1 // Every block
    const streamingQuantity = 0 // THORChain works out how many
    const affiliateAddress = "dx" // thorname or address
    const affiliateBps = 0 // set as required, also tracks volume

    console.log('Source address is: ' + destinationAddress)

    const swapParams: AmmEstimateSwapParams = {
    fromAsset: fromAsset,
    amount: new CryptoAmount(assetToBase(assetAmount(amount)), fromAsset),
    destinationAsset: toAsset,
    destinationAddress: destinationAddress,
    streamingInterval: streamingInterval, // you can use streaming swap OR toleranceBps
    streamingQuantity: streamingQuantity,
    affiliateAddress: affiliateAddress,
    affiliateBps: affiliateBps,
    // toleranceBps: 300, //optional
    wallet: wallet,
    walletIndex: 0
    }


    const outPutCanSwap = await tcAmm.estimateSwap(swapParams)
    printTx(outPutCanSwap, swapParams.amount)

    if (outPutCanSwap.txEstimate.canSwap) {
    const output = await tcAmm.doSwap(wallet, swapParams)
    console.log(
    `Tx hash: ${output.hash},\n Tx url: ${output.url}\n WaitTime: ${outPutCanSwap.txEstimate.outboundDelaySeconds}`,
    )
    console.log('Waiting for transaction to be confirmed...')
    await delayedLog(
    'hash',
    outPutCanSwap.txEstimate.outboundDelaySeconds <= 6
    ? 12000
    : outPutCanSwap.txEstimate.outboundDelaySeconds * 1000,
    )
    //await checkTx(Network.Mainnet, output.hash)
    }
    } catch (error) {
    console.error(error)
    }

    const main = async () => {

    const thorchainCache = new ThorchainCache(new Thornode(), new MidgardQuery(new MidgardCache()))
    const thorchainQuery = new ThorchainQuery(thorchainCache)
    const thorchainAmm = new ThorchainAMM(thorchainQuery)

    // random seed with no funds. Can use a wallet from xchain or your own.
    const seed = "rookie peanut glimpse blade wrong cereal capable liar divert shy region wrap"
    const wallet = new Wallet(seed, thorchainQuery) // creates keystore wallet. Wallet wraps the individual xchain clients

    await doSwap(thorchainAmm, wallet)
    }

    main()
    .then(() => process.exit(0))
    .catch((err) => console.error(err))