Skip to content

Commit 48371fb

Browse files
chore: Use functional options pattern
1 parent 1dd8f66 commit 48371fb

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

devnet-sdk/system/periphery/go-ethereum/fees.go

+38-16
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,55 @@ type EIP1559FeeEstimator struct {
2727
// Access to the Ethereum client is needed to get the fee information from the chain
2828
client EIP1159FeeEthClient
2929

30+
options eip1559FeeEstimatorOptions
31+
}
32+
33+
type eip1559FeeEstimatorOptions struct {
3034
// The base multiplier is used to increase the maxFeePerGas (GasFeeCap) by a factor
3135
baseMultiplier float64
3236

3337
// The tip multiplier is used to increase the maxPriorityFeePerGas (GasTipCap) by a factor
3438
tipMultiplier float64
3539
}
3640

37-
func NewEIP1559FeeEstimator(client EIP1159FeeEthClient) *EIP1559FeeEstimator {
38-
return &EIP1559FeeEstimator{
39-
client: client,
40-
baseMultiplier: 1.0,
41-
tipMultiplier: 1.0,
42-
}
41+
type EIP1559FeeEstimatorOption interface {
42+
apply(*eip1559FeeEstimatorOptions)
43+
}
44+
45+
type eip1559FeeEstimatorOptionBaseMultiplier float64
46+
47+
func (o eip1559FeeEstimatorOptionBaseMultiplier) apply(opts *eip1559FeeEstimatorOptions) {
48+
opts.baseMultiplier = float64(o)
49+
}
50+
51+
func WithEIP1559BaseMultiplier(multiplier float64) EIP1559FeeEstimatorOption {
52+
return eip1559FeeEstimatorOptionBaseMultiplier(multiplier)
4353
}
4454

45-
func (f *EIP1559FeeEstimator) WithBaseMultiplier(multiplier float64) *EIP1559FeeEstimator {
46-
newF := *f
47-
newF.baseMultiplier = multiplier
55+
type eip1559FeeEstimatorOptionTipMultiplier float64
56+
57+
func (o eip1559FeeEstimatorOptionTipMultiplier) apply(opts *eip1559FeeEstimatorOptions) {
58+
opts.tipMultiplier = float64(o)
59+
}
4860

49-
return &newF
61+
func WithEIP1559TipMultiplier(multiplier float64) EIP1559FeeEstimatorOption {
62+
return eip1559FeeEstimatorOptionTipMultiplier(multiplier)
5063
}
5164

52-
func (f *EIP1559FeeEstimator) WithTipMultiplier(multiplier float64) *EIP1559FeeEstimator {
53-
newF := *f
54-
newF.tipMultiplier = multiplier
65+
func NewEIP1559FeeEstimator(client EIP1159FeeEthClient, opts ...EIP1559FeeEstimatorOption) *EIP1559FeeEstimator {
66+
options := eip1559FeeEstimatorOptions{
67+
baseMultiplier: 1.0,
68+
tipMultiplier: 1.0,
69+
}
70+
71+
for _, o := range opts {
72+
o.apply(&options)
73+
}
5574

56-
return &newF
75+
return &EIP1559FeeEstimator{
76+
client: client,
77+
options: options,
78+
}
5779
}
5880

5981
func (f *EIP1559FeeEstimator) EstimateFees(ctx context.Context, opts *bind.TransactOpts) (*bind.TransactOpts, error) {
@@ -68,7 +90,7 @@ func (f *EIP1559FeeEstimator) EstimateFees(ctx context.Context, opts *bind.Trans
6890
}
6991

7092
// GasTipCap represents the maxPriorityFeePerGas
71-
newOpts.GasTipCap = multiplyBigInt(tipCap, f.tipMultiplier)
93+
newOpts.GasTipCap = multiplyBigInt(tipCap, f.options.tipMultiplier)
7294
}
7395

7496
// Add a gas fee cap if needed
@@ -81,7 +103,7 @@ func (f *EIP1559FeeEstimator) EstimateFees(ctx context.Context, opts *bind.Trans
81103
baseFee := block.BaseFee()
82104
if baseFee != nil {
83105
// The adjusted base fee takes the multiplier into account
84-
adjustedBaseFee := multiplyBigInt(baseFee, f.baseMultiplier)
106+
adjustedBaseFee := multiplyBigInt(baseFee, f.options.baseMultiplier)
85107

86108
// The total fee (maxFeePerGas) is the sum of the base fee and the tip
87109
newOpts.GasFeeCap = big.NewInt(0).Add(adjustedBaseFee, newOpts.GasTipCap)

devnet-sdk/system/periphery/go-ethereum/fees_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestEstimateEIP1559Fees(t *testing.T) {
8080
// We create a fee estimator with a custom tip multiplier
8181
feeEstimator := NewEIP1559FeeEstimator(&mockFeeEthClientImpl{
8282
tipCapValue: tipCapValue,
83-
}).WithTipMultiplier(tipMultiplier)
83+
}, WithEIP1559TipMultiplier(tipMultiplier))
8484

8585
newOpts, err := feeEstimator.EstimateFees(context.Background(), defaultOpts)
8686
require.NoError(t, err)
@@ -181,7 +181,7 @@ func TestEstimateEIP1559Fees(t *testing.T) {
181181
// We create a fee estimator with a custom tip multiplier
182182
feeEstimator := NewEIP1559FeeEstimator(&mockFeeEthClientImpl{
183183
blockValue: blockValue,
184-
}).WithBaseMultiplier(baseMultiplier)
184+
}, WithEIP1559BaseMultiplier(baseMultiplier))
185185

186186
newOpts, err := feeEstimator.EstimateFees(context.Background(), defaultOpts)
187187
require.NoError(t, err)

0 commit comments

Comments
 (0)