UNPKG

@xchainjs/xchain-avax

Version:
162 lines (152 loc) 7.86 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var xchainClient = require('@xchainjs/xchain-client'); var xchainEvm = require('@xchainjs/xchain-evm'); var xchainEvmProviders = require('@xchainjs/xchain-evm-providers'); var xchainUtil = require('@xchainjs/xchain-util'); var ethers = require('ethers'); var BigNumber = require('bignumber.js'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var BigNumber__default = /*#__PURE__*/_interopDefault(BigNumber); // Import necessary modules and classes from external packages and files // Define constants related to Avalanche const AVAX_DECIMAL = 18; const LOWER_FEE_BOUND = 1000000000; const UPPER_FEE_BOUND = 1000000000000; const AVAX_GAS_ASSET_DECIMAL = 18; const AVAXChain = 'AVAX'; const AssetAVAX = { chain: AVAXChain, symbol: 'AVAX', ticker: 'AVAX', type: xchainUtil.AssetType.NATIVE }; // Ankr api key const ankrApiKey = process.env.ANKR_API_KEY; // Define JSON-RPC providers for mainnet and testnet const AVALANCHE_MAINNET_ETHERS_PROVIDER = new ethers.JsonRpcProvider(`https://rpc.ankr.com/avalanche/${ankrApiKey}`, { name: 'avalanche', chainId: 43114, }); const AVALANCHE_TESTNET_ETHERS_PROVIDER = new ethers.JsonRpcProvider(`https://rpc.ankr.com/avalanche_fuji/${ankrApiKey}`, { name: 'fuji', chainId: 43113, }); // Define ethers providers for different networks const ethersJSProviders = { [xchainClient.Network.Mainnet]: AVALANCHE_MAINNET_ETHERS_PROVIDER, [xchainClient.Network.Testnet]: AVALANCHE_TESTNET_ETHERS_PROVIDER, [xchainClient.Network.Stagenet]: AVALANCHE_MAINNET_ETHERS_PROVIDER, }; // Define online providers (Etherscan) for mainnet and testnet const AVAX_ONLINE_PROVIDER_TESTNET = new xchainEvmProviders.EtherscanProviderV2(AVALANCHE_TESTNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', AVAXChain, AssetAVAX, 18, 43113); const AVAX_ONLINE_PROVIDER_MAINNET = new xchainEvmProviders.EtherscanProviderV2(AVALANCHE_MAINNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', AVAXChain, AssetAVAX, 18, 43114); // Define providers for different networks const avaxProviders = { [xchainClient.Network.Mainnet]: AVAX_ONLINE_PROVIDER_MAINNET, [xchainClient.Network.Testnet]: AVAX_ONLINE_PROVIDER_TESTNET, [xchainClient.Network.Stagenet]: AVAX_ONLINE_PROVIDER_MAINNET, }; // Define Routescan providers for mainnet and testnet const ROUTESCAN_PROVIDER_MAINNET = new xchainEvmProviders.RoutescanProvider(AVALANCHE_MAINNET_ETHERS_PROVIDER, 'https://api.routescan.io', 43114, AssetAVAX, AVAX_DECIMAL); const ROUTESCAN_PROVIDER_TESTNET = new xchainEvmProviders.RoutescanProvider(AVALANCHE_TESTNET_ETHERS_PROVIDER, 'https://api.routescan.io', 43113, AssetAVAX, AVAX_DECIMAL, true); // Define Routescan providers for different networks const routescanProviders = { [xchainClient.Network.Mainnet]: ROUTESCAN_PROVIDER_MAINNET, [xchainClient.Network.Testnet]: ROUTESCAN_PROVIDER_TESTNET, [xchainClient.Network.Stagenet]: ROUTESCAN_PROVIDER_MAINNET, }; // Define explorer providers for mainnet and testnet const AVAX_MAINNET_EXPLORER = new xchainClient.ExplorerProvider('https://snowtrace.dev/', 'https://snowtrace.dev/address/%%ADDRESS%%', 'https://snowtrace.dev/tx/%%TX_ID%%'); const AVAX_TESTNET_EXPLORER = new xchainClient.ExplorerProvider('https://testnet.snowtrace.dev/', 'https://testnet.snowtrace.dev/address/%%ADDRESS%%', 'https://testnet.snowtrace.dev/tx/%%TX_ID%%'); // Define explorer providers for different networks const avaxExplorerProviders = { [xchainClient.Network.Mainnet]: AVAX_MAINNET_EXPLORER, [xchainClient.Network.Testnet]: AVAX_TESTNET_EXPLORER, [xchainClient.Network.Stagenet]: AVAX_MAINNET_EXPLORER, }; // Define root derivation paths for different networks const ethRootDerivationPaths = { [xchainClient.Network.Mainnet]: `m/44'/60'/0'/0/`, [xchainClient.Network.Testnet]: `m/44'/60'/0'/0/`, [xchainClient.Network.Stagenet]: `m/44'/60'/0'/0/`, }; // Define default parameters for the Avalanche client const defaults = { [xchainClient.Network.Mainnet]: { approveGasLimit: new BigNumber__default.default(200000), transferGasAssetGasLimit: new BigNumber__default.default(23000), transferTokenGasLimit: new BigNumber__default.default(100000), gasPrice: new BigNumber__default.default(30 * Math.pow(10, 9)), }, [xchainClient.Network.Testnet]: { approveGasLimit: new BigNumber__default.default(200000), transferGasAssetGasLimit: new BigNumber__default.default(23000), transferTokenGasLimit: new BigNumber__default.default(100000), gasPrice: new BigNumber__default.default(30 * Math.pow(10, 9)), }, [xchainClient.Network.Stagenet]: { approveGasLimit: new BigNumber__default.default(200000), transferGasAssetGasLimit: new BigNumber__default.default(23000), transferTokenGasLimit: new BigNumber__default.default(100000), gasPrice: new BigNumber__default.default(30 * Math.pow(10, 9)), }, }; // Define the default parameters for the Avalanche client const defaultAvaxParams = { chain: AVAXChain, gasAsset: AssetAVAX, gasAssetDecimals: AVAX_GAS_ASSET_DECIMAL, defaults, providers: ethersJSProviders, explorerProviders: avaxExplorerProviders, dataProviders: [avaxProviders, routescanProviders], network: xchainClient.Network.Mainnet, feeBounds: { lower: LOWER_FEE_BOUND, upper: UPPER_FEE_BOUND, }, rootDerivationPaths: ethRootDerivationPaths, }; // Import the Client class from '@xchainjs/xchain-evm' module // Define and export the Client class as the default exportClientKeystore class ClientKeystore extends xchainEvm.ClientKeystore { // Constructor function that takes an optional config parameter, defaulting to defaultAvaxParams constructor(config = defaultAvaxParams) { super(Object.assign(Object.assign({}, config), { signer: config.phrase ? new xchainEvm.KeystoreSigner({ phrase: config.phrase, provider: config.providers[config.network || xchainClient.Network.Mainnet], derivationPath: config.rootDerivationPaths ? config.rootDerivationPaths[config.network || xchainClient.Network.Mainnet] : '', }) : undefined })); } } /** * Class definition for the Avalanche EVM client. * Extends the `XchainEvmClient` class. */ class ClientLedger extends xchainEvm.ClientLedger { /** * Constructor for the Avalanche 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 xchainEvm.LedgerSigner({ transport: config.transport, provider: config.providers[config.network || xchainClient.Network.Mainnet], derivationPath: config.rootDerivationPaths ? config.rootDerivationPaths[config.network || xchainClient.Network.Mainnet] : '', }) })); } } // Export all elements from the 'client' module exports.AVAXChain = AVAXChain; exports.AVAX_DECIMAL = AVAX_DECIMAL; exports.AVAX_GAS_ASSET_DECIMAL = AVAX_GAS_ASSET_DECIMAL; exports.AssetAVAX = AssetAVAX; exports.Client = ClientKeystore; exports.ClientKeystore = ClientKeystore; exports.ClientLedger = ClientLedger; exports.LOWER_FEE_BOUND = LOWER_FEE_BOUND; exports.UPPER_FEE_BOUND = UPPER_FEE_BOUND; exports.default = ClientKeystore; exports.defaultAvaxParams = defaultAvaxParams;