# How to Use

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

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

### Peer Dependencies

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

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

```
yarn install
yarn test
```

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

#### Connect Wallet to New Bitcoincash Client <a href="#connect-wallet-to-new-bitcoincash-client" id="connect-wallet-to-new-bitcoincash-client"></a>

The network default is Mainnet

```
// Imports
import { Client } from "@xchainjs/xchain-bitcoincash"

// Connect wallet to new BitcoinCash Client & validate address
const connectWallet = async () => {
    let phrase = "phrase"
    const bchClient = new Client({phrase})
    let address = bchClient.getAddress()
    let isValid = bchClient.validateAddress(address)
    if( isValid === true ){
        try {
            const balance = await bchClient.getBalance(address)
            let assetAmount = (baseToAsset(balance[0].amount)).amount()
            console.log(`With balance: ${assetAmount}`)
    
        } catch (error) {
            console.log(`Caught: ${error}`)
        }
    } else {
        console.log(`Address ${address} is not valid`)
    }
}

```

#### Transfer Using BitcoinCash Client <a href="#transfer-using-bitcoincash-client" id="transfer-using-bitcoincash-client"></a>

Transaction parameters. The default fee rate is: 1\
Returns transaction hash string

```

//Imports 
import { assetToBase, assetAmount, AssetBCH } from "@xchainjs/xchain-util"

// Convert amount to transfer to BaseAmount using helper functions  
const transferBitcoinCash = async () => {
    let amountToTransfer = 0.01
    let recipient = await getRecipientAddress()
    let phrase = "phrase"
    const bchClient = new Client({ phrase })
    let amount = assetToBase(assetAmount(amountToTransfer, BCH_DECIMAL))
    console.log("Building transaction")
    try {
        const txid = await bchClient.transfer({ 
            "asset": AssetBCH,
            "amount": amount,
            "recipient":recipient,
        })
        console.log(`Transaction sent: ${txid}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    } 
}

// Build transaction with feeRate adjustment
const feeRates = await bchClient.getFeeRates()
console.log(feeRates.average, feeRates.fast, feeRates.fastest)

try {
        const txid = await bchClient.transfer({ 
            "asset": AssetBCH,
            "amount": amount,
            "recipient":recipient,
            feeRate: feeRates.average // default is 1
        })
        console.log(`Transaction sent: ${txid}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    } 

```

#### Get Current Fees & Fee Rates. <a href="#get-current-fees--fee-rates" id="get-current-fees--fee-rates"></a>

getFees() returns fees in base amount i.e BCH `Fees Fast: 0.00000234 Fastest: 0.0000117 Average: 0.00000117`\
getFeeRates() returns feeRates as `number`

```
// Query client for fees and fee rates
const returnFees = async () => {
    llet phrase = "phrase"
    const bchClient = new Client({phrase })
    try {
        const {fast, fastest, average} = await bchClient.getFees()
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)
        const feeRates = await bchClient.getFeeRates()
        console.log(feeRates.average, feeRates.fast, feeRates.fastest)
    } 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>

```

// Returns transaction object for a particular txId
const transactionData = async () => {
    let phrase = "phrase"
    const bchClient = new Client({phrase })
    let hash = "insert hash"

    try {
        const txData = await bchClient.getTransactionData(hash)
        console.log(`From ${JSON.stringify(txData)}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}

// Returns transaction history for a particular address
const transactionHistory = async () => {
    let phrase = "phrase"
    const bchClient = new Client({phrase })
    let Address = bchClient.getAddress()

    try {
        const txHistory = await bchClient.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}`)
    }
}
```

<br>


---

# 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-utxo/xchain-bitcoincash/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.
