@xchainjs/xchain-avax
Version:
Avax EVM client for XChainJS
144 lines (138 loc) • 6.87 kB
JavaScript
import { Network, ExplorerProvider } from '@xchainjs/xchain-client';
import { ClientKeystore as ClientKeystore$1, KeystoreSigner, ClientLedger as ClientLedger$1, LedgerSigner } from '@xchainjs/xchain-evm';
import { EtherscanProviderV2, RoutescanProvider } from '@xchainjs/xchain-evm-providers';
import { AssetType } from '@xchainjs/xchain-util';
import { JsonRpcProvider } from 'ethers';
import BigNumber from 'bignumber.js';
// 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: 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 JsonRpcProvider(`https://rpc.ankr.com/avalanche/${ankrApiKey}`, {
name: 'avalanche',
chainId: 43114,
});
const AVALANCHE_TESTNET_ETHERS_PROVIDER = new JsonRpcProvider(`https://rpc.ankr.com/avalanche_fuji/${ankrApiKey}`, {
name: 'fuji',
chainId: 43113,
});
// Define ethers providers for different networks
const ethersJSProviders = {
[Network.Mainnet]: AVALANCHE_MAINNET_ETHERS_PROVIDER,
[Network.Testnet]: AVALANCHE_TESTNET_ETHERS_PROVIDER,
[Network.Stagenet]: AVALANCHE_MAINNET_ETHERS_PROVIDER,
};
// Define online providers (Etherscan) for mainnet and testnet
const AVAX_ONLINE_PROVIDER_TESTNET = new 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 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 = {
[Network.Mainnet]: AVAX_ONLINE_PROVIDER_MAINNET,
[Network.Testnet]: AVAX_ONLINE_PROVIDER_TESTNET,
[Network.Stagenet]: AVAX_ONLINE_PROVIDER_MAINNET,
};
// Define Routescan providers for mainnet and testnet
const ROUTESCAN_PROVIDER_MAINNET = new RoutescanProvider(AVALANCHE_MAINNET_ETHERS_PROVIDER, 'https://api.routescan.io', 43114, AssetAVAX, AVAX_DECIMAL);
const ROUTESCAN_PROVIDER_TESTNET = new RoutescanProvider(AVALANCHE_TESTNET_ETHERS_PROVIDER, 'https://api.routescan.io', 43113, AssetAVAX, AVAX_DECIMAL, true);
// Define Routescan providers for different networks
const routescanProviders = {
[Network.Mainnet]: ROUTESCAN_PROVIDER_MAINNET,
[Network.Testnet]: ROUTESCAN_PROVIDER_TESTNET,
[Network.Stagenet]: ROUTESCAN_PROVIDER_MAINNET,
};
// Define explorer providers for mainnet and testnet
const AVAX_MAINNET_EXPLORER = new ExplorerProvider('https://snowtrace.dev/', 'https://snowtrace.dev/address/%%ADDRESS%%', 'https://snowtrace.dev/tx/%%TX_ID%%');
const AVAX_TESTNET_EXPLORER = new 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 = {
[Network.Mainnet]: AVAX_MAINNET_EXPLORER,
[Network.Testnet]: AVAX_TESTNET_EXPLORER,
[Network.Stagenet]: AVAX_MAINNET_EXPLORER,
};
// Define root derivation paths for different networks
const ethRootDerivationPaths = {
[Network.Mainnet]: `m/44'/60'/0'/0/`,
[Network.Testnet]: `m/44'/60'/0'/0/`,
[Network.Stagenet]: `m/44'/60'/0'/0/`,
};
// Define default parameters for the Avalanche client
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)),
},
};
// 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: 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 ClientKeystore$1 {
// Constructor function that takes an optional config parameter, defaulting to defaultAvaxParams
constructor(config = defaultAvaxParams) {
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 Avalanche EVM client.
* Extends the `XchainEvmClient` class.
*/
class ClientLedger extends ClientLedger$1 {
/**
* 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 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 { AVAXChain, AVAX_DECIMAL, AVAX_GAS_ASSET_DECIMAL, AssetAVAX, ClientKeystore as Client, ClientKeystore, ClientLedger, LOWER_FEE_BOUND, UPPER_FEE_BOUND, ClientKeystore as default, defaultAvaxParams };