XchainJS
  • Overview
  • Installation
  • Examples instructions
  • Clients
    • xchain-evm
      • xchain-avax
        • How to Use
      • xchain-arbitrum
        • How to Use
      • xchain-bsc
        • How to Use
      • xchain-ethereum
        • How to Use
    • xchain-utxo
      • xchain-bitcoin
        • How to use
      • xchain-bitcoincash
        • How to Use
      • xchain-dash
      • xchain-doge
        • How to Use
      • xchain-litecoin
        • How to Use
    • xchain-cosmos-sdk
      • xchain-cosmos
        • How to Use
      • xchain-kujira
        • How to Use
      • xchain-mayachain
        • How to Use
      • xchain-thorchain
        • How to Use
    • xchain-binance
      • How to Use
    • xchain-solana
      • How to use
      • Examples
        • Generate address
        • Get balances
        • Get token balance
        • Prepare transaction
        • Make transaction
        • Make token transaction
  • Wallet
  • Protocols
    • THORChain
      • xchain-thorchain-amm
        • How to Use
        • Make swap using THORChain
        • Handle liquidity and savers
        • Open and close loans
      • xchain-thorchain-query
        • How to Use
        • Check transaction status
        • Estimate a swap
      • xchain-midgard
        • How to Use
      • xchain-thornode
        • How to Use
      • xchain-midgard-query
        • How to Use
    • MAYAProtocol
      • xchain-mayachain-amm
        • How to Use
        • Make swap using MAYAChain
      • xchain-mayachain-query
        • How to Use
      • xchain-mayamidgard
        • How to Use
      • xchain-mayanode
        • How to Use
      • xchain-mayamidgard-query
        • How to Use
  • Aggregator
  • Providers
    • xchain-utxo-providers
      • How it Works
    • xchain-evm-providers
      • How it Works
  • Others
    • xchain-crypto
      • How it Works
      • How to Use
    • xchain-util
      • How it Works
      • How to Use
  • Contributors
  • Documentation maintenance
Powered by GitBook
On this page
  • Installation
  • Peer Dependencies
  • Testing
  • Example
  • Connect Wallet to New Binance Chain Client
  • Transfer BNB Using BncClient
  • Get Transaction Data & Transaction History
  • Get Current Fees
  • Multisend Transaction
  1. Clients
  2. xchain-binance

How to Use

Previousxchain-binanceNextxchain-solana

Last updated 8 months ago

Installation

yarn add @xchainjs/xchain-binance

Peer Dependencies

You can visit the 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 Binance Chain Client

The network default is Mainnet

//Imports 
import { Client } from '@xchainjs/xchain-binance'
import { FeeOption } from "@xchainjs/xchain-client"

// Connect wallet to new btc client 
const connectWallet = async () => {
    let phrase = "phrase"
    const bncClient = new Client({phrase })  
    let address = bncClient.getAddress()
    console.log(`Asset Address is: ${address}`)

    let balances = await bncClient.getBalance(address)
    try {
        let assetAmount = (baseToAsset(balances[0].amount)).amount()
        console.log(`with balance: ${assetAmount}`)
    } catch (error) {
        console.log('no balance')
    }
}

Transfer BNB Using BncClient

Create a new instance of BncClient Set the correct amount using xchain-util helper functions Build new transactions using TxParams and call transfer.

const transferBnb = async () => {
    let amountToTransfer = 0.0001
    let recipient = await getRecipientAddress()
    let phrase = "phrase"
    const bncClient = new Client({phrase })
    let amount = assetToBase(assetAmount(amountToTransfer, 8))
    console.log("Building transaction")
    try {
        const txid = await bncClient.transfer({
            "walletIndex":0,
            "asset": AssetBNB,
            "amount": amount,
            "recipient": recipient,
            "memo": "memo"
        })
        console.log(`Amount ${amount.amount().toString()} ${AssetBNB.symbol} TransactionId: ${txid}`)
    } catch (error) {
        console.log(`Transfer failed: ${error}`)
    }
}

Get Transaction Data & Transaction History

Create a new BncClient instance Call getTransactionData(hash) returns [object object] Retrieve relevant data

    let hash = "insert hash string"
    try {
        const txData = await bncClient.getTransactionData(hash)
        console.log(`From ${JSON.stringify(txData.from[0]["from"])}`)
        console.log(`To ${JSON.stringify(txData.to[0]["to"])}`)

    } catch (error) {
        console.log(`Error: ${error}`)
    }

// Retrieve transaction history for a set address
// txHistoryParams > address, offset, startTime, asset? 
    try {
            const txHistory = await bncClient.getTransactions({address: Address, limit:4 })
            console.log(`Found ${txHistory.total.toString()}`)
            txHistory.txs.forEach(tx => console.log(JSON.stringify(tx.to)))
            
        } catch (error) {
            console.log(`Error: ${error}`)
    }

Get Current Fees

BNC is a fixed fee client, average, fast, and fastest return the same value. getFees() returns current fees for the network getMultiSendFees() returns current fees for a multi-send tx

    try {
        const fee = await bncClient.getFees()
        console.log(`Fees average:  ${baseToAsset(fee.average).amount()}`)
        console.log(`Fees fast:  ${baseToAsset(fee.fast).amount()}`)
        console.log(`Fees fastest:  ${baseToAsset(fee.fastest).amount()}`)
        
    } catch (error) {
        console.log(error)
    }

Multisend Transaction

Building a multisend transaction with BncClient Build transactions with transactions taking an Array of objects.

const multisendTransfer = async () => {
    let recipientA = "recipient A address"
    let recipientB = "recipient B address"
    let amountToTransfer = 0.0001
    let amountA = assetToBase(assetAmount(amountToTransfer, 8))
    let amountB = assetToBase(assetAmount(amountToTransfer, 8))
    let phrase = "phrase"
    const bncClient = new Client({phrase})
    console.log("Building transaction ")
    try {
        const txId = await bncClient.multiSend({
            "walletIndex": 0,
            "transactions": [{
                "coins":[{
                    "asset": AssetBNB, 
                    "amount": amountA                 
                }],
                "to":recipientA
            }, {
                "coins": [{
                    "asset":AssetBNB,
                    "amount": amountB
                }],
                "to":recipientB
            }],
            "memo": "memo"
        })
        console.log(`Multisend Txid: ${txId}`)
    } catch (error) {
        console.log(`Multi transfer failed: ${error} `)        
    }
}
xchain-binance