flashbots / ethers-provider-flashbots-bundle

Flashbots provider for ethers.js
549 stars 215 forks source link

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

Open gamagire opened 2 years ago

gamagire commented 2 years ago

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
j05u3 commented 2 years ago

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 commented 2 years ago
j05u3 commented 2 years ago

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