Skip to content

Latest commit

 

History

History
473 lines (418 loc) · 20.2 KB

api.md

File metadata and controls

473 lines (418 loc) · 20.2 KB

API

Modules

Classes

Transaction

Build an unsigned transaction object.

RawTransaction

Build an unsigned rawTransaction hex string.

BnbApi

Create binance API server.

BnbRpc

The Binance Chain Node rpc client.

Wallet

Create or import BNB wallet.

Transaction

Build an unsigned transaction object.

  • Transaction

    new Transaction(network)

    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet' <String>

    create() ⇒ Transaction

    create an unsigned transaction, the parameter list corresponds to the parameters of each transaction, e.g: transfer、multiSend、placeOrder、cancelOrder、list、issue、freeze、unfreeze、burn、mint eg:

    const {Transaction} = require('binance-utils')
    const transaction = new Transaction('testnet');
    const tx = transaction.create('transfer', fromAddress, toAddress, amount, assert, accountNumber, sequence, memo);
    const multiTx = transaction.create('multiSend', fromAddress, outputs, accountNumber, sequence, memo);
    //and more...

    transfer(fromAddress, toAddress, amount, asset, accountNumber, sequence, memo) => Transaction

    Transfer tokens from one address to another.

    • fromAddress : from address <String>
    • toAddress : to address <String>
    • amount : Transfer amount <Number>
    • asset : asset <Number>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>
    • memo : memo <String>[optional]

    multiSend(fromAddress, outputs, accountNumber, sequence, memo) => Transaction

    Create a multi send tx

    • fromAddress : from address <String>
    • outputs : outputs <Array>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>
    • memo : memo <String>[optional] eg:
    const outputs = [
    {
      "to": "tbnb1p4kpnj5qz5spsaf0d2555h6ctngse0me5q57qe",
      "coins": [{
        "denom": "BNB",
        "amount": 10
      },{
        "denom": "BTC",
        "amount": 10
      }]
    },
    {
      "to": "tbnb1scjj8chhhp7lngdeflltzex22yaf9ep59ls4gk",
      "coins": [{
        "denom": "BTC",
        "amount": 10
      },{
        "denom": "BNB",
        "amount": 10
      }]
    }]

    cancelOrder(fromAddress, symbol, refid, accountNumber, sequence) => Transaction

    • fromAddress : from address <String>
    • symbol : symbol the market pair <String>
    • refid : refid the order ID of the order to cancel <String>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    placeOrder(address, symbol, side, price, quantity, timeinforce, accountNumber, sequence) => Transaction

    Place an order.

    • address : address <String>
    • symbol : symbol the market pair <Number>
    • side : side (1-Buy, 2-Sell) <String>
    • price : price <Number>
    • quantity : quantity <Number>
    • timeinforce : timeinforce (1-GTC(Good Till Expire), 3-IOC(Immediate or Cancel)) <Number>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    list(address, proposalId, baseAsset, quoteAsset, initPrice, accountNumber, sequence) => Transaction

    Add a new trading pair

    • address : address <String>
    • proposalId : proposalId <Number>
    • baseAsset : baseAsset <String>
    • quoteAsset : quoteAsset <String>
    • initPrice : initPrice <Number>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    issue(senderAddress, tokenName, symbol, totalSupply, mintable, accountNumber, sequence) => Transaction

    Create a new asset Transaction on Binance Chain

    • senderAddress : sender address <String>
    • tokenName : token name <String>
    • symbol : symbol <String>
    • totalSupply : total supply <Number>
    • mintable : mintable <Boolean>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    freeze(fromAddress, symbol, amount, accountNumber, sequence) => Transaction

    Freeze some amount of token

    • fromAddress : from address <String>
    • symbol : token name <String>
    • amount : amount <String>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    unfreeze(fromAddress, symbol, amount, accountNumber, sequence) => Transaction

    Unfreeze some amount of token

    • fromAddress : from address <String>
    • symbol : token name <String>
    • amount : amount <String>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    burn(fromAddress, symbol, amount, accountNumber, sequence) => Transaction

    Burn some amount of token

    • fromAddress : from address <String>
    • symbol : token name <String>
    • amount : amount <String>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

    mint(fromAddress, symbol, amount, accountNumber, sequence) => Transaction

    Mint tokens for an existing token

    • fromAddress : from address <String>
    • symbol : token name <String>
    • amount : amount <String>
    • accountNumber : account number <Number>
    • sequence : sequence <Number>

RawTransaction

Build an unsigned rawTransaction hex string

  • RawTransaction

    new RawTransaction(network)

    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet' <String>

    create() => String

    create an unsigned transaction hex string, the parameter list corresponds to the parameters of each transaction, e.g: transfer、multiSend、placeOrder、cancelOrder、list、issue、freeze、unfreeze、burn、mint eg:

    const {RawTransaction} = require('binance-utils')
    const rawTransaction = new RawTransaction('testnet');
    const txStr = rawTransaction.create('transfer', fromAddress, toAddress, amount, assert, accountNumber, sequence, memo);
    const multiTxStr = rawTransaction.create('multiSend', fromAddress, outputs, accountNumber, sequence, memo);
    //and more...

    static parseTransaction(rawTransaction) => Transaction

    Converts the rawTransaction string to Transaction

    • rawTransaction : unsigned transaction hex string <String> eg:
    const {RawTransaction} = require('binance-utils')
    const rawTransaction = new RawTransaction('testnet');
    const txStr = rawTransaction.create('transfer', fromAddress, toAddress, amount, assert, accountNumber, sequence, memo);
    const transaction = RawTransaction.parseTransaction(txStr);

