ethers-provider-flashbots-bundle icon indicating copy to clipboard operation
ethers-provider-flashbots-bundle copied to clipboard

calculateBundlePricing returns incorrect (negative gas for EIP-1559 tx)

Open gamagire opened this issue 3 years ago • 3 comments

gasPricePaidBySearcher here is the priority fee -- it's already 'above' the base fee so subtracting base gas makes it go negative...

  private calculateBundlePricing(
    bundleTransactions: Array<BlocksApiResponseTransactionDetails | TransactionSimulation>,
    baseFee: BigNumber
  ) {
    console.log('DBG__ bundle Transactions = ', bundleTransactions);
    const bundleGasPricing = bundleTransactions.reduce(
      (acc, transactionDetail) => {
        const gasUsed = 'gas_used' in transactionDetail ? transactionDetail.gas_used : transactionDetail.gasUsed
        const gasPricePaidBySearcher = BigNumber.from(
          'gas_price' in transactionDetail ? transactionDetail.gas_price : transactionDetail.gasPrice
        )
        const priorityFeeReceivedByMiner = gasPricePaidBySearcher.sub(baseFee)  <<<<<<------------------- THIS is incorrect

gamagire avatar Dec 13 '21 20:12 gamagire

This is an example of what I received in the eth_callBundle endpoint:

{
  "coinbaseDiff": "21993659553843018",
  "ethSentToCoinbase": "21993659553647165",
  "fromAddress": "", // removed
  "gasFees": "195853",
  "gasPrice": "112296771322",
  "gasUsed": 195853,
  "toAddress": "", // removed
  "txHash": "0x436a9d8091a17637f7d2c6f0421bf2d13c8a0a64891c1fef7d02a7620e5b53a2",
  "value": "0x"
}

It looks like gasPrice is calculated: ethSentToCoinbase / gasUsed:

21993659553647165 / 195853 = 112296771321.58897

Blocknumber: 14636777 Note: my txn is also EIP-1559

j05u3 avatar Apr 25 '22 15:04 j05u3

  • gasFees matches the product of maxPriorityFee * gasUsed (I sent a maxPriorityFee = 1wei)
  • ethSentToCoinbase matches what I transferred to block.coinbase

j05u3 avatar Apr 25 '22 15:04 j05u3

btw, I found on the last version of mev-geth that gasPrice is actually coinbaseDiff divided by gasUsed:

jsonResult["gasPrice"] = new(big.Int).Div(coinbaseDiffTx, big.NewInt(int64(receipt.GasUsed))).String()

we can also see that:

jsonResult["ethSentToCoinbase"] = new(big.Int).Sub(coinbaseDiffTx, gasFeesTx).String()

and gasFees (variable gasFeesTx) is the total tip paid to the miner through gas fees (I guess you can also call this priority fee)

so coinbaseDiff is the sum of ethSentToCoinbase plus gasFees

Source: https://github.com/flashbots/mev-geth/blob/v1.10.17-mev0.6.1/internal/ethapi/api.go#L2391

j05u3 avatar Apr 25 '22 18:04 j05u3