@@ -11,12 +11,9 @@ import (
11
11
// transaction in gas.
12
12
const overhead uint64 = 4200 + 200 * params .TxDataNonZeroGasEIP2028
13
13
14
- // hundredMillion is a constant used in the gas encoding formula
15
- const hundredMillion uint64 = 100_000_000
16
-
17
14
// feeScalar is used to scale the calculations in EncodeL2GasLimit
18
15
// to prevent them from being too large
19
- const feeScalar uint64 = 1000
16
+ const feeScalar uint64 = 10_000_000
20
17
21
18
// TxGasPrice is a constant that determines the result of `eth_gasPrice`
22
19
// It is scaled upwards by 50%
@@ -27,8 +24,10 @@ const TxGasPrice uint64 = feeScalar + (feeScalar / 2)
27
24
// BigTxGasPrice is the L2GasPrice as type big.Int
28
25
var BigTxGasPrice = new (big.Int ).SetUint64 (TxGasPrice )
29
26
var bigFeeScalar = new (big.Int ).SetUint64 (feeScalar )
30
- var bigHundredMillion = new (big.Int ).SetUint64 (hundredMillion )
31
- var BigTenThousand = new (big.Int ).SetUint64 (10000 )
27
+
28
+ const tenThousand = 10000
29
+
30
+ var BigTenThousand = new (big.Int ).SetUint64 (tenThousand )
32
31
33
32
// EncodeTxGasLimit computes the `tx.gasLimit` based on the L1/L2 gas prices and
34
33
// the L2 gas limit. The L2 gas limit is encoded inside of the lower order bits
@@ -42,12 +41,14 @@ var BigTenThousand = new(big.Int).SetUint64(10000)
42
41
// the fee, so increasing the L2 Gas limit will increase the fee paid.
43
42
// The calculation is:
44
43
// l1GasLimit = zero_count(data) * 4 + non_zero_count(data) * 16 + overhead
44
+ // roundedL2GasLimit = ceilmod(l2GasLimit, 10_000)
45
45
// l1Fee = l1GasPrice * l1GasLimit
46
- // l2Fee = l2GasPrice * l2GasLimit
46
+ // l2Fee = l2GasPrice * roundedL2GasLimit
47
47
// sum = l1Fee + l2Fee
48
48
// scaled = sum / scalar
49
- // rounded = ceilmod(scaled, hundredMillion)
50
- // result = rounded + l2GasLimit
49
+ // rounded = ceilmod(scaled, tenThousand)
50
+ // roundedScaledL2GasLimit = roundedL2GasLimit / tenThousand
51
+ // result = rounded + roundedScaledL2GasLimit
51
52
// Note that for simplicity purposes, only the calldata is passed into this
52
53
// function when in reality the RLP encoded transaction should be. The
53
54
// additional cost is added to the overhead constant to prevent the need to RLP
@@ -59,7 +60,7 @@ func EncodeTxGasLimit(data []byte, l1GasPrice, l2GasLimit, l2GasPrice *big.Int)
59
60
l2Fee := new (big.Int ).Mul (l2GasPrice , roundedL2GasLimit )
60
61
sum := new (big.Int ).Add (l1Fee , l2Fee )
61
62
scaled := new (big.Int ).Div (sum , bigFeeScalar )
62
- rounded := Ceilmod (scaled , bigHundredMillion )
63
+ rounded := Ceilmod (scaled , BigTenThousand )
63
64
roundedScaledL2GasLimit := new (big.Int ).Div (roundedL2GasLimit , BigTenThousand )
64
65
result := new (big.Int ).Add (rounded , roundedScaledL2GasLimit )
65
66
return result
@@ -81,6 +82,11 @@ func DecodeL2GasLimit(gasLimit *big.Int) *big.Int {
81
82
return new (big.Int ).Mul (scaled , BigTenThousand )
82
83
}
83
84
85
+ func DecodeL2GasLimitU64 (gasLimit uint64 ) uint64 {
86
+ scaled := gasLimit % tenThousand
87
+ return scaled * tenThousand
88
+ }
89
+
84
90
// calculateL1GasLimit computes the L1 gasLimit based on the calldata and
85
91
// constant sized overhead. The overhead can be decreased as the cost of the
86
92
// batch submission goes down via contract optimizations. This will not overflow
0 commit comments