-
Notifications
You must be signed in to change notification settings - Fork 105
/
utils.go
145 lines (133 loc) · 4.24 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package sender
import (
"fmt"
"math/big"
"github.com/holiman/uint256"
coretypes "github.com/ethereum/go-ethereum/core/types"
)
var (
multiplier = big.NewInt(11500) //nolint:gomnd // its okay.
quotient = big.NewInt(10000) //nolint:gomnd // its okay.
)
// BumpGas bumps the gas on a tx by a 15% increase.
func BumpGas(tx *coretypes.Transaction) *coretypes.Transaction {
var innerTx coretypes.TxData
switch tx.Type() {
case coretypes.DynamicFeeTxType, coretypes.BlobTxType:
// Bump the existing gas tip cap 15% (10% is required but add a buffer to be safe).
bumpedGasTipCap := new(big.Int).Mul(tx.GasTipCap(), multiplier)
bumpedGasTipCap = new(big.Int).Quo(bumpedGasTipCap, quotient)
// Bump the existing gas fee cap 15% (only 10% required but add a buffer to be safe).
bumpedGasFeeCap := new(big.Int).Mul(tx.GasFeeCap(), multiplier)
bumpedGasFeeCap = new(big.Int).Quo(bumpedGasFeeCap, quotient)
if tx.Type() == coretypes.BlobTxType {
// Bump the existing blob gas fee cap 15%. // TODO: verify that this is correct.
bumpedBlobGasFeeCap := new(big.Int).Mul(tx.BlobGasFeeCap(), multiplier)
bumpedBlobGasFeeCap = new(big.Int).Quo(bumpedBlobGasFeeCap, quotient)
innerTx = &coretypes.BlobTx{
Nonce: tx.Nonce(),
To: *tx.To(),
Gas: tx.Gas(),
Value: uint256.MustFromBig(tx.Value()),
Data: tx.Data(),
GasTipCap: uint256.MustFromBig(bumpedGasTipCap),
GasFeeCap: uint256.MustFromBig(bumpedGasFeeCap),
BlobFeeCap: uint256.MustFromBig(bumpedBlobGasFeeCap),
BlobHashes: tx.BlobHashes(),
Sidecar: tx.BlobTxSidecar(),
}
} else {
innerTx = &coretypes.DynamicFeeTx{
ChainID: tx.ChainId(),
Nonce: tx.Nonce(),
GasTipCap: bumpedGasTipCap,
GasFeeCap: bumpedGasFeeCap,
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
}
}
case coretypes.LegacyTxType, coretypes.AccessListTxType:
// Bump the gas price by 15% (10% is required but we add a buffer to be safe).
bumpedGasPrice := new(big.Int).Mul(tx.GasPrice(), multiplier)
bumpedGasPrice = new(big.Int).Quo(bumpedGasPrice, quotient)
if tx.Type() == coretypes.AccessListTxType {
innerTx = &coretypes.AccessListTx{
ChainID: tx.ChainId(),
Nonce: tx.Nonce(),
GasPrice: bumpedGasPrice,
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
AccessList: tx.AccessList(),
}
} else {
innerTx = &coretypes.LegacyTx{
Nonce: tx.Nonce(),
To: tx.To(),
Gas: tx.Gas(),
GasPrice: bumpedGasPrice,
Value: tx.Value(),
Data: tx.Data(),
}
}
default:
panic(fmt.Sprintf("trying to bump gas on unknown tx type (%d)", tx.Type()))
}
return coretypes.NewTx(innerTx)
}
// SetNonce sets the given nonce on a tx.
func SetNonce(tx *coretypes.Transaction, nonce uint64) *coretypes.Transaction {
var innerTx coretypes.TxData
switch tx.Type() {
case coretypes.DynamicFeeTxType:
innerTx = &coretypes.DynamicFeeTx{
ChainID: tx.ChainId(),
Nonce: nonce,
GasTipCap: tx.GasTipCap(),
GasFeeCap: tx.GasFeeCap(),
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
}
case coretypes.LegacyTxType:
innerTx = &coretypes.LegacyTx{
Nonce: nonce,
To: tx.To(),
Gas: tx.Gas(),
GasPrice: tx.GasPrice(),
Value: tx.Value(),
Data: tx.Data(),
}
case coretypes.AccessListTxType:
innerTx = &coretypes.AccessListTx{
ChainID: tx.ChainId(),
Nonce: nonce,
GasPrice: tx.GasPrice(),
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
AccessList: tx.AccessList(),
}
case coretypes.BlobTxType:
innerTx = &coretypes.BlobTx{
Nonce: nonce,
To: *tx.To(),
Gas: tx.Gas(),
Value: uint256.MustFromBig(tx.Value()),
Data: tx.Data(),
GasTipCap: uint256.MustFromBig(tx.GasTipCap()),
GasFeeCap: uint256.MustFromBig(tx.GasFeeCap()),
BlobFeeCap: uint256.MustFromBig(tx.BlobGasFeeCap()),
BlobHashes: tx.BlobHashes(),
Sidecar: tx.BlobTxSidecar(),
}
default:
panic(fmt.Sprintf("trying to set nonce on unknown tx type (%d)", tx.Type()))
}
return coretypes.NewTx(innerTx)
}