BnbApi

Create binance API server

  • BnbApi

    new BnbApi(server)

    • server : Binance Chain public url <String> eg:
    const {BnbApi} = require('binance-utils');
    const apiProvider = new BnbApi('http://localhost:8080');

    async broadcastTx(rawTransaction, sync) => Promise

    Broadcast a raw transaction to the blockchain.

    • rawTransaction : Signed and serialized raw transaction <String>
    • sync : Use synchronous mode, default true <Boolean>

    async getAccount(address) => Promise

    Get account

    • address : Address <String>

    async getBalance(address) => Promise

    Get account balances

    • address : Address <String>

    async getMarkets(limit, offset) => Promise

    Get markets

    • limit : Max 1000 is default <Number>
    • offset : from beggining, default 0 <Number>

BnbRpc

The Binance Chain Node rpc client

  • BnbRpc

    new BnbRpc(uriString, netWork)

    • uriString : dataseed address <String>
    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet' <String> eg:
    const {BnbRpc} = require('binance-utils');
    const rpcProvider = new BnbApi('http://localhost:27146', 'testnet');

    async getTokenInfo(symbol) => Promise

    Get token info

    • symbol : symbol <String>

    async listAllTokens(offset, limit) => Promise

    Get tokens by offset and limit

    • offset : offset <Number>
    • limit : limit <Number>

    async getAccount(address) => Promise

    Get account

    • address : address <String>

    async getBalances(address) => Promise

    Get account balances

    • address : address <String>

    async getBalance(address, symbol) => Promise

    Get balance by symbol and address

    • address : address <String>
    • symbol : symbol <String>

    async getOpenOrders(address, symbol) => Promise

    Get open orders info

    • address : address <String>
    • symbol : symbol <String>

    async getTradingPairs(offset, limit) => Promise

    Get trading pairs info

    • offset : offset <Number>
    • limit : limit <Number>

    async getDepth(tradePair) => Promise

    Get depath

    • tradePair : tradePair <String>

    async status() => Promise

    Get fullnode status and block info

    async tx({hash, prove}) => Promise

    Get transaction by hash

    • hash : transaction hash <Buffer>
    • prove : Include a proof of the transaction inclusion in the block, default false `

    async block({height}) => Promise

    Get block at a given height. If no height is provided, it will fetch the latest block.

    • height : block height <Number>

    broadcastTxSync({tx})

    The transaction will be broadcasted and returns with the response from CheckTx.

    • tx : signed raw transaction info bytes in hex <Buffer>

Wallet

Create or import BNB wallet

  • Wallet

    new Wallet(privateKey, network) => Wallet

    • privateKey : private key,if the private key is empty, a new private key will be produced. <String>
    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet' <String> eg:
    const {Wallet} = require('binance-utils');
    // If the private key is empty, a new private key will be produced.
    const wallet = new Wallet('private key', 'testnet');

    exportKeystore(password) => Object

    Export current privateKey keystore object, and returns the private key and address.

    • password : The keystore object password. <String> eg:
    const keystore = wallet.exportKeystore('password');

    serializePubKey() => Buffer

    Serializes current wallet public key in a 33-byte compressed format. eg:

    const pubKey = wallet.serializePubKey();

    sign(transaction) => String

    Generates a signature (64 byte <r,s>) for a transaction based on current wallet privateKey

    • transaction : Unsigned transaction sign bytes hexstring or Transaction. <String|Transaction> eg:
    const rawTransaction = rawTransaction.create('transfer', fromAddress, toAddress, amount, assert, account_number, sequence, memo);
    
    const signature = wallet.sign(rawTransaction);
    //or
    const transaction = RawTransaction.parseTransaction(rawTransaction);
    const signature = wallet.sign(transaction);

    export() => Object

    Export wallet account info eg:

    const pubKey = wallet.serializePubKey();

    static createMnemonicWallet(mnemonic, network) => Wallet

    Create or import mnemonic HD wallet

    • mnemonic : The mnemonic phrase words. <String>
    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet'. <String> eg:
    const mnemonicHDWallet = Wallet.createMnemonicWallet('mnemonic...', 'testnet');

    static importKeystoreWallet(keystore, password, network) => Wallet

    Import keystore object wallet

    • keystore : keystore Keystore object. <Json Object>
    • password : password <String>
    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet'. <String> eg:
    const mnemonicHDWallet = Wallet.importKeystoreWallet(keystore, 'password', 'testnet');

    static checkAddress(address, network) => Boolean

    Checks whether an address is valid.

    • address : address. <String>
    • network : Binance chain network, supported 'mainnet/testnet', default: 'mainnet'. <String> eg:
    const mnemonicHDWallet = Wallet.checkAddress('tbnb1fu029wlrx87pdxecqrw96a662v6cdrj5zsv5rd', 'testnet');

    static verifySignature(sigHex, signBytesHex, publicKeyHex) => Boolean

    Verifies a signature (64 byte <r,s>) given the sign bytes and public key.

    • sigHex : The signature hexstring. <String>
    • signBytesHex : Unsigned transaction sign bytes hexstring. <String>
    • publicKeyHex : The public key.<String> eg:
    const rawTransaction = rawTransaction.create('transfer', fromAddress, toAddress, amount, assert, account_number, sequence, memo);
    const transaction = RawTransaction.parseTransaction(rawTransaction);
    const signature = wallet.sign(transaction.signMsgHex);
    const verify= Wallet.verifySignature(signature, transaction.signMsgHex, wallet.export().publicKey);
    // verify = true