UNPKG

@xchainjs/xchain-binance

Version:

Custom Binance client and utilities used by XChainJS clients

203 lines (202 loc) 7.76 kB
import { BncClient } from '@binance-chain/javascript-sdk/lib/client'; import { AssetInfo, BaseXChainClient, Fees, Network, PreparedTx, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client'; import { Address, Asset, BaseAmount, TokenAsset } from '@xchainjs/xchain-util'; import { Account, Balance } from './types'; export type Coin = { asset: Asset; amount: BaseAmount; }; export type MultiTransfer = { to: Address; coins: Coin[]; }; export type MultiSendParams = { walletIndex?: number; transactions: MultiTransfer[]; memo?: string; }; /** * Interface for custom Binance client */ export interface BinanceClient { purgeClient(): void; getBncClient(): BncClient; getAccount(address?: Address, index?: number): Promise<Account>; getMultiSendFees(): Promise<Fees>; getSingleAndMultiFees(): Promise<{ single: Fees; multi: Fees; }>; multiSend(params: MultiSendParams): Promise<TxHash>; } /** * Custom Binance client */ declare class Client extends BaseXChainClient implements BinanceClient, XChainClient { private bncClient; /** * Constructor * Client has to be initialised with network type and phrase. * It will throw an error if an invalid phrase has been passed. * @param {XChainClientParams} params * @throws {"Invalid phrase"} Thrown if the given phase is invalid. */ constructor({ network, phrase, rootDerivationPaths, }: XChainClientParams); /** * Get the BncClient interface. * @returns {BncClient} The BncClient from `@binance-chain/javascript-sdk`. */ getBncClient(): BncClient; /** * Set/update the current network. * @param {Network} network * @returns {void} * * @throws {"Network must be provided"} * Thrown if network has not been set before. */ setNetwork(network: Network): void; /** * Get the client URL based on the network. * @returns {string} The client URL for binance chain based on the network. */ private getClientUrl; /** * Get the explorer URL based on the network. * * @returns {string} The explorer URL based on the network. */ getExplorerUrl(): string; /** * Get the explorer URL for a given address based on the network. * @param {Address} address The address to generate the explorer URL for. * @returns {string} The explorer URL for the given address. */ getExplorerAddressUrl(address: Address): string; /** * Get the explorer URL for a given transaction ID based on the network. * @param {string} txID The transaction ID to generate the explorer URL for. * @returns {string} The explorer URL for the given transaction ID. */ getExplorerTxUrl(txID: string): string; /** * @private * Get the private key for a given account index. * @param {number} index The account index for the derivation path. * @returns {PrivKey} The private key generated from the given phrase. * @throws {"Phrase not set"} Thrown if the phrase has not been set befor * Throws an error if phrase has not been set before * */ private getPrivateKey; /** * Get the address for a given account index. * @deprecated Use getAddressAsync instead. */ getAddress(index?: number): string; /** * Get the current address asynchronously for a given account index. * @param {number} index The account index for the derivation path. (optional) * @returns {Address} A promise resolving to the current address. * @throws {Error} Thrown if the phrase has not been set before. * A phrase is needed to create a wallet and to derive an address from it. */ getAddressAsync(index?: number): Promise<string>; /** * Validate the given address. * @param {Address} address The address to validate. * @returns {boolean} `true` if the address is valid, `false` otherwise. */ validateAddress(address: Address): boolean; /** * Get asset information. * @returns Asset information. */ getAssetInfo(): AssetInfo; /** * Get account data for a given address. * @param {Address} address The address to get account data for. (optional) * By default, it will return account data for the current wallet. * @param {number} index The account index for the derivation path. (optional) * @returns {Account} A promise resolving to the account details of the given address. */ getAccount(address?: Address, index?: number): Promise<Account>; /** * Get the balance of a given address. * @param {Address} address The address to get the balance for. (optional) * By default, it will return the balance of the current wallet. * @param {(Asset | TokenAsset)[]} asset If not set, it will return all assets available. (optional) * @returns {Balance[]} The balance of the address. */ getBalance(address: Address, assets?: (Asset | TokenAsset)[]): Promise<Balance[]>; /** * @private * Search transactions with parameters. * @returns {Params} The parameters to be used for transaction search. */ private searchTransactions; /** * Get transaction history of a given address with pagination options. * By default it will return the transaction history of the current wallet. * @param {TxHistoryParams} params The options to get transaction history. (optional) * @returns {TxsPage} The transaction history. */ getTransactions(params?: TxHistoryParams): Promise<TxsPage>; /** * Get the transaction details of a given transaction id. * @param {string} txId The transaction id. * @returns {Tx} The transaction details of the given transaction id. */ getTransactionData(txId: string): Promise<Tx>; /** * Broadcast multi-send transaction. * @param {MultiSendParams} params The multi-send transfer options. * @returns {TxHash} The transaction hash. */ multiSend({ walletIndex, transactions, memo }: MultiSendParams): Promise<TxHash>; /** * Transfer balances. * @param {TxParams} params The transfer options. * @returns {TxHash} The transaction hash. */ transfer({ walletIndex, asset, amount, recipient, memo }: TxParams): Promise<TxHash>; /** * Broadcast a raw transaction. * @param {string} txHex The hexadecimal representation of the raw transaction. * @returns {string} The result of broadcasting the transaction. */ broadcastTx(txHex: string): Promise<string>; /** * Get the current transfer fee. * @returns {TransferFee} The current transfer fee. */ private getTransferFee; /** * Get the current fee. * If the fee rate cannot be obtained from Thorchain, it falls back to fetching transfer fees from the Binance API. * * @returns {Fees} The current fee. */ getFees(): Promise<Fees>; /** * Get the current fee for multi-send transaction. * @returns {Fees} The current fee for multi-send transaction. */ getMultiSendFees(): Promise<Fees>; /** * Get the current fee for both single and multi-send transaction. * * @returns {SingleAndMultiFees} The current fee for both single and multi-send transaction. */ getSingleAndMultiFees(): Promise<{ single: Fees; multi: Fees; }>; /** * Prepare transfer. * Currently not supported for Binance chain. * @param {TxParams&Address} params The transfer options. * @returns {PreparedTx} The unsigned transaction data. */ prepareTx(): Promise<PreparedTx>; } export { Client };