Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-readers-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/sdk': patch
---

Fixes a bug in the SDK which would cause the SDK to throw if no tx nonce is provided
25 changes: 23 additions & 2 deletions packages/sdk/src/l2-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ import { toProvider, toNumber, toBigNumber } from './utils'

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.
*
Expand Down Expand Up @@ -57,7 +78,7 @@ export const estimateL1Gas = async (
gasPrice: tx.gasPrice,
type: tx.type,
gasLimit: tx.gasLimit,
nonce: toNumber(tx.nonce as NumberLike),
nonce: await getNonceForTx(l2Provider, tx),
})
)
}
Expand All @@ -81,7 +102,7 @@ export const estimateL1GasCost = async (
gasPrice: tx.gasPrice,
type: tx.type,
gasLimit: tx.gasLimit,
nonce: toNumber(tx.nonce as NumberLike),
nonce: await getNonceForTx(l2Provider, tx),
})
)
}
Expand Down