How to Use

Installation

yarn add @xchainjs/xchain-binance

Peer Dependencies

You can visit the xchain-binance 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} `)        
    }
}

Last updated