Skip to content

Commit

Permalink
fix(celo): properly convert incoming params.gas into tx.gasLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Jul 31, 2023
1 parent 21e0c82 commit fa97ee5
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/bin/ethers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -137,6 +143,7 @@ new WalletMiddlewareServer(
mock_filters,
gas_price_factor,
gas_limit_factor,
force_eip_155,
force_eip_1559,
eth_gas_price_factor
)
Expand Down
1 change: 1 addition & 0 deletions src/bin/ethers/infura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
54 changes: 35 additions & 19 deletions src/bin/w3gw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}
Expand All @@ -102,13 +115,16 @@ console.info(
' ',
'$ '
.concat(process.argv[0], ' ')
.concat(process.argv[1], ' [<ecosystem>[:<network>] [custom-rpc-provider-url]]')
.concat(
process.argv[1],
' [<ecosystem>[:<network>] [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) {
Expand Down
11 changes: 8 additions & 3 deletions src/lib/celo/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/ethers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
) {
Expand All @@ -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
)
Expand Down
26 changes: 18 additions & 8 deletions src/lib/ethers/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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}` })
Expand All @@ -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) {
Expand Down Expand Up @@ -339,8 +348,8 @@ class WalletWrapper {
return gasLimit
}

async getNetwork(): Promise<any> {
return `0x${this.chainId.toString(16)}`
async getNetwork (): Promise<any> {
return `0x${this.provider.network.chainId.toString(16)}`
}

/**
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit fa97ee5

Please sign in to comment.