> For the complete documentation index, see [llms.txt](https://xchainjs.gitbook.io/xchainjs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xchainjs.gitbook.io/xchainjs/clients/xchain-cosmos-sdk/xchain-thorchain/how-to-use.md).

# How to Use

## Installation <a href="#installation" id="installation"></a>

```
yarn add @xchainjs/xchain-thorchain
```

### Peer Dependencies

You can visit the [xchain-thorchain](https://github.com/xchainjs/xchainjs-lib/blob/master/packages/xchain-thorchain/package.json) package repository to get the updated peer dependencies by looking at the "peerDependencies" object in the "package.json" file.

### Testing <a href="#thorchain-client-testing" id="thorchain-client-testing"></a>

```
yarn install
yarn test
```

## Examples <a href="#basic-usage-examples" id="basic-usage-examples"></a>

#### Connect wallet to new Thor Client <a href="#connect-wallet-to-new-thor-client" id="connect-wallet-to-new-thor-client"></a>

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 <a href="#transfer-rune-using-thor-client" id="transfer-rune-using-thor-client"></a>

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 <a href="#get-transaction-data--transaction-history" id="get-transaction-data--transaction-history"></a>

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 <a href="#get-transfer-fees" id="get-transfer-fees"></a>

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 <a href="#get-network-and-explorer-data" id="get-network-and-explorer-data"></a>

```
// 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}`)
    }
}
```
