/
suggested_price_estimator.go
238 lines (207 loc) · 8.41 KB
/
suggested_price_estimator.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gas
import (
"context"
"fmt"
"slices"
"sync"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
pkgerrors "github.com/pkg/errors"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
bigmath "github.com/smartcontractkit/chainlink-common/pkg/utils/big_math"
"github.com/smartcontractkit/chainlink/v2/common/fee"
feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
)
var (
_ EvmEstimator = &SuggestedPriceEstimator{}
)
type suggestedPriceConfig interface {
BumpPercent() uint16
BumpMin() *assets.Wei
}
//go:generate mockery --quiet --name rpcClient --output ./mocks/ --case=underscore --structname RPCClient
type rpcClient interface {
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
}
// SuggestedPriceEstimator is an Estimator which uses the suggested gas price from eth_gasPrice.
type SuggestedPriceEstimator struct {
services.StateMachine
cfg suggestedPriceConfig
client rpcClient
pollPeriod time.Duration
logger logger.Logger
gasPriceMu sync.RWMutex
GasPrice *assets.Wei
chForceRefetch chan (chan struct{})
chInitialised chan struct{}
chStop services.StopChan
chDone chan struct{}
}
// NewSuggestedPriceEstimator returns a new Estimator which uses the suggested gas price.
func NewSuggestedPriceEstimator(lggr logger.Logger, client rpcClient, cfg suggestedPriceConfig) EvmEstimator {
return &SuggestedPriceEstimator{
client: client,
pollPeriod: 10 * time.Second,
logger: logger.Named(lggr, "SuggestedPriceEstimator"),
cfg: cfg,
chForceRefetch: make(chan (chan struct{})),
chInitialised: make(chan struct{}),
chStop: make(chan struct{}),
chDone: make(chan struct{}),
}
}
func (o *SuggestedPriceEstimator) Name() string {
return o.logger.Name()
}
func (o *SuggestedPriceEstimator) Start(context.Context) error {
return o.StartOnce("SuggestedPriceEstimator", func() error {
go o.run()
<-o.chInitialised
return nil
})
}
func (o *SuggestedPriceEstimator) Close() error {
return o.StopOnce("SuggestedPriceEstimator", func() error {
close(o.chStop)
<-o.chDone
return nil
})
}
func (o *SuggestedPriceEstimator) HealthReport() map[string]error {
return map[string]error{o.Name(): o.Healthy()}
}
func (o *SuggestedPriceEstimator) run() {
defer close(o.chDone)
t := o.refreshPrice()
close(o.chInitialised)
for {
select {
case <-o.chStop:
return
case ch := <-o.chForceRefetch:
t.Stop()
t = o.refreshPrice()
close(ch)
case <-t.C:
t = o.refreshPrice()
}
}
}
func (o *SuggestedPriceEstimator) refreshPrice() (t *time.Timer) {
t = time.NewTimer(utils.WithJitter(o.pollPeriod))
var res hexutil.Big
ctx, cancel := o.chStop.CtxCancel(evmclient.ContextWithDefaultTimeout())
defer cancel()
if err := o.client.CallContext(ctx, &res, "eth_gasPrice"); err != nil {
o.logger.Warnf("Failed to refresh prices, got error: %s", err)
return
}
bi := (*assets.Wei)(&res)
o.logger.Debugw("refreshPrice", "GasPrice", bi)
o.gasPriceMu.Lock()
defer o.gasPriceMu.Unlock()
o.GasPrice = bi
return
}
// Uses the force refetch chan to trigger a price update and blocks until complete
func (o *SuggestedPriceEstimator) forceRefresh(ctx context.Context) (err error) {
ch := make(chan struct{})
select {
case o.chForceRefetch <- ch:
case <-o.chStop:
return pkgerrors.New("estimator stopped")
case <-ctx.Done():
return ctx.Err()
}
select {
case <-ch:
case <-o.chStop:
return pkgerrors.New("estimator stopped")
case <-ctx.Done():
return ctx.Err()
}
return
}
func (o *SuggestedPriceEstimator) OnNewLongestChain(context.Context, *evmtypes.Head) {}
func (*SuggestedPriceEstimator) GetDynamicFee(_ context.Context, _ uint64, _ *assets.Wei) (fee DynamicFee, chainSpecificGasLimit uint64, err error) {
err = pkgerrors.New("dynamic fees are not implemented for this estimator")
return
}
func (*SuggestedPriceEstimator) BumpDynamicFee(_ context.Context, _ DynamicFee, _ uint64, _ *assets.Wei, _ []EvmPriorAttempt) (bumped DynamicFee, chainSpecificGasLimit uint64, err error) {
err = pkgerrors.New("dynamic fees are not implemented for this estimator")
return
}
func (o *SuggestedPriceEstimator) GetLegacyGas(ctx context.Context, _ []byte, GasLimit uint64, maxGasPriceWei *assets.Wei, opts ...feetypes.Opt) (gasPrice *assets.Wei, chainSpecificGasLimit uint64, err error) {
chainSpecificGasLimit = GasLimit
ok := o.IfStarted(func() {
if slices.Contains(opts, feetypes.OptForceRefetch) {
err = o.forceRefresh(ctx)
}
if gasPrice = o.getGasPrice(); gasPrice == nil {
err = pkgerrors.New("failed to estimate gas; gas price not set")
return
}
o.logger.Debugw("GetLegacyGas", "GasPrice", gasPrice, "GasLimit", GasLimit)
})
if !ok {
return nil, 0, pkgerrors.New("estimator is not started")
} else if err != nil {
return
}
// For L2 chains, submitting a transaction that is not priced high enough will cause the call to fail, so if the cap is lower than the RPC suggested gas price, this transaction cannot succeed
if gasPrice != nil && gasPrice.Cmp(maxGasPriceWei) > 0 {
return nil, 0, pkgerrors.Errorf("estimated gas price: %s is greater than the maximum gas price configured: %s", gasPrice.String(), maxGasPriceWei.String())
}
return
}
// Refreshes the gas price by making a call to the RPC in case the current one has gone stale.
// Adds the larger of BumpPercent and BumpMin configs as a buffer on top of the price returned from the RPC.
// The only reason bumping logic would be called on the SuggestedPriceEstimator is if there was a significant price spike
// between the last price update and when the tx was submitted. Refreshing the price helps ensure the latest market changes are accounted for.
func (o *SuggestedPriceEstimator) BumpLegacyGas(ctx context.Context, originalFee *assets.Wei, feeLimit uint64, maxGasPriceWei *assets.Wei, _ []EvmPriorAttempt) (newGasPrice *assets.Wei, chainSpecificGasLimit uint64, err error) {
chainSpecificGasLimit = feeLimit
ok := o.IfStarted(func() {
// Immediately return error if original fee is greater than or equal to the max gas price
// Prevents a loop of resubmitting the attempt with the max gas price
if originalFee.Cmp(maxGasPriceWei) >= 0 {
err = fmt.Errorf("original fee (%s) greater than or equal to max gas price (%s) so cannot be bumped further", originalFee.String(), maxGasPriceWei.String())
return
}
err = o.forceRefresh(ctx)
if newGasPrice = o.getGasPrice(); newGasPrice == nil {
err = pkgerrors.New("failed to refresh and return gas; gas price not set")
return
}
o.logger.Debugw("GasPrice", newGasPrice, "GasLimit", feeLimit)
})
if !ok {
return nil, 0, pkgerrors.New("estimator is not started")
} else if err != nil {
return
}
if newGasPrice != nil && newGasPrice.Cmp(maxGasPriceWei) > 0 {
return nil, 0, pkgerrors.Errorf("estimated gas price: %s is greater than the maximum gas price configured: %s", newGasPrice.String(), maxGasPriceWei.String())
}
// Add a buffer on top of the gas price returned by the RPC.
// Bump logic when using the suggested gas price from an RPC is realistically only needed when there is increased volatility in gas price.
// This buffer is a precaution to increase the chance of getting this tx on chain
bufferedPrice := fee.MaxBumpedFee(newGasPrice.ToInt(), o.cfg.BumpPercent(), o.cfg.BumpMin().ToInt())
// If the new suggested price is less than or equal to the max and the buffer puts the new price over the max, return the max price instead
// The buffer is added on top of the suggested price during bumping as just a precaution. It is better to resubmit the transaction with the max gas price instead of erroring.
newGasPrice = assets.NewWei(bigmath.Min(bufferedPrice, maxGasPriceWei.ToInt()))
// Return the original price if the refreshed price with the buffer is lower to ensure the bumped gas price is always equal or higher to the previous attempt
if originalFee != nil && originalFee.Cmp(newGasPrice) > 0 {
return originalFee, chainSpecificGasLimit, nil
}
return
}
func (o *SuggestedPriceEstimator) getGasPrice() (GasPrice *assets.Wei) {
o.gasPriceMu.RLock()
defer o.gasPriceMu.RUnlock()
return o.GasPrice
}