github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/internal/ethapi/transaction_args.go (about) 1 // Copyright 2021 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethapi 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "math/big" 25 26 "github.com/scroll-tech/go-ethereum/common" 27 "github.com/scroll-tech/go-ethereum/common/hexutil" 28 "github.com/scroll-tech/go-ethereum/common/math" 29 "github.com/scroll-tech/go-ethereum/core/types" 30 "github.com/scroll-tech/go-ethereum/log" 31 "github.com/scroll-tech/go-ethereum/rpc" 32 ) 33 34 // TransactionArgs represents the arguments to construct a new transaction 35 // or a message call. 36 type TransactionArgs struct { 37 From *common.Address `json:"from"` 38 To *common.Address `json:"to"` 39 Gas *hexutil.Uint64 `json:"gas"` 40 GasPrice *hexutil.Big `json:"gasPrice"` 41 MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"` 42 MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"` 43 Value *hexutil.Big `json:"value"` 44 Nonce *hexutil.Uint64 `json:"nonce"` 45 46 // We accept "data" and "input" for backwards-compatibility reasons. 47 // "input" is the newer name and should be preferred by clients. 48 // Issue detail: https://github.com/scroll-tech/go-ethereum/issues/15628 49 Data *hexutil.Bytes `json:"data"` 50 Input *hexutil.Bytes `json:"input"` 51 52 // Introduced by AccessListTxType transaction. 53 AccessList *types.AccessList `json:"accessList,omitempty"` 54 ChainID *hexutil.Big `json:"chainId,omitempty"` 55 } 56 57 // from retrieves the transaction sender address. 58 func (arg *TransactionArgs) from() common.Address { 59 if arg.From == nil { 60 return common.Address{} 61 } 62 return *arg.From 63 } 64 65 // data retrieves the transaction calldata. Input field is preferred. 66 func (arg *TransactionArgs) data() []byte { 67 if arg.Input != nil { 68 return *arg.Input 69 } 70 if arg.Data != nil { 71 return *arg.Data 72 } 73 return nil 74 } 75 76 // setDefaults fills in default values for unspecified tx fields. 77 func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { 78 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 79 return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 80 } 81 // After london, default to 1559 unless gasPrice is set 82 head := b.CurrentHeader() 83 // If user specifies both maxPriorityfee and maxFee, then we do not 84 // need to consult the chain for defaults. It's definitely a London tx. 85 if args.MaxPriorityFeePerGas == nil || args.MaxFeePerGas == nil { 86 // In this clause, user left some fields unspecified. 87 if b.ChainConfig().IsLondon(head.Number) && args.GasPrice == nil { 88 if args.MaxPriorityFeePerGas == nil { 89 tip, err := b.SuggestGasTipCap(ctx) 90 if err != nil { 91 return err 92 } 93 args.MaxPriorityFeePerGas = (*hexutil.Big)(tip) 94 } 95 if args.MaxFeePerGas == nil { 96 var gasFeeCap *big.Int 97 if head.BaseFee != nil { 98 gasFeeCap = new(big.Int).Add( 99 (*big.Int)(args.MaxPriorityFeePerGas), 100 new(big.Int).Mul(head.BaseFee, big.NewInt(2)), 101 ) 102 } else { 103 gasFeeCap = new(big.Int).Set( 104 (*big.Int)(args.MaxPriorityFeePerGas)) 105 } 106 args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap) 107 } 108 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 109 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 110 } 111 } else { 112 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 113 return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") 114 } 115 if args.GasPrice == nil { 116 price, err := b.SuggestGasTipCap(ctx) 117 if err != nil { 118 return err 119 } 120 if b.ChainConfig().IsLondon(head.Number) { 121 // The legacy tx gas price suggestion should not add 2x base fee 122 // because all fees are consumed, so it would result in a spiral 123 // upwards. 124 if head.BaseFee != nil { 125 price.Add(price, head.BaseFee) 126 } 127 } 128 args.GasPrice = (*hexutil.Big)(price) 129 } 130 } 131 } else { 132 // Both maxPriorityfee and maxFee set by caller. Sanity-check their internal relation 133 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 134 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 135 } 136 } 137 if args.Value == nil { 138 args.Value = new(hexutil.Big) 139 } 140 if args.Nonce == nil { 141 nonce, err := b.GetPoolNonce(ctx, args.from()) 142 if err != nil { 143 return err 144 } 145 args.Nonce = (*hexutil.Uint64)(&nonce) 146 } 147 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 148 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 149 } 150 if args.To == nil && len(args.data()) == 0 { 151 return errors.New(`contract creation without any data provided`) 152 } 153 // Estimate the gas usage if necessary. 154 if args.Gas == nil { 155 // These fields are immutable during the estimation, safe to 156 // pass the pointer directly. 157 data := args.data() 158 callArgs := TransactionArgs{ 159 From: args.From, 160 To: args.To, 161 GasPrice: args.GasPrice, 162 MaxFeePerGas: args.MaxFeePerGas, 163 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 164 Value: args.Value, 165 Data: (*hexutil.Bytes)(&data), 166 AccessList: args.AccessList, 167 } 168 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 169 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 170 if err != nil { 171 return err 172 } 173 args.Gas = &estimated 174 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 175 } 176 if args.ChainID == nil { 177 id := (*hexutil.Big)(b.ChainConfig().ChainID) 178 args.ChainID = id 179 } 180 return nil 181 } 182 183 // ToMessage converts the transaction arguments to the Message type used by the 184 // core evm. This method is used in calls and traces that do not require a real 185 // live transaction. 186 func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) { 187 // Reject invalid combinations of pre- and post-1559 fee styles 188 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 189 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 190 } 191 // Set sender address or use zero address if none specified. 192 addr := args.from() 193 194 // Set default gas & gas price if none were set 195 gas := globalGasCap 196 if gas == 0 { 197 gas = uint64(math.MaxUint64 / 2) 198 } 199 if args.Gas != nil { 200 gas = uint64(*args.Gas) 201 } 202 if globalGasCap != 0 && globalGasCap < gas { 203 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 204 gas = globalGasCap 205 } 206 var ( 207 gasPrice *big.Int 208 gasFeeCap *big.Int 209 gasTipCap *big.Int 210 ) 211 if baseFee == nil { 212 // If there's no basefee, then it must be a non-1559 execution 213 gasPrice = new(big.Int) 214 if args.GasPrice != nil { 215 gasPrice = args.GasPrice.ToInt() 216 } 217 gasFeeCap, gasTipCap = gasPrice, gasPrice 218 } else { 219 // A basefee is provided, necessitating 1559-type execution 220 if args.GasPrice != nil { 221 // User specified the legacy gas field, convert to 1559 gas typing 222 gasPrice = args.GasPrice.ToInt() 223 gasFeeCap, gasTipCap = gasPrice, gasPrice 224 } else { 225 // User specified 1559 gas feilds (or none), use those 226 gasFeeCap = new(big.Int) 227 if args.MaxFeePerGas != nil { 228 gasFeeCap = args.MaxFeePerGas.ToInt() 229 } 230 gasTipCap = new(big.Int) 231 if args.MaxPriorityFeePerGas != nil { 232 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 233 } 234 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 235 gasPrice = new(big.Int) 236 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 237 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 238 } 239 } 240 } 241 value := new(big.Int) 242 if args.Value != nil { 243 value = args.Value.ToInt() 244 } 245 data := args.data() 246 var accessList types.AccessList 247 if args.AccessList != nil { 248 accessList = *args.AccessList 249 } 250 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true) 251 return msg, nil 252 } 253 254 // toTransaction converts the arguments to a transaction. 255 // This assumes that setDefaults has been called. 256 func (args *TransactionArgs) toTransaction() *types.Transaction { 257 var data types.TxData 258 switch { 259 case args.MaxFeePerGas != nil: 260 al := types.AccessList{} 261 if args.AccessList != nil { 262 al = *args.AccessList 263 } 264 data = &types.DynamicFeeTx{ 265 To: args.To, 266 ChainID: (*big.Int)(args.ChainID), 267 Nonce: uint64(*args.Nonce), 268 Gas: uint64(*args.Gas), 269 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 270 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 271 Value: (*big.Int)(args.Value), 272 Data: args.data(), 273 AccessList: al, 274 } 275 case args.AccessList != nil: 276 data = &types.AccessListTx{ 277 To: args.To, 278 ChainID: (*big.Int)(args.ChainID), 279 Nonce: uint64(*args.Nonce), 280 Gas: uint64(*args.Gas), 281 GasPrice: (*big.Int)(args.GasPrice), 282 Value: (*big.Int)(args.Value), 283 Data: args.data(), 284 AccessList: *args.AccessList, 285 } 286 default: 287 data = &types.LegacyTx{ 288 To: args.To, 289 Nonce: uint64(*args.Nonce), 290 Gas: uint64(*args.Gas), 291 GasPrice: (*big.Int)(args.GasPrice), 292 Value: (*big.Int)(args.Value), 293 Data: args.data(), 294 } 295 } 296 return types.NewTx(data) 297 } 298 299 // ToTransaction converts the arguments to a transaction. 300 // This assumes that setDefaults has been called. 301 func (args *TransactionArgs) ToTransaction() *types.Transaction { 302 return args.toTransaction() 303 }