@xchainjs/xchain-ethereum
Version:
Ethereum EVM client for XChainJS
185 lines (175 loc) • 7.54 kB
JavaScript
'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);
/**
* 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: xchainUtil.AssetType.NATIVE,
};
// ===== Ethers providers =====
const ETH_MAINNET_ETHERS_PROVIDER = new ethers.JsonRpcProvider('https://eth.llamarpc.com', 'homestead');
const network = ethers.Network.from('sepolia');
const ETH_TESTNET_ETHERS_PROVIDER = new ethers.JsonRpcProvider('https://ethereum-sepolia-rpc.publicnode.com', network);
// Object to map network to ethers providers
const ethersJSProviders = {
[xchainClient.Network.Mainnet]: ETH_MAINNET_ETHERS_PROVIDER,
[xchainClient.Network.Testnet]: ETH_TESTNET_ETHERS_PROVIDER,
[xchainClient.Network.Stagenet]: ETH_MAINNET_ETHERS_PROVIDER,
};
// ===== Ethers providers =====
// ===== ONLINE providers =====
// Testnet online provider
const ETH_ONLINE_PROVIDER_TESTNET = new xchainEvmProviders.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 xchainEvmProviders.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 = {
[xchainClient.Network.Mainnet]: ETH_ONLINE_PROVIDER_MAINNET,
[xchainClient.Network.Testnet]: ETH_ONLINE_PROVIDER_TESTNET,
[xchainClient.Network.Stagenet]: ETH_ONLINE_PROVIDER_MAINNET,
};
// ===== ONLINE providers =====
// ===== Explorers =====
// Mainnet explorer provider
const ETH_MAINNET_EXPLORER = new xchainClient.ExplorerProvider('https://etherscan.io', 'https://etherscan.io/address/%%ADDRESS%%', 'https://etherscan.io/tx/%%TX_ID%%');
// Testnet explorer provider
const ETH_TESTNET_EXPLORER = new xchainClient.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 = {
[xchainClient.Network.Mainnet]: ETH_MAINNET_EXPLORER,
[xchainClient.Network.Testnet]: ETH_TESTNET_EXPLORER,
[xchainClient.Network.Stagenet]: ETH_MAINNET_EXPLORER,
};
// ===== Explorers =====
/**
* Root derivation paths for Ethereum 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/",
};
/**
* Default parameters for Ethereum clients.
*/
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)),
},
};
/**
* Default Ethereum client parameters.
*/
const defaultEthParams = {
chain: ETHChain,
gasAsset: AssetETH,
gasAssetDecimals: ETH_GAS_ASSET_DECIMAL,
defaults,
providers: ethersJSProviders,
explorerProviders: ethExplorerProviders,
dataProviders: [ethProviders],
network: xchainClient.Network.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 xchainEvm.ClientKeystore {
/**
* 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 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 Ethereum EVM client.
* Extends the `XchainEvmClient` class.
*/
class ClientLedger extends xchainEvm.ClientLedger {
/**
* Constructor for the Ethereum EVM client.
* @param {Object} config - Configuration object for the client (optional).
* Defaults to `defaultEthParams` if not provided.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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.AssetETH = AssetETH;
exports.Client = ClientKeystore;
exports.ClientKeystore = ClientKeystore;
exports.ClientLedger = ClientLedger;
exports.ETHChain = ETHChain;
exports.ETH_GAS_ASSET_DECIMAL = ETH_GAS_ASSET_DECIMAL;
exports.LOWER_FEE_BOUND = LOWER_FEE_BOUND;
exports.UPPER_FEE_BOUND = UPPER_FEE_BOUND;
exports.default = ClientKeystore;
exports.defaultEthParams = defaultEthParams;