UNPKG

@xchainjs/xchain-bsc

Version:

Binance Smart Chain EVM client for XChainJS

155 lines (149 loc) 6.07 kB
import { Network, ExplorerProvider } from '@xchainjs/xchain-client'; import { ClientKeystore as ClientKeystore$1, KeystoreSigner, ClientLedger as ClientLedger$1, LedgerSigner } from '@xchainjs/xchain-evm'; import { EtherscanProviderV2 } from '@xchainjs/xchain-evm-providers'; import { AssetType } from '@xchainjs/xchain-util'; import { JsonRpcProvider } from 'ethers'; import BigNumber from 'bignumber.js'; /** * Module providing configuration parameters and providers for the Binance Smart Chain (BSC) client. */ /** * Lower fee bound for BSC transactions. */ const LOWER_FEE_BOUND = 99000000; /** * Upper fee bound for BSC transactions. */ const UPPER_FEE_BOUND = 1000000000000; /** * Decimal precision for BSC gas asset. */ const BSC_GAS_ASSET_DECIMAL = 18; /** * Chain identifier for Binance Smart Chain (BSC). */ const BSCChain = 'BSC'; /** * Base "chain" asset of Binance Smart Chain (BSC). */ const AssetBSC = { chain: BSCChain, symbol: 'BNB', ticker: 'BNB', type: AssetType.NATIVE, }; // Ankr api key const ankrApiKey = process.env.ANKR_API_KEY; // Ethers providers const BSC_MAINNET_ETHERS_PROVIDER = new JsonRpcProvider(`https://rpc.ankr.com/bsc/${ankrApiKey}`); const BSC_TESTNET_ETHERS_PROVIDER = new JsonRpcProvider('https://bsc-testnet.public.blastapi.io'); const ethersJSProviders = { [Network.Mainnet]: BSC_MAINNET_ETHERS_PROVIDER, [Network.Testnet]: BSC_TESTNET_ETHERS_PROVIDER, [Network.Stagenet]: BSC_MAINNET_ETHERS_PROVIDER, }; // ONLINE providers const BSC_ONLINE_PROVIDER_TESTNET = new EtherscanProviderV2(BSC_TESTNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', BSCChain, AssetBSC, BSC_GAS_ASSET_DECIMAL, 97); const BSC_ONLINE_PROVIDER_MAINNET = new EtherscanProviderV2(BSC_MAINNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', BSCChain, AssetBSC, BSC_GAS_ASSET_DECIMAL, 56); const bscProviders = { [Network.Mainnet]: BSC_ONLINE_PROVIDER_MAINNET, [Network.Testnet]: BSC_ONLINE_PROVIDER_TESTNET, [Network.Stagenet]: BSC_ONLINE_PROVIDER_MAINNET, }; // Explorers const BSC_MAINNET_EXPLORER = new ExplorerProvider('https://bscscan.com/', 'https://bscscan.com/address/%%ADDRESS%%', 'https://bscscan.com/tx/%%TX_ID%%'); const BSC_TESTNET_EXPLORER = new ExplorerProvider('https://testnet.bscscan.com/', 'https://testnet.bscscan.com/address/%%ADDRESS%%', 'https://testnet.bscscan.com/tx/%%TX_ID%%'); const bscExplorerProviders = { [Network.Mainnet]: BSC_MAINNET_EXPLORER, [Network.Testnet]: BSC_TESTNET_EXPLORER, [Network.Stagenet]: BSC_MAINNET_EXPLORER, }; // Default parameters const ethRootDerivationPaths = { [Network.Mainnet]: "m/44'/60'/0'/0/", [Network.Testnet]: "m/44'/60'/0'/0/", [Network.Stagenet]: "m/44'/60'/0'/0/", }; const defaults = { [Network.Mainnet]: { approveGasLimit: new BigNumber(200000), transferGasAssetGasLimit: new BigNumber(23000), transferTokenGasLimit: new BigNumber(100000), gasPrice: new BigNumber(30 * Math.pow(10, 9)), }, [Network.Testnet]: { approveGasLimit: new BigNumber(200000), transferGasAssetGasLimit: new BigNumber(23000), transferTokenGasLimit: new BigNumber(100000), gasPrice: new BigNumber(30 * Math.pow(10, 9)), }, [Network.Stagenet]: { approveGasLimit: new BigNumber(200000), transferGasAssetGasLimit: new BigNumber(23000), transferTokenGasLimit: new BigNumber(100000), gasPrice: new BigNumber(30 * Math.pow(10, 9)), }, }; /** * Default parameters for the BSC client. */ const defaultBscParams = { chain: BSCChain, gasAsset: AssetBSC, gasAssetDecimals: BSC_GAS_ASSET_DECIMAL, defaults, providers: ethersJSProviders, explorerProviders: bscExplorerProviders, dataProviders: [bscProviders], network: Network.Mainnet, feeBounds: { lower: LOWER_FEE_BOUND, upper: UPPER_FEE_BOUND, }, rootDerivationPaths: ethRootDerivationPaths, }; /** * Module importing and providing a customized client for the Binance Smart Chain (BSC). */ /** * Customized BSC client extending the base XchainEvmClient. */ class ClientKeystore extends ClientKeystore$1 { /** * Constructor for the BSC client. * * @param {Object} config Configuration parameters for the client. Defaults to defaultBscParams if not provided. */ constructor(config = defaultBscParams) { super(Object.assign(Object.assign({}, config), { signer: config.phrase ? new KeystoreSigner({ phrase: config.phrase, provider: config.providers[config.network || Network.Mainnet], derivationPath: config.rootDerivationPaths ? config.rootDerivationPaths[config.network || Network.Mainnet] : '', }) : undefined })); } } /** * Class definition for the Binance Smart Chain EVM client. * Extends the `XchainEvmClient` class. */ class ClientLedger extends ClientLedger$1 { /** * Constructor for the Binance Smart Chain EVM client. * @param {Object} config - Configuration object for the client (optional). * Defaults to `defaultEthParams` if not provided. */ constructor(config) { // Call the constructor of the parent class with the provided config or the default parameters super(Object.assign(Object.assign({}, config), { signer: new LedgerSigner({ transport: config.transport, provider: config.providers[config.network || Network.Mainnet], derivationPath: config.rootDerivationPaths ? config.rootDerivationPaths[config.network || Network.Mainnet] : '', }) })); } } // Export all elements from the 'client' module export { AssetBSC, BSCChain, BSC_GAS_ASSET_DECIMAL, ClientKeystore as Client, ClientKeystore, ClientLedger, LOWER_FEE_BOUND, UPPER_FEE_BOUND, ClientKeystore as default, defaultBscParams };