Skip to content

Commit

Permalink
fix(l2geth): do not throw an error when estimating gas for txs with n…
Browse files Browse the repository at this point in the history
…il data (#940)

* fix(l2geth): do not throw an error when estimating gas for txs with nil data

* chore: add changeset
  • Loading branch information
gakonst committed May 24, 2021
1 parent 467d6cb commit c880043
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-crabs-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/l2geth': patch
---

Fix gas estimation logic for simple ETH transfers
8 changes: 8 additions & 0 deletions integration-tests/test/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ describe('Basic RPC tests', () => {
})

describe('eth_estimateGas (returns the fee)', () => {
it('should return a gas estimate for txs with empty data', async () => {
const estimate = await l2Provider.estimateGas({
to: DEFAULT_TRANSACTION.to,
value: 0,
})
expect(estimate).to.be.eq(21000)
})

it('should return a gas estimate that grows with the size of data', async () => {
const dataLen = [0, 2, 8, 64, 256]
const l1GasPrice = await env.l1Wallet.provider.getGasPrice()
Expand Down
12 changes: 7 additions & 5 deletions l2geth/internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,6 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
// fees can compensate for the additional costs the sequencer pays for publishing the
// transaction calldata
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap *big.Int) (hexutil.Uint64, error) {
if args.Data == nil {
return 0, errors.New("transaction data cannot be nil")
}

// 1. get the gas that would be used by the transaction
gasUsed, err := legacyDoEstimateGas(ctx, b, args, blockNrOrHash, gasCap)
if err != nil {
Expand All @@ -1056,7 +1052,13 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
}

// 3. calculate the fee and normalize by the default gas price
fee := core.CalculateRollupFee(*args.Data, uint64(gasUsed), dataPrice, executionPrice).Uint64() / defaultGasPrice
var data []byte
if args.Data == nil {
data = []byte{}
} else {
data = *args.Data
}
fee := core.CalculateRollupFee(data, uint64(gasUsed), dataPrice, executionPrice).Uint64() / defaultGasPrice
if fee < 21000 {
fee = 21000
}
Expand Down

0 comments on commit c880043

Please sign in to comment.