github.com/arjunbeliever/ignite@v0.0.0-20220406110515-46bbbbec2587/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/arjunbeliever/ignite/common" 27 "github.com/arjunbeliever/ignite/common/hexutil" 28 "github.com/arjunbeliever/ignite/common/math" 29 "github.com/arjunbeliever/ignite/core/types" 30 "github.com/arjunbeliever/ignite/log" 31 "github.com/arjunbeliever/ignite/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/arjunbeliever/ignite/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 gasFeeCap := new(big.Int).Add( 97 (*big.Int)(args.MaxPriorityFeePerGas), 98 new(big.Int).Mul(head.BaseFee, big.NewInt(2)), 99 ) 100 args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap) 101 } 102 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 103 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 104 } 105 } else { 106 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 107 return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") 108 } 109 if args.GasPrice == nil { 110 price, err := b.SuggestGasTipCap(ctx) 111 if err != nil { 112 return err 113 } 114 if b.ChainConfig().IsLondon(head.Number) { 115 // The legacy tx gas price suggestion should not add 2x base fee 116 // because all fees are consumed, so it would result in a spiral 117 // upwards. 118 price.Add(price, head.BaseFee) 119 } 120 args.GasPrice = (*hexutil.Big)(price) 121 } 122 } 123 } else { 124 // Both maxPriorityfee and maxFee set by caller. Sanity-check their internal relation 125 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 126 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 127 } 128 } 129 if args.Value == nil { 130 args.Value = new(hexutil.Big) 131 } 132 if args.Nonce == nil { 133 nonce, err := b.GetPoolNonce(ctx, args.from()) 134 if err != nil { 135 return err 136 } 137 args.Nonce = (*hexutil.Uint64)(&nonce) 138 } 139 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 140 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 141 } 142 if args.To == nil && len(args.data()) == 0 { 143 return errors.New(`contract creation without any data provided`) 144 } 145 // Estimate the gas usage if necessary. 146 if args.Gas == nil { 147 // These fields are immutable during the estimation, safe to 148 // pass the pointer directly. 149 callArgs := TransactionArgs{ 150 From: args.From, 151 To: args.To, 152 GasPrice: args.GasPrice, 153 MaxFeePerGas: args.MaxFeePerGas, 154 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 155 Value: args.Value, 156 Data: args.Data, 157 AccessList: args.AccessList, 158 } 159 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 160 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 161 if err != nil { 162 return err 163 } 164 args.Gas = &estimated 165 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 166 } 167 if args.ChainID == nil { 168 id := (*hexutil.Big)(b.ChainConfig().ChainID) 169 args.ChainID = id 170 } 171 return nil 172 } 173 174 // ToMessage converts th transaction arguments to the Message type used by the 175 // core evm. This method is used in calls and traces that do not require a real 176 // live transaction. 177 func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) { 178 // Reject invalid combinations of pre- and post-1559 fee styles 179 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 180 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 181 } 182 // Set sender address or use zero address if none specified. 183 addr := args.from() 184 185 // Set default gas & gas price if none were set 186 gas := globalGasCap 187 if gas == 0 { 188 gas = uint64(math.MaxUint64 / 2) 189 } 190 if args.Gas != nil { 191 gas = uint64(*args.Gas) 192 } 193 if globalGasCap != 0 && globalGasCap < gas { 194 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 195 gas = globalGasCap 196 } 197 var ( 198 gasPrice *big.Int 199 gasFeeCap *big.Int 200 gasTipCap *big.Int 201 ) 202 if baseFee == nil { 203 // If there's no basefee, then it must be a non-1559 execution 204 gasPrice = new(big.Int) 205 if args.GasPrice != nil { 206 gasPrice = args.GasPrice.ToInt() 207 } 208 gasFeeCap, gasTipCap = gasPrice, gasPrice 209 } else { 210 // A basefee is provided, necessitating 1559-type execution 211 if args.GasPrice != nil { 212 // User specified the legacy gas field, convert to 1559 gas typing 213 gasPrice = args.GasPrice.ToInt() 214 gasFeeCap, gasTipCap = gasPrice, gasPrice 215 } else { 216 // User specified 1559 gas feilds (or none), use those 217 gasFeeCap = new(big.Int) 218 if args.MaxFeePerGas != nil { 219 gasFeeCap = args.MaxFeePerGas.ToInt() 220 } 221 gasTipCap = new(big.Int) 222 if args.MaxPriorityFeePerGas != nil { 223 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 224 } 225 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 226 gasPrice = new(big.Int) 227 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 228 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 229 } 230 } 231 } 232 value := new(big.Int) 233 if args.Value != nil { 234 value = args.Value.ToInt() 235 } 236 data := args.data() 237 var accessList types.AccessList 238 if args.AccessList != nil { 239 accessList = *args.AccessList 240 } 241 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, false) 242 return msg, nil 243 } 244 245 // toTransaction converts the arguments to a transaction. 246 // This assumes that setDefaults has been called. 247 func (args *TransactionArgs) toTransaction() *types.Transaction { 248 var data types.TxData 249 switch { 250 case args.MaxFeePerGas != nil: 251 al := types.AccessList{} 252 if args.AccessList != nil { 253 al = *args.AccessList 254 } 255 data = &types.DynamicFeeTx{ 256 To: args.To, 257 ChainID: (*big.Int)(args.ChainID), 258 Nonce: uint64(*args.Nonce), 259 Gas: uint64(*args.Gas), 260 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 261 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 262 Value: (*big.Int)(args.Value), 263 Data: args.data(), 264 AccessList: al, 265 } 266 case args.AccessList != nil: 267 data = &types.AccessListTx{ 268 To: args.To, 269 ChainID: (*big.Int)(args.ChainID), 270 Nonce: uint64(*args.Nonce), 271 Gas: uint64(*args.Gas), 272 GasPrice: (*big.Int)(args.GasPrice), 273 Value: (*big.Int)(args.Value), 274 Data: args.data(), 275 AccessList: *args.AccessList, 276 } 277 default: 278 data = &types.LegacyTx{ 279 To: args.To, 280 Nonce: uint64(*args.Nonce), 281 Gas: uint64(*args.Gas), 282 GasPrice: (*big.Int)(args.GasPrice), 283 Value: (*big.Int)(args.Value), 284 Data: args.data(), 285 } 286 } 287 return types.NewTx(data) 288 } 289 290 // ToTransaction converts the arguments to a transaction. 291 // This assumes that setDefaults has been called. 292 func (args *TransactionArgs) ToTransaction() *types.Transaction { 293 return args.toTransaction() 294 }