How to Use

Installation

yarn add @xchainjs/xchain-thorchain

Peer Dependencies

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

Testing

yarn install
yarn test

Examples

Connect wallet to new Thor Client

Create a new thorchain client The client has two different sets of parameters XchainClientParams & ThorchainClientParams ThorchainClient includes chainIds. getChainIds() returns all chain id's for default Client Url. The network default is Mainnet


// Imports 
import { Client, getChainIds, getDefaultClientUrl} from '@xchainjs/xchain-thorchain'
import { assetToBase, baseToAsset, assetAmount } from "@xchainjs/xchain-util"


// Create new instance of the client and query chain for balances. 
const connectWallet = async () => {

    let phrase = "phrase"
    const thorClient = new Client({phrase})
    let address = thorClient.getAddress()
    console.log(`Address: ${address}`)
    try {
        const balance = await thorClient.getBalance(address)
        let assetAmount = (baseToAsset(balance[0].amount)).amount()
        console.log(`With balance: ${assetAmount}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Transfer Rune using Thor Client

Create a new Thorchain client instance Convert amount to transfer to base amount Build transaction.


//Imports
import { assetToBase, baseToAsset, assetAmount } from "@xchainjs/xchain-util"
 
const transferRune = async () => {

    let phrase = "phrase"
    const thorClient = new Client({phrase})
    let amountToTransfer = 0.1
    let amount = assetToBase(assetAmount(amountToTransfer, DECIMAL ))
    let recipient = "thor1cf4dsll8rema8y3xvvsn2t786xrkhp3d679qxh" 
    try {
        const txid = await thorClient.transfer({
            "amount": amount,
            "recipient": recipient,
            "memo": "test",
            "asset": AssetRuneNative,
            "walletIndex": 0 
        })
        console.log(`Transaction sent: ${JSON.stringify(txid)}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Transaction Data & Transaction History

Retrieve transaction data using transaction hash and address


const transactionData = async () => {
 
    let phrase = "phrase"
    const thorClient = new Client({phrase})
    let hash = "insert hash"
    let address = thorClient.getAddress()
    try {
        const txData = await thorClient.getTransactionData(hash, address)
        console.log(`From ${JSON.stringify(txData)}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}
// By default getTransactions() returns the transactions for the current address
const transactionHistory = async () => {

    let phrase = "phrase"
    const thorClient = new Client({phrase})
    
    try {
        const txHistory = await thorClient.getTransactions() 
        console.log(`Found ${txHistory.total.toString()}`)
        txHistory.txs.forEach(tx => console.log(tx.hash))
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Transfer Fees

Thorchain runs on a fee type of Flatfee set to 0.02 rune

// Returns Fees Fast: 0.02 Fastest: 0.02 Average: 0.02
const fee = async () => {

    let phrase = "phrase"
    const thorClient = new Client({phrase})
    try {
        const {fast, fastest, average} = await thorClient.getFees()
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Network and Explorer Data

// Query thorchain client for network data and explorer data

const explorerUrl = async () => {

    let phrase = "phrase"
    const thorClient = new Client({phrase})
    let hash = "insert hash"
    try {
        const networkData = thorClient.getExplorerUrl()
        console.log(`Explorer url: ${networkData}`)
        const transactionUrl = thorClient.getExplorerTxUrl(hash)
        console.log(`Explorer transaction: ${transactionUrl}`)

    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Last updated