Skip to content

Commit 434a7d1

Browse files
committed
feat: CopyAndDefault method for Gas added
1 parent 55f1579 commit 434a7d1

3 files changed

Lines changed: 64 additions & 49 deletions

File tree

client/config/client.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,46 @@ func DefaultGas() Gas {
281281
}
282282
}
283283

284+
// CopyAndDefault copy Gas and sets default values for any unset configs.
285+
func (g *Gas) CopyAndDefault() *Gas {
286+
switch g.TxType {
287+
case 0:
288+
return &Gas{
289+
TxType: 0,
290+
GasLimit: g.GasLimit,
291+
GasPriceMultiplier: g.GasPriceMultiplier,
292+
GasPriceFixed: g.GasPriceFixed,
293+
}
294+
default: // TxType 2
295+
newGas := &Gas{
296+
TxType: 2,
297+
GasLimit: g.GasLimit,
298+
BaseFeePerGasCap: g.BaseFeePerGasCap,
299+
}
300+
if g.MaxPriorityMultiplier != nil {
301+
newGas.MaxPriorityMultiplier = g.MaxPriorityMultiplier
302+
} else {
303+
newGas.MaxPriorityMultiplier = DefaultMaxPriorityMultiplier
304+
}
305+
if g.MinimalMaxPriorityFee != nil {
306+
newGas.MinimalMaxPriorityFee = g.MinimalMaxPriorityFee
307+
} else {
308+
newGas.MinimalMaxPriorityFee = DefaultMinimalMaxPriorityFee
309+
}
310+
if g.MaximalMaxPriorityFee != nil {
311+
newGas.MaximalMaxPriorityFee = g.MaximalMaxPriorityFee
312+
} else {
313+
newGas.MaximalMaxPriorityFee = DefaultMaximalMaxPriorityFee
314+
}
315+
if g.BaseFeeMultiplier != nil {
316+
newGas.BaseFeeMultiplier = g.BaseFeeMultiplier
317+
} else {
318+
newGas.BaseFeeMultiplier = DefaultBaseFeeMultiplier
319+
}
320+
return newGas
321+
}
322+
}
323+
284324
// EnforceMaxPriorityFeeCaps returns capped fee.
285325
// A new value is returned and the underlying value of the fee is unchanged/
286326
func (g *Gas) EnforceMaxPriorityFeeCaps(fee *big.Int) *big.Int {
@@ -305,7 +345,6 @@ func (g *Gas) validate() error {
305345

306346
switch g.TxType {
307347
case 0:
308-
309348
if g.GasPriceFixed.Cmp(common.Big0) != 0 && g.GasPriceMultiplier != 0.0 {
310349
return errors.New("only one of gas_price_fixed and gas_price_multiplier can be set to a non-zero value for type 0 transaction")
311350
}

utils/chain/tx_utils.go

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ func GetGasPrice(gasConfig *config.Gas, client *ethclient.Client, timeout time.D
336336
//
337337
// For type 2 transaction on i-th attempt,
338338
// the multipliers are increased by i, and caps are increased by 11% per attempt.
339+
//
340+
// Any unset values will be set to default.
339341
func GasConfigForAttempt(cfg *config.Gas, attempt int) *config.Gas {
340342
switch cfg.TxType {
341343
case 0:
@@ -352,63 +354,26 @@ func GasConfigForAttempt(cfg *config.Gas, attempt int) *config.Gas {
352354
GasPriceMultiplier: max(1.0, cfg.GasPriceMultiplier) * float32(retryMultiplier),
353355
GasPriceFixed: cfg.GasPriceFixed,
354356
}
355-
case 2:
357+
default: // type 2 and invalid types
356358
attemptBig := big.NewInt(int64(attempt))
357359

358-
maxPriorityMultiplier := new(big.Int)
359-
if cfg.MaxPriorityMultiplier != nil && cfg.MaxPriorityMultiplier.Cmp(common.Big0) == 1 {
360-
maxPriorityMultiplier.Set(cfg.MaxPriorityMultiplier)
361-
maxPriorityMultiplier.Add(maxPriorityMultiplier, attemptBig)
362-
} else {
363-
maxPriorityMultiplier.SetInt64(defaultTipMultiplier + int64(attempt))
364-
}
365-
366-
baseFeeMultiplier := new(big.Int)
367-
if cfg.BaseFeeMultiplier != nil && cfg.BaseFeeMultiplier.Cmp(common.Big0) == 1 {
368-
baseFeeMultiplier.Set(cfg.BaseFeeMultiplier)
369-
baseFeeMultiplier.Add(baseFeeMultiplier, attemptBig)
370-
} else {
371-
baseFeeMultiplier.Add(config.DefaultBaseFeeMultiplier, attemptBig)
372-
}
373-
374-
maxMaxPriorityFee := new(big.Int)
375-
if cfg.MaximalMaxPriorityFee != nil && cfg.MaximalMaxPriorityFee.Cmp(common.Big0) == 1 {
376-
maxMaxPriorityFee.Set(cfg.MaximalMaxPriorityFee)
377-
} else {
378-
maxMaxPriorityFee.Set(config.DefaultMaximalMaxPriorityFee)
379-
}
360+
c := cfg.CopyAndDefault()
380361

381-
minMaxPriorityFee := new(big.Int)
382-
if cfg.MinimalMaxPriorityFee != nil && cfg.MinimalMaxPriorityFee.Cmp(common.Big0) == 1 {
383-
minMaxPriorityFee.Set(cfg.MinimalMaxPriorityFee)
384-
} else {
385-
minMaxPriorityFee.Set(config.DefaultMinimalMaxPriorityFee)
386-
}
362+
c.MaxPriorityMultiplier.Add(c.MaxPriorityMultiplier, attemptBig)
363+
c.BaseFeeMultiplier.Add(c.BaseFeeMultiplier, attemptBig)
387364

388365
// Increase caps by 11% per retry
389366
if attempt > 0 {
390367
multiplier := new(big.Int).Exp(big.NewInt(multiplierBumpTimes100), attemptBig, nil)
391368
normalizer := new(big.Int).Exp(big.NewInt(normalizer), attemptBig, nil)
392369

393-
maxMaxPriorityFee.Mul(maxMaxPriorityFee, multiplier)
394-
maxMaxPriorityFee.Div(maxMaxPriorityFee, normalizer)
370+
c.MaximalMaxPriorityFee.Mul(c.MaximalMaxPriorityFee, multiplier)
371+
c.MaximalMaxPriorityFee.Div(c.MaximalMaxPriorityFee, normalizer)
395372

396-
minMaxPriorityFee.Mul(minMaxPriorityFee, multiplier)
397-
minMaxPriorityFee.Div(minMaxPriorityFee, normalizer)
373+
c.MinimalMaxPriorityFee.Mul(c.MinimalMaxPriorityFee, multiplier)
374+
c.MinimalMaxPriorityFee.Div(c.MinimalMaxPriorityFee, normalizer)
398375
}
399376

400-
return &config.Gas{
401-
TxType: 2,
402-
GasLimit: cfg.GasLimit,
403-
404-
BaseFeeMultiplier: baseFeeMultiplier,
405-
MaxPriorityMultiplier: maxPriorityMultiplier,
406-
MaximalMaxPriorityFee: maxMaxPriorityFee,
407-
MinimalMaxPriorityFee: minMaxPriorityFee,
408-
409-
BaseFeePerGasCap: cfg.BaseFeePerGasCap,
410-
}
411-
default:
412-
return cfg
377+
return c
413378
}
414379
}

utils/chain/tx_utils_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,20 @@ func TestGasConfigForAttemptType0(t *testing.T) {
144144
TxType: 2,
145145
MaxPriorityMultiplier: big.NewInt(0),
146146
BaseFeeMultiplier: big.NewInt(0),
147+
BaseFeePerGasCap: big.NewInt(0),
148+
MaximalMaxPriorityFee: big.NewInt(0),
149+
MinimalMaxPriorityFee: big.NewInt(0),
150+
},
151+
ri: 0,
152+
expected: config2.Gas{
153+
TxType: 2,
154+
GasLimit: 0,
155+
MaxPriorityMultiplier: big.NewInt(0),
156+
BaseFeeMultiplier: big.NewInt(0),
157+
BaseFeePerGasCap: big.NewInt(0),
158+
MaximalMaxPriorityFee: big.NewInt(0),
159+
MinimalMaxPriorityFee: big.NewInt(0),
147160
},
148-
ri: 0,
149-
expected: config2.DefaultGas(),
150161
},
151162
{
152163
name: "zero type 2,2 ",

0 commit comments

Comments
 (0)