@xchainjs/xchain-ethereum
Version:
Ethereum EVM client for XChainJS
167 lines (161 loc) • 6.62 kB
JavaScript
import { Network as Network$1, 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, Network } from 'ethers';
import BigNumber from 'bignumber.js';
/**
* Constants for fee bounds in Gwei.
*/
const LOWER_FEE_BOUND = 400000000;
const UPPER_FEE_BOUND = 1000000000000;
/**
* Decimal places for Ethereum gas asset.
*/
const ETH_GAS_ASSET_DECIMAL = 18;
/**
* Chain identifier for Ethereum.
*/
const ETHChain = 'ETH';
/**
* Ethereum asset information.
*/
const AssetETH = {
chain: ETHChain,
symbol: 'ETH',
ticker: 'ETH',
type: AssetType.NATIVE,
};
// ===== Ethers providers =====
const ETH_MAINNET_ETHERS_PROVIDER = new JsonRpcProvider('https://eth.llamarpc.com', 'homestead');
const network = Network.from('sepolia');
const ETH_TESTNET_ETHERS_PROVIDER = new JsonRpcProvider('https://ethereum-sepolia-rpc.publicnode.com', network);
// Object to map network to ethers providers
const ethersJSProviders = {
[Network$1.Mainnet]: ETH_MAINNET_ETHERS_PROVIDER,
[Network$1.Testnet]: ETH_TESTNET_ETHERS_PROVIDER,
[Network$1.Stagenet]: ETH_MAINNET_ETHERS_PROVIDER,
};
// ===== Ethers providers =====
// ===== ONLINE providers =====
// Testnet online provider
const ETH_ONLINE_PROVIDER_TESTNET = new EtherscanProviderV2(ETH_TESTNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', ETHChain, AssetETH, ETH_GAS_ASSET_DECIMAL, 11155111);
// Mainnet online provider
const ETH_ONLINE_PROVIDER_MAINNET = new EtherscanProviderV2(ETH_MAINNET_ETHERS_PROVIDER, 'https://api.etherscan.io/v2', process.env.ETHERSCAN_API_KEY || '', ETHChain, AssetETH, ETH_GAS_ASSET_DECIMAL, 1);
// Object to map network to online providers
const ethProviders = {
[Network$1.Mainnet]: ETH_ONLINE_PROVIDER_MAINNET,
[Network$1.Testnet]: ETH_ONLINE_PROVIDER_TESTNET,
[Network$1.Stagenet]: ETH_ONLINE_PROVIDER_MAINNET,
};
// ===== ONLINE providers =====
// ===== Explorers =====
// Mainnet explorer provider
const ETH_MAINNET_EXPLORER = new ExplorerProvider('https://etherscan.io', 'https://etherscan.io/address/%%ADDRESS%%', 'https://etherscan.io/tx/%%TX_ID%%');
// Testnet explorer provider
const ETH_TESTNET_EXPLORER = new ExplorerProvider('https://sepolia.etherscan.io/', 'https://sepolia.etherscan.io/address/%%ADDRESS%%', 'https://sepolia.etherscan.io/tx/%%TX_ID%%');
// Object to map network to explorer providers
const ethExplorerProviders = {
[Network$1.Mainnet]: ETH_MAINNET_EXPLORER,
[Network$1.Testnet]: ETH_TESTNET_EXPLORER,
[Network$1.Stagenet]: ETH_MAINNET_EXPLORER,
};
// ===== Explorers =====
/**
* Root derivation paths for Ethereum networks.
*/
const ethRootDerivationPaths = {
[Network$1.Mainnet]: "m/44'/60'/0'/0/",
[Network$1.Testnet]: "m/44'/60'/0'/0/",
[Network$1.Stagenet]: "m/44'/60'/0'/0/",
};
/**
* Default parameters for Ethereum clients.
*/
const defaults = {
[Network$1.Mainnet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(30 * Math.pow(10, 9)),
},
[Network$1.Testnet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(30 * Math.pow(10, 9)),
},
[Network$1.Stagenet]: {
approveGasLimit: new BigNumber(200000),
transferGasAssetGasLimit: new BigNumber(23000),
transferTokenGasLimit: new BigNumber(100000),
gasPrice: new BigNumber(30 * Math.pow(10, 9)),
},
};
/**
* Default Ethereum client parameters.
*/
const defaultEthParams = {
chain: ETHChain,
gasAsset: AssetETH,
gasAssetDecimals: ETH_GAS_ASSET_DECIMAL,
defaults,
providers: ethersJSProviders,
explorerProviders: ethExplorerProviders,
dataProviders: [ethProviders],
network: Network$1.Mainnet,
feeBounds: {
lower: LOWER_FEE_BOUND,
upper: UPPER_FEE_BOUND,
},
rootDerivationPaths: ethRootDerivationPaths,
};
/**
* Import statements
* Importing the `Client` class from the '@xchainjs/xchain-evm' module
* Importing the `defaultEthParams` constant from the './const' file
*/
/**
* Class definition for the Ethereum EVM client.
* Extends the `XchainEvmClient` class.
*/
class ClientKeystore extends ClientKeystore$1 {
/**
* Constructor for the Ethereum EVM client.
* @param {Object} config - Configuration object for the client (optional).
* Defaults to `defaultEthParams` if not provided.
*/
constructor(config = defaultEthParams) {
// Call the constructor of the parent class with the provided config or the default parameters
super(Object.assign(Object.assign({}, config), { signer: config.phrase
? new KeystoreSigner({
phrase: config.phrase,
provider: config.providers[config.network || Network$1.Mainnet],
derivationPath: config.rootDerivationPaths
? config.rootDerivationPaths[config.network || Network$1.Mainnet]
: '',
})
: undefined }));
}
}
/**
* Class definition for the Ethereum EVM client.
* Extends the `XchainEvmClient` class.
*/
class ClientLedger extends ClientLedger$1 {
/**
* Constructor for the Ethereum 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$1.Mainnet],
derivationPath: config.rootDerivationPaths ? config.rootDerivationPaths[config.network || Network$1.Mainnet] : '',
}) }));
}
}
// Export all elements from the 'client' module
export { AssetETH, ClientKeystore as Client, ClientKeystore, ClientLedger, ETHChain, ETH_GAS_ASSET_DECIMAL, LOWER_FEE_BOUND, UPPER_FEE_BOUND, ClientKeystore as default, defaultEthParams };