Commit 86901552 authored by smartcontracts's avatar smartcontracts Committed by GitHub

fix(sdk): bug causing error when no tx nonce given (#2556)

Fixes a bug in the SDK that would throw an error when the user did not
provide a nonce along with the transaction (when estimating gas). If no
nonce is provided, will try to find a reasonable nonce or use a default
(large) nonce that will give a good upper bound for the L1 gas cost.
parent 7b89c072
---
'@eth-optimism/sdk': patch
---
Fixes a bug in the SDK which would cause the SDK to throw if no tx nonce is provided
...@@ -11,6 +11,27 @@ import { toProvider, toNumber, toBigNumber } from './utils' ...@@ -11,6 +11,27 @@ import { toProvider, toNumber, toBigNumber } from './utils'
type ProviderTypeIsWrong = any type ProviderTypeIsWrong = any
/**
* Gets a reasonable nonce for the transaction.
*
* @param provider Provider to get the nonce from.
* @param tx Requested transaction.
* @returns A reasonable nonce for the transaction.
*/
const getNonceForTx = async (
provider: ProviderLike,
tx: TransactionRequest
): Promise<number> => {
if (tx.nonce !== undefined) {
return toNumber(tx.nonce as NumberLike)
} else if (tx.from !== undefined) {
return toProvider(provider).getTransactionCount(tx.from)
} else {
// Large nonce with lots of non-zero bytes
return 0xffffffff
}
}
/** /**
* Returns a Contract object for the GasPriceOracle. * Returns a Contract object for the GasPriceOracle.
* *
...@@ -57,7 +78,7 @@ export const estimateL1Gas = async ( ...@@ -57,7 +78,7 @@ export const estimateL1Gas = async (
gasPrice: tx.gasPrice, gasPrice: tx.gasPrice,
type: tx.type, type: tx.type,
gasLimit: tx.gasLimit, gasLimit: tx.gasLimit,
nonce: toNumber(tx.nonce as NumberLike), nonce: await getNonceForTx(l2Provider, tx),
}) })
) )
} }
...@@ -81,7 +102,7 @@ export const estimateL1GasCost = async ( ...@@ -81,7 +102,7 @@ export const estimateL1GasCost = async (
gasPrice: tx.gasPrice, gasPrice: tx.gasPrice,
type: tx.type, type: tx.type,
gasLimit: tx.gasLimit, gasLimit: tx.gasLimit,
nonce: toNumber(tx.nonce as NumberLike), nonce: await getNonceForTx(l2Provider, tx),
}) })
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment