How to Use
Installation
yarn add @xchainjs/xchain-bitcoincash
Peer Dependencies
You can visit the xchain-bitcoincash package repository to get the updated peer dependencies by looking at the "peerDependencies" object in the "package.json" file.
Testing
yarn install
yarn test
Example
Connect Wallet to New Bitcoincash Client
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
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.
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
// 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}`)
}
}
Last updated