@xchainjs/xchain-arbitrum
Version:
Arbitrum EVM client for XChainJS
115 lines (111 loc) • 5.22 kB
JavaScript
import { Network, ExplorerProvider } from '@xchainjs/xchain-client';
import { Client as Client$1, KeystoreSigner } 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';
// Import necessary modules and classes from external packages and files
// Define constants related to Arbitrum
const ARB_DECIMAL = 18;
const LOWER_FEE_BOUND = 10000000;
const UPPER_FEE_BOUND = 1000000000;
const ARB_GAS_ASSET_DECIMAL = 18;
const ARBChain = 'ARB';
// ARB ETH Gas asset
const AssetAETH = { chain: ARBChain, symbol: 'ETH', ticker: 'ETH', type: AssetType.NATIVE };
// ARB
const AssetARB = {
chain: ARBChain,
symbol: 'ARB-0x912ce59144191c1204e64559fe8253a0e49e6548',
ticker: 'ARB',
type: AssetType.TOKEN,
};
// Define JSON-RPC providers for mainnet and testnet
// Ankr api key
const ARBITRUM_MAINNET_ETHERS_PROVIDER = new JsonRpcProvider('https://arb1.arbitrum.io/rpc');
const ARBITRUM_TESTNET_ETHERS_PROVIDER = new JsonRpcProvider('https://goerli-rollup.arbitrum.io/rpc');
// Define ethers providers for different networks
const ethersJSProviders = {
[Network.Mainnet]: ARBITRUM_MAINNET_ETHERS_PROVIDER,
[Network.Testnet]: ARBITRUM_TESTNET_ETHERS_PROVIDER,
[Network.Stagenet]: ARBITRUM_MAINNET_ETHERS_PROVIDER,
};
// Define online providers (Etherscan) for mainnet and testnet
const ARB_ONLINE_PROVIDER_MAINNET = new EtherscanProviderV2(ARBITRUM_MAINNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', ARBChain, AssetAETH, 18, 42161);
const ARB_ONLINE_PROVIDER_TESTNET = new EtherscanProviderV2(ARBITRUM_TESTNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', ARBChain, AssetAETH, 18, 421614);
// Define providers for different networks
const arbProviders = {
[Network.Mainnet]: ARB_ONLINE_PROVIDER_MAINNET,
[Network.Testnet]: ARB_ONLINE_PROVIDER_TESTNET,
[Network.Stagenet]: ARB_ONLINE_PROVIDER_MAINNET,
};
// Define explorer providers for mainnet and testnet
const ARB_MAINNET_EXPLORER = new ExplorerProvider('https://arbiscan.io/', 'https://arbiscan.io/address/%%ADDRESS%%', 'https://arbiscan.io/tx/%%TX_ID%%');
const ARB_TESTNET_EXPLORER = new ExplorerProvider('https://goerli.arbiscan.io', 'https://goerli.arbiscan.io/address/%%ADDRESS%%', 'https://goerli.arbiscan.io/tx/%%TX_ID%%');
// Define explorer providers for different networks
const arbExplorerProviders = {
[Network.Mainnet]: ARB_MAINNET_EXPLORER,
[Network.Testnet]: ARB_TESTNET_EXPLORER,
[Network.Stagenet]: ARB_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 Arbitrum client
const defaults = {
[Network.Mainnet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(0.2 * Math.pow(10, 9)),
},
[Network.Testnet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(0.2 * Math.pow(10, 9)),
},
[Network.Stagenet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(0.2 * Math.pow(10, 9)),
},
};
// Define the default parameters for the Arbitrum client
const defaultArbParams = {
chain: ARBChain,
gasAsset: AssetAETH,
gasAssetDecimals: ARB_GAS_ASSET_DECIMAL,
defaults,
providers: ethersJSProviders,
explorerProviders: arbExplorerProviders,
dataProviders: [arbProviders],
network: Network.Mainnet,
feeBounds: {
lower: LOWER_FEE_BOUND,
upper: UPPER_FEE_BOUND,
},
rootDerivationPaths: ethRootDerivationPaths,
};
// Import the Client class from '@xchainjs/xchain-evm' module
// Create a class called Client that extends the XchainEvmClient class
class Client extends Client$1 {
// Constructor function that takes an optional config parameter, defaulting to defaultArbParams
constructor(config = defaultArbParams) {
// Call the constructor of the parent class (XchainEvmClient) with the provided config
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 }));
}
}
export { ARBChain, ARB_DECIMAL, ARB_GAS_ASSET_DECIMAL, AssetAETH, AssetARB, Client, LOWER_FEE_BOUND, UPPER_FEE_BOUND, defaultArbParams };