[Application Development] how to get l1gasUsed? #279
-
Did you check the documentation?
Did you check for duplicate questions?
Issue Descriptioni am unsure how to get the l1gasUsed. i've read the docs and even tried the gasOracle but they return different values so i am stuck here. for example this zero data eth transfer tx: following the math in the documentation wouldnt lead to it: then a call to the gasOracle getL1GasUsed returns 1276 which is correct or what am i missing? Additional InformationNo response FeedbackNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Hey @wew6d48g! UPD: See end of this post. Down below I wrote examples of getting gas using the Oracle example and with substraction padding. Honestly, I'm confused about the formula myself lol. I hope someone will explain. You should also use RLP encoding for Tx. package main
import (
"context"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func getL1GasUsed(txBytes []byte) int {
totalGas := 0
for _, b := range txBytes {
if b == 0 {
totalGas += 4
} else {
totalGas += 16
}
}
unsigned := totalGas + 188
return unsigned + (68 * 16)
}
func main() {
client, err := ethclient.Dial("https://rpc.ankr.com/optimism")
if err != nil {
log.Fatal(err)
}
fetchedTransaction, _, err := client.TransactionByHash(
context.Background(),
common.HexToHash("0x60aa868551c437fd510edfbe4166231289e036f822860dd6ec6671b473abbcd8"),
)
if err != nil {
log.Fatal(err)
}
binaryTx, err := fetchedTransaction.MarshalBinary()
if err != nil {
log.Fatal(err)
}
txDataGas := getL1GasUsed(binaryTx)
log.Println("L1GasUsed using Oracle formula:", txDataGas)
log.Println("L1GasUsed without padding:", txDataGas-68*16)
log.Println("L1GasUsed with dynamic padding:", float64(txDataGas-68*16)*0.684)
}Console output: Also, in Oracle, you may notice that at the end of the function, the value 68 * 16 (1088) is added to the gas used. 3140 - 1088 = 2052. uint256 unsigned = total + overhead();
return unsigned + (68 * 16);The documentation explains it like this: "Adds 68 bytes of padding to account for the fact that the input doesnot have a signature." So the explorer removes that padding. I also found an interesting point in the researcher. I will leave out a number of other calculations, but you can check it for yourself. In fee calculation explorer using L1GasUsed from formula, in L1GasUsed field we have value from Oracle but without padding lol: If we using formula from docs: UPD number million: I figured it out, they count the commission in ether correctly, but in l1 gas used they do not apply dynamic scalar (a feature?). as a result, the value from oracle is used there - 1088 (padding 68 bytes) and multiplied by dynamic scalar. the same value is returned by the formula from the documentation. |
Beta Was this translation helpful? Give feedback.
-
|
Hey @wew6d48g, We hope your recent question was resolved to your satisfaction. Your feedback is invaluable and helps us improve our support services. Could you spare a moment to fill out a short feedback survey. Thank you for helping us improve our developer community. |
Beta Was this translation helpful? Give feedback.



Hey @wew6d48g! UPD: See end of this post.
Down below I wrote examples of getting gas using the Oracle example and with substraction padding. Honestly, I'm confused about the formula myself lol. I hope someone will explain. You should also use RLP encoding for Tx.