diff --git a/package.json b/package.json index f9463a3..e41befb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "web3-jsonrpc-gateway", - "version": "1.7.14", + "version": "1.7.19", "description": "A Web3 provider that handles accounts on its own but delegates chain queries to a 3rd party service", "main": "dist/bin/w3gw.js", "files": [ @@ -65,7 +65,7 @@ "meter:mainnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_GAS_PRICE=5000000000000 ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_LIMIT=8000000 node dist/bin/ethers https://rpc.meter.io 9523", "metis:goerli": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_LIMIT=50000000 node dist/bin/ethers https://goerli.gateway.metisdevops.link 8536", "metis:mainnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=false ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_LIMIT=50000000 node dist/bin/ethers https://andromeda.metis.io/?owner=1088 9536", - "moonbeam:moonbase": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=50000000000 ETHERS_GAS_LIMIT=8000000 ETHERS_MOCK_FILTERS=true node dist/bin/ethers https://rpc.testnet.moonbeam.network 8531", + "moonbeam:moonbase": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=50000000000 ETHERS_GAS_LIMIT=8000000 ETHERS_MOCK_FILTERS=true node dist/bin/ethers https://moonbase-alpha.public.blastapi.io 8531", "moonbeam:moonriver": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=50000000000 ETHERS_GAS_LIMIT=14999999 ETHERS_MOCK_FILTERS=true node dist/bin/ethers https://rpc.api.moonriver.moonbeam.network 7531", "moonbeam:mainnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=100000000000 ETHERS_GAS_LIMIT=14999999 ETHERS_MOCK_FILTERS=true node dist/bin/ethers https://rpc.api.moonbeam.network 9531", "okxchain:testnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_LIMIT=8000000 ETHERS_GAS_PRICE_FACTOR=1.1 node dist/bin/ethers https://exchaintestrpc.okex.org 8528", diff --git a/src/bin/ethers/index.ts b/src/bin/ethers/index.ts index 6854c65..3e1aee6 100644 --- a/src/bin/ethers/index.ts +++ b/src/bin/ethers/index.ts @@ -101,6 +101,12 @@ if (process.env.ETHERS_GAS_LIMIT_FACTOR) { gas_limit_factor = parseFloat(process.env.ETHERS_GAS_LIMIT_FACTOR) } +// Optional: force EIP-155 replay-protected transactions +let force_eip_155: boolean = false +if (process.env.ETHERS_FORCE_EIP_155) { + force_eip_155 = JSON.parse(process.env.ETHERS_FORCE_EIP_155) +} + // Optional: force EIP-1559's type 2 transactions let force_eip_1559: boolean = false if (process.env.ETHERS_FORCE_EIP_1559) { @@ -137,6 +143,7 @@ new WalletMiddlewareServer( mock_filters, gas_price_factor, gas_limit_factor, + force_eip_155, force_eip_1559, eth_gas_price_factor ) diff --git a/src/bin/ethers/infura.ts b/src/bin/ethers/infura.ts index 2841941..040defa 100644 --- a/src/bin/ethers/infura.ts +++ b/src/bin/ethers/infura.ts @@ -105,6 +105,7 @@ new WalletMiddlewareServer( false, // mock filters gas_price_factor, gas_limit_factor, + false, // force EIP-155 txs false, // force EIP-1559 txs false // eth gas price factor ) diff --git a/src/bin/w3gw.js b/src/bin/w3gw.js index 9df2013..286ec6e 100644 --- a/src/bin/w3gw.js +++ b/src/bin/w3gw.js @@ -5,31 +5,39 @@ const scripts = require('../../package.json').scripts if (process.argv.length >= 3) { // search for network and launch gateway, if found - let ecosystem + let ecosystem for (var key in scripts) { - if (key.indexOf(":") > -1 && key === process.argv[2]) { + if (key.indexOf(':') > -1 && key === process.argv[2]) { if (process.env.W3GW_SEED_PHRASE || process.env.W3GW_PRIVATE_KEYS) { var cmdline = scripts[key].split(' ') // substitute "node path/to/bin" to "npx w3gw-bin" - var index = cmdline.findIndex(item => item === "node") - if (index > -1) cmdline[index] = "npx" - index = cmdline.findIndex(item => item.startsWith("dist/bin")) - if (index > -1) cmdline[index] = `w3gw-${cmdline[index].split("/").slice(-1)}` + var index = cmdline.findIndex(item => item === 'node') + if (index > -1) cmdline[index] = 'npx' + index = cmdline.findIndex(item => item.startsWith('dist/bin')) + if (index > -1) + cmdline[index] = `w3gw-${cmdline[index].split('/').slice(-1)}` // replace all references to $W3GW_PROVIDER_URL cmdline = cmdline.map(item => { - if (item.indexOf("$W3GW_PROVIDER_KEY") > -1) { + if (item.indexOf('$W3GW_PROVIDER_KEY') > -1) { if (!process.env.W3GW_PROVIDER_KEY) { console.info() - console.info("Cannot launch", key, "gateway: the W3GW_PROVIDER_KEY envar must be set!") + console.info( + 'Cannot launch', + key, + 'gateway: the W3GW_PROVIDER_KEY envar must be set!' + ) process.exit(0) } - return item.replaceAll("$W3GW_PROVIDER_KEY", process.env.W3GW_PROVIDER_KEY) + return item.replaceAll( + '$W3GW_PROVIDER_KEY', + process.env.W3GW_PROVIDER_KEY + ) } else { return item } }) if (process.argv.length >= 4) { - // a specific JSONRPC provider has been specified in the command line: + // a specific JSONRPC provider has been specified in the command line: cmdline[cmdline.length - 2] = process.argv[3] if (process.env.W3GW_PORT) { cmdline[cmdline.length - 1] = process.env.W3GW_PORT @@ -73,11 +81,16 @@ if (process.argv.length >= 3) { process.exit(0) } else { console.info() - console.info("Cannot launch", key, "gateway !!") - console.info("Please, setup the W3GW_SEED_PHRASE environment variable, or add it to the .env file!") + console.info('Cannot launch', key, 'gateway !!') + console.info( + 'Please, setup the W3GW_SEED_PHRASE environment variable, or add it to the .env file!' + ) process.exit(1) } - } else if (key.indexOf(":") && key.split(":")[0].toLowerCase() === process.argv[2].toLowerCase()) { + } else if ( + key.indexOf(':') && + key.split(':')[0].toLowerCase() === process.argv[2].toLowerCase() + ) { ecosystem = process.argv[2].toLowerCase() break } @@ -87,9 +100,9 @@ if (process.argv.length >= 3) { const header = `AVAILABLE NETWORKS ON ${ecosystem.toUpperCase()}` console.info() console.info(header) - console.info("=".repeat(header.length)) + console.info('='.repeat(header.length)) for (var key in scripts) { - if (key.split(":")[0].toLowerCase() === ecosystem) { + if (key.split(':')[0].toLowerCase() === ecosystem) { console.info(' ', key) } } @@ -102,13 +115,16 @@ console.info( ' ', '$ ' .concat(process.argv[0], ' ') - .concat(process.argv[1], ' [[:] [custom-rpc-provider-url]]') + .concat( + process.argv[1], + ' [[:] [custom-rpc-provider-url]]' + ) ) console.info() -const header = "AVAILABLE NETWORKS" -console.info(" ", header) -console.info(" ", "=".repeat(header.length)) +const header = 'AVAILABLE NETWORKS' +console.info(' ', header) +console.info(' ', '='.repeat(header.length)) console.info() for (var key in scripts) { if (key.indexOf(':') > -1) { diff --git a/src/lib/celo/wrapper.ts b/src/lib/celo/wrapper.ts index e27c0d6..adb62d2 100644 --- a/src/lib/celo/wrapper.ts +++ b/src/lib/celo/wrapper.ts @@ -87,7 +87,7 @@ class WalletWrapper { // Compose base transaction: let tx: any = { - from: params.from || this.getAccounts()[0], + from: params.from, // || this.getAccounts()[0], to: params.to, data: params.data, gasPrice, @@ -97,7 +97,12 @@ class WalletWrapper { } // Estimate gas limit, if not specified, but `params.from` is: - const gasLimit = await this.processEthEstimateGas(socket, tx) + let gasLimit = params.gas + if (!gasLimit && params.from) { + gasLimit = await this.processEthEstimateGas(socket, tx) + } else { + gasLimit = params.gas + } tx = { ...tx, gasLimit: gasLimit @@ -123,7 +128,7 @@ class WalletWrapper { }) logger.verbose({ socket, - message: `> Gas limit: ${tx.gasLimit.toString()} gas` + message: `> Gas limit: ${tx.gasLimit.toString()} gas units` }) logger.verbose({ socket, diff --git a/src/lib/ethers/server.ts b/src/lib/ethers/server.ts index 31f0c80..39a6205 100644 --- a/src/lib/ethers/server.ts +++ b/src/lib/ethers/server.ts @@ -16,7 +16,7 @@ class WalletMiddlewareServer { wrapper: WalletWrapper constructor ( - provider: ethers.providers.JsonRpcProvider, + provider: ethers.providers.StaticJsonRpcProvider, seed_phrase: string, interleave_blocks: number, gas_price: number, @@ -28,6 +28,7 @@ class WalletMiddlewareServer { mock_filters: boolean, gas_price_factor: number, gas_limit_factor: number, + force_eip_155: boolean, force_eip_1559: boolean, eth_gas_price_factor: boolean ) { @@ -45,6 +46,7 @@ class WalletMiddlewareServer { estimate_gas_price, gas_price_factor, gas_limit_factor, + force_eip_155, force_eip_1559, eth_gas_price_factor ) diff --git a/src/lib/ethers/wrapper.ts b/src/lib/ethers/wrapper.ts index cb5f0ff..60b54d6 100644 --- a/src/lib/ethers/wrapper.ts +++ b/src/lib/ethers/wrapper.ts @@ -25,12 +25,13 @@ class WalletWrapper { estimateGasLimit: boolean estimateGasPrice: boolean ethGasPriceFactor: boolean + forceEIP155: boolean forceType2Txs: boolean gasPriceFactor!: number gasLimitFactor!: number interleaveBlocks: number lastKnownBlock: number - provider: ethers.providers.JsonRpcProvider + provider: ethers.providers.StaticJsonRpcProvider wallets: Wallet[] constructor ( @@ -44,15 +45,16 @@ class WalletWrapper { estimate_gas_price: boolean, gas_price_factor: number, gas_limit_factor: number, + force_eip_155: boolean, force_eip_1559: boolean, eth_gas_price_factor: boolean ) { - this.chainId = provider.network.chainId this.defaultGasPrice = gas_price this.defaultGasLimit = gas_limit this.estimateGasLimit = estimate_gas_limit this.estimateGasPrice = estimate_gas_price this.ethGasPriceFactor = eth_gas_price_factor + this.forceEIP155 = force_eip_155 this.forceType2Txs = force_eip_1559 this.gasPriceFactor = gas_price_factor this.gasLimitFactor = gas_limit_factor @@ -85,8 +87,13 @@ class WalletWrapper { to: params.to, value: params.value, data: params.data, - nonce: params.nonce, - chainId: this.chainId + nonce: params.nonce + } + if (this.forceEIP155) { + tx = { + ...tx, + chainId: this.provider.network.chainId + } } if (tx.from) { logger.verbose({ socket, message: `> From: ${tx.from}` }) @@ -99,7 +106,9 @@ class WalletWrapper { }` }) logger.verbose({ socket, message: `> Value: ${tx.value || 0} wei` }) - logger.verbose({ socket, message: `> ChainId: ${tx.chainId}` }) + if (this.forceEIP155) { + logger.verbose({ socket, message: `> ChainId: ${tx.chainId}` }) + } // Complete tx type, if necessary: if (this.forceType2Txs) { @@ -339,8 +348,8 @@ class WalletWrapper { return gasLimit } - async getNetwork(): Promise { - return `0x${this.chainId.toString(16)}` + async getNetwork (): Promise { + return `0x${this.provider.network.chainId.toString(16)}` } /** @@ -404,7 +413,8 @@ class WalletWrapper { ) if (this.interleaveBlocks > 0) { // Check for rollbacks, and get block tag: - const blockTag = (await this.checkRollbacks(socket)) - this.interleaveBlocks + const blockTag = + (await this.checkRollbacks(socket)) - this.interleaveBlocks logger.verbose({ socket, message: `> Block tag: ${this.lastKnownBlock} --> ${blockTag}`