# How to Use

## Installation

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

### Peer Dependencies

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

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

```
yarn install
yarn test
```

## Example <a href="#basic-usage-example" id="basic-usage-example"></a>

```
/import { Client, defaultAvaxParams } from "@xchainjs/xchain-avax"
import { FeeOption } from "@xchainjs/xchain-client"
import { assetToBase, baseToAsset, assetAmount, Asset, Chain  } from "@xchainjs/xchain-util"
```

### Connect Wallet to New Avax Chain Client <a href="#connect-wallet-to-new-avax-chain-client" id="connect-wallet-to-new-avax-chain-client"></a>

The network default is Mainnet

```
// Create new Avax Asset 
const assetRIP: Asset = {
    chain: Chain.Avalanche,
    symbol: `RIP-0x224695ba2a98e4a096a519b503336e06d9116e48`,
    ticker: `RIP`,
    synth: false,
  }
// Create new Avax Client Instance
const connectWallet =async () => {
    defaultAvaxParams.phrase = "phrase"
    const avaxClient = new Client(defaultAvaxParams)
    let address = avaxClient.getAddress()
    console.log(`Address: ${address}`)
    let isValid = avaxClient.validateAddress(address)
    if( isValid === true ){
        try {
            const balance = await avaxClient.getBalance(address)
            let assetAmount = (baseToAsset(balance[1].amount)).amount()
            console.log(`With balance: ${assetAmount}`)
    
        } catch (error) {
            console.log(`Caught: ${error}`)
        }
    }
}
```

### Transfer Avax Using AvaxClient <a href="#transfer-avax-using-avaxclient" id="transfer-avax-using-avaxclient"></a>

```
const transferAvax = async () => {
    let amountToTransfer = 0.1
    let recipient = "address"
    defaultAvaxParams.phrase = "phrase"
    const avaxClient = new Client(defaultAvaxParams)
    let amount = assetToBase(assetAmount(amountToTransfer, 18))
    console.log("Building transaction", JSON.stringify(amount.amount()))
    try {
        const txid = await avaxClient.transfer({ 
            "asset": assetRIP,
            "amount": amount,
            "recipient":recipient,
            feeOption: FeeOption.Fast,
        })
        console.log(`Transaction sent: ${txid}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    } 
}
```

### Get Fees <a href="#get-fees" id="get-fees"></a>

```
const returnFees = async () => {
    defaultAvaxParams.phrase = "phrase"
    const avaxClient = new Client(defaultAvaxParams)
    let amountToTransfer = 20
    let amount = assetToBase(assetAmount(amountToTransfer, 18))
    let recipient = "recipient"
    try {
        const txParams: TxParams = {
            walletIndex: 0,
            asset: assetRIP,
            amount: amount,
            recipient: recipient,
        }
        const {fast, fastest, average} = await avaxClient.getFees(txParams)
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}
```

### Get Transaction Data & History <a href="#get-transaction-data--history" id="get-transaction-data--history"></a>

```
const transactionData = async () => {
    defaultAvaxParams.phrase = "phrase"
    const avaxClient = new Client(defaultAvaxParams)
    let hash = "0x60721cf788b7cd4e56acf6479e71dfbd12e6c79c15e76595e4e52409bf686d4c"

    try {
        const txData = await avaxClient.getTransactionData(hash)
        console.log(`From ${JSON.stringify(txData)}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}
// Address History
const transactionHistory = async () => {
    defaultAvaxParams.phrase = "phrase"
    const avaxClient = new Client(defaultAvaxParams)
    let Address = avaxClient.getAddress()

    try {
        const txHistory = await avaxClient.getTransactions({address: Address, limit: 4 })
        console.log(`Found ${txHistory.total.toString()}`)
        txHistory.txs.forEach(tx => console.log(tx.hash))
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xchainjs.gitbook.io/xchainjs/clients/xchain-evm/xchain-avax/how-to-use.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
