You can visit the xchain-bitcoin 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 Client and Access Class Methods
Decrypt keystore returns phrase
Create a new client instance
Use the client to getAddress() & getBalance() of address
The network default is Mainnet
//Imports
import { Client } from "@xchainjs/xchain-bitcoin"
// Connect wallet to new btc client
const connectWallet = async () => {
let phrase = "phrase"
const btcClient = new Client({ phrase})
let address = btcClient.getAddress()
console.log(`Asset Address is: ${address}`)
let balances = await btcClient.getBalance(address)
try {
let assetAmount = (baseToAsset(balances[0].amount)).amount()
console.log(`Asset address balance: ${assetAmount}`)
} catch (error) {
console.log('Address has no balance')
}
}
Transfer BTC Using btcClient
Default feeRate is fast
Import helper functions from @xchainjs/xchain-util
Decrypt keystore to retrieve a phrase
Use utils to convert to BaseAmount for tx params
Build TX using parameters
Client.transfer() > returns Promise<string> - The transaction hash
//Imports
import { assetToBase, baseToAsset, assetAmount, AssetBTC } from "@xchainjs/xchain-util"
let amountToTransfer = 0.0001
let recipient = 'Recipent_address'
const transfer = async () => {
let phrase = "phrase"
let btcClient = new Client({phrase })
let amount = assetToBase(assetAmount(amountToTransfer, 8))
try {
const txid = await btcClient.transfer({
'asset': AssetBTC,
'recipient': recipient,
'amount': amount,
'memo': "payment"
})
console.log(`Amount: ${amount.amount().toString()} ${AssetBTC.symbol} Transaction id: ${txid}`)
} catch (error){
console.log(`Transfer failed ${error}`)
}
}
Transfer & Set feeRate
Build transactions using parameters
Set feeRate in transaction parameters
Or use getFeeRates()
//Returns FeeRates > this allows for dynamic feeRate adjustment on selection
const { fast, fastest, average } = await btcClient.getFeeRates()
try {
const txid = await btcClient.transfer({
'asset': AssetBTC,
'recipient': recipient,
'amount': amount,
'memo': "test transfer",
feeRate: fast
})
console.log(`Amount ${baseToAsset(amount).amount()} ${AssetBTC.symbol} Transaction id ${txid}`)
} catch (error){
console.log(`Transfer failed ${error}`)
}
Get Fees & FeeRates Estimations
Client function getFees() returns an object
Fast: {"type":"BASE","decimal":8}
Create a new btcClient instance
Call getTransaction(hash) returns JSON object
const transactionData = async () => {
let phrase = "phrase"
let btcClient = new Client({network: Network.Mainnet, phrase })
let hash = "txhash string"
try{
const txData = await btcClient.getTransactionData(hash)
console.log(`From ${JSON.stringify(txData)}`)
}catch (error) {
console.log(`Error: ${error}`)
}
}
Get Transaction History
Search the client for transaction history
Create a new client instance
Call a function with a variable address
Results can be filtered with extra parameters { offset, limit, startTime, asset?}