-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfee_quoter.go
More file actions
322 lines (290 loc) · 11.3 KB
/
Copy pathfee_quoter.go
File metadata and controls
322 lines (290 loc) · 11.3 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package feequoter
import (
"context"
"math/big"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/tvm/cell"
ccipcommon "github.com/smartcontractkit/chainlink-ton/pkg/ccip/bindings/common"
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tvm"
)
const (
tokenPriceGetter = "tokenPrice"
StaticConfigGetter = "staticConfig"
)
// Fee Quoter opcodes
const (
OpcodeUpdatePrices = 0x20000001
OpcodeUpdateFeeTokens = 0xD0984986
OpcodeUpdateTokenTransferFeeConfigs = 0xB2826316
OpcodeUpdateDestChainConfigs = 0x29950BAA
OpcodeFeeQuoterGetValidatedFee = 0x7496FF56
OpcodeFeeQuoterMessageValidated = 0x1FA60374
)
// Fee Quoter exit codes
const (
ErrorUnsupportedChainFamilySelector tvm.ExitCode = tvm.ExitCode(1001)
ErrorGasLimitTooHigh tvm.ExitCode = tvm.ExitCode(1002)
ExtraArgOutOfOrderExecutionMustBeTrue tvm.ExitCode = tvm.ExitCode(1003)
ErrorInvalidExtraArgsData tvm.ExitCode = tvm.ExitCode(1004)
ErrorUnsupportedNumberOfTokens tvm.ExitCode = tvm.ExitCode(1005)
ErrorInvalidSuiReceiverAddress tvm.ExitCode = tvm.ExitCode(1006)
ErrorInvalidTokenReceiver tvm.ExitCode = tvm.ExitCode(1007)
ErrorTooManySuiExtraArgsReceiverObjectIDs tvm.ExitCode = tvm.ExitCode(1008)
ErrorMsgDataTooLarge tvm.ExitCode = tvm.ExitCode(1009)
)
type Storage struct {
ID uint32 `tlb:"## 32"`
Ownable ccipcommon.Ownable2Step `tlb:"."`
MaxFeeJuelsPerMsg *big.Int `tlb:"## 96"`
LinkToken *address.Address `tlb:"addr"`
TokenPriceStalenessThreshold uint64 `tlb:"## 64"`
UsdPerToken *cell.Dictionary `tlb:"dict 267"`
PremiumMultiplierWeiPerEth *cell.Dictionary `tlb:"dict 267"`
DestChainConfigs *cell.Dictionary `tlb:"dict 64"`
}
type USDPerUnitGas struct {
ExecutionGasPrice *big.Int `tlb:"## 112"`
DataAvailabilityGasPrice *big.Int `tlb:"## 112"`
Timestamp uint64 `tlb:"## 64"`
}
type DestChainConfig struct {
IsEnabled bool `tlb:"bool"`
MaxNumberOfTokensPerMsg uint16 `tlb:"## 16"`
MaxDataBytes uint32 `tlb:"## 32"`
MaxPerMsgGasLimit uint32 `tlb:"## 32"`
DestGasOverhead uint32 `tlb:"## 32"`
DestGasPerPayloadByteBase uint8 `tlb:"## 8"`
DestGasPerPayloadByteHigh uint8 `tlb:"## 8"`
DestGasPerPayloadByteThreshold uint16 `tlb:"## 16"`
DestDataAvailabilityOverheadGas uint32 `tlb:"## 32"`
DestGasPerDataAvailabilityByte uint16 `tlb:"## 16"`
DestDataAvailabilityMultiplierBps uint16 `tlb:"## 16"`
ChainFamilySelector uint32 `tlb:"## 32"`
EnforceOutOfOrder bool `tlb:"bool"`
DefaultTokenFeeUsdCents uint16 `tlb:"## 16"`
DefaultTokenDestGasOverhead uint32 `tlb:"## 32"`
DefaultTxGasLimit uint32 `tlb:"## 32"`
GasMultiplierWeiPerEth uint64 `tlb:"## 64"`
GasPriceStalenessThreshold uint32 `tlb:"## 32"`
NetworkFeeUsdCents uint32 `tlb:"## 32"`
}
func (c *DestChainConfig) FromResult(result *ton.ExecutionResult) error {
isEnabledInt, err := result.Int(0)
if err != nil {
return err
}
isEnabled := isEnabledInt.Cmp(big.NewInt(-1)) == 0
maxNumberOfTokensPerMsg, err := result.Int(1)
if err != nil {
return err
}
maxDataBytes, err := result.Int(2)
if err != nil {
return err
}
maxPerMsgGasLimit, err := result.Int(3)
if err != nil {
return err
}
destGasOverhead, err := result.Int(4)
if err != nil {
return err
}
destGasPerPayloadByteBase, err := result.Int(5)
if err != nil {
return err
}
destGasPerPayloadByteHigh, err := result.Int(6)
if err != nil {
return err
}
destGasPerPayloadByteThreshold, err := result.Int(7)
if err != nil {
return err
}
destDataAvailabilityOverheadGas, err := result.Int(8)
if err != nil {
return err
}
destGasPerDataAvailabilityByte, err := result.Int(9)
if err != nil {
return err
}
destDataAvailabilityMultiplierBps, err := result.Int(10)
if err != nil {
return err
}
chainFamilySelector, err := result.Int(11)
if err != nil {
return err
}
enforceOutOfOrderInt, err := result.Int(12)
if err != nil {
return err
}
enforceOutOfOrder := enforceOutOfOrderInt.Cmp(big.NewInt(-1)) == 0
defaultTokenFeeUsdCents, err := result.Int(13)
if err != nil {
return err
}
defaultTokenDestGasOverhead, err := result.Int(14)
if err != nil {
return err
}
defaultTxGasLimit, err := result.Int(15)
if err != nil {
return err
}
gasMultiplierWeiPerEth, err := result.Int(16)
if err != nil {
return err
}
gasPriceStalenessThreshold, err := result.Int(17)
if err != nil {
return err
}
networkFeeUsdCents, err := result.Int(18)
if err != nil {
return err
}
*c = DestChainConfig{
IsEnabled: isEnabled,
MaxNumberOfTokensPerMsg: uint16(maxNumberOfTokensPerMsg.Uint64()), //nolint:gosec // G115
MaxDataBytes: uint32(maxDataBytes.Uint64()), //nolint:gosec // G115
MaxPerMsgGasLimit: uint32(maxPerMsgGasLimit.Uint64()), //nolint:gosec // G115
DestGasOverhead: uint32(destGasOverhead.Uint64()), //nolint:gosec // G115
DestGasPerPayloadByteBase: uint8(destGasPerPayloadByteBase.Uint64()), //nolint:gosec // G115
DestGasPerPayloadByteHigh: uint8(destGasPerPayloadByteHigh.Uint64()), //nolint:gosec // G115
DestGasPerPayloadByteThreshold: uint16(destGasPerPayloadByteThreshold.Uint64()), //nolint:gosec // G115
DestDataAvailabilityOverheadGas: uint32(destDataAvailabilityOverheadGas.Uint64()), //nolint:gosec // G115
DestGasPerDataAvailabilityByte: uint16(destGasPerDataAvailabilityByte.Uint64()), //nolint:gosec // G115
DestDataAvailabilityMultiplierBps: uint16(destDataAvailabilityMultiplierBps.Uint64()), //nolint:gosec // G115
ChainFamilySelector: uint32(chainFamilySelector.Uint64()), //nolint:gosec // G115
EnforceOutOfOrder: enforceOutOfOrder,
DefaultTokenFeeUsdCents: uint16(defaultTokenFeeUsdCents.Uint64()), //nolint:gosec // G115
DefaultTokenDestGasOverhead: uint32(defaultTokenDestGasOverhead.Uint64()), //nolint:gosec // G115
DefaultTxGasLimit: uint32(defaultTxGasLimit.Uint64()), //nolint:gosec // G115
GasMultiplierWeiPerEth: gasMultiplierWeiPerEth.Uint64(),
GasPriceStalenessThreshold: uint32(gasPriceStalenessThreshold.Uint64()), //nolint:gosec // G115
NetworkFeeUsdCents: uint32(networkFeeUsdCents.Uint64()), //nolint:gosec // G115
}
return nil
}
func (c *DestChainConfig) FetchResult(ctx context.Context, client ton.APIClientWrapped, block *ton.BlockIDExt, contractAddr *address.Address, destChainSelector []interface{}) error {
return ccipcommon.FetchResultHelper(ctx, client, block, contractAddr, ccipcommon.DestChainConfigGetter, destChainSelector, c.FromResult)
}
type TokenTransferFeeConfig struct {
IsEnabled bool `tlb:"bool"`
MinFeeUsdCents uint32 `tlb:"## 32"`
MaxFeeUsdCents uint32 `tlb:"## 32"`
DeciBps uint16 `tlb:"## 16"`
DestGasOverhead uint32 `tlb:"## 32"`
DestBytesOverhead uint32 `tlb:"## 32"`
}
type TimestampedPrice struct {
Value *big.Int `tlb:"## 224"`
Timestamp uint32 `tlb:"## 32"`
}
// TODO: we can't parse ton.ExecutionResult via tlb, implement as a tlb feature upstream
func (p *TimestampedPrice) FromResult(result *ton.ExecutionResult) error {
value, err := result.Int(0)
if err != nil {
return err
}
timestamp, err := result.Int(1)
if err != nil {
return err
}
*p = TimestampedPrice{
Value: value,
Timestamp: uint32(timestamp.Uint64()), //nolint:gosec // G115
}
return nil
}
func (p *TimestampedPrice) FetchResult(ctx context.Context, client ton.APIClientWrapped, block *ton.BlockIDExt, contractAddr *address.Address, opts []interface{}) error {
return ccipcommon.FetchResultHelper(ctx, client, block, contractAddr, tokenPriceGetter, opts, p.FromResult)
}
type TokenPriceUpdate struct {
SourceToken *address.Address `tlb:"addr"`
UsdPerToken *big.Int `tlb:"## 224"`
}
type GasPriceUpdate struct {
DestChainSelector uint64 `tlb:"## 64"`
ExecutionGasPrice *big.Int `tlb:"## 112"`
DataAvailabilityGasPrice *big.Int `tlb:"## 112"`
}
type FeeToken struct {
PremiumMultiplierWeiPerEth uint64 `tlb:"## 64"`
}
// Methods
// Generic wrapper for fee quoter messages with metadata
type GetValidatedFee struct {
_ tlb.Magic `tlb:"#7496FF56"` //nolint:revive // Ignore opcode tag
Msg *cell.Cell `tlb:"^"` // Cell containing the CCIPSend message
Metadata *cell.Cell `tlb:"^"` // Cell containing metadata
}
type MessageValidated struct {
_ tlb.Magic `tlb:"#1FA60374"` //nolint:revive // Ignore opcode tag
Msg *cell.Cell `tlb:"^"` // TODO put content here
Metadata *cell.Cell `tlb:"^"`
Fee *tlb.Coins `tlb:"."`
}
type UpdatePrices struct {
_ tlb.Magic `tlb:"#20000001"` //nolint:revive // Ignore opcode tag
TokenPrices ccipcommon.SnakeData[TokenPriceUpdate] `tlb:"^"`
GasPrices ccipcommon.SnakeData[GasPriceUpdate] `tlb:"^"`
}
type UpdateFeeTokens struct {
_ tlb.Magic `tlb:"#D0984986"` //nolint:revive // Ignore opcode tag
Add *cell.Dictionary `tlb:"dict 267"`
Remove ccipcommon.SnakeData[*address.Address] `tlb:"^"`
}
type UpdateTokenTransferFeeConfig struct {
_ tlb.Magic `tlb:"#B2826316"` //nolint:revive // Ignore opcode tag
Add map[*address.Address]TokenTransferFeeConfig
Remove []*address.Address `tlb:"addr"`
}
type UpdateTokenTransferFeeConfigs struct{}
type UpdateDestChainConfig struct {
DestinationChainSelector uint64 `tlb:"## 64"`
DestChainConfig DestChainConfig `tlb:"."`
}
type UpdateDestChainConfigs struct {
_ tlb.Magic `tlb:"#29950BAA"` //nolint:revive // Ignore opcode tag
Updates ccipcommon.SnakeData[UpdateDestChainConfig] `tlb:"^"`
}
// binding types that supports FetchResult interface with rpc client
type StaticConfig struct {
MaxFeeJuelsPerMsg *big.Int
LinkToken *address.Address
StalenessThreshold uint32
}
func (s *StaticConfig) FromResult(result *ton.ExecutionResult) error {
maxFeeJuelsPerMsg, err := result.Int(0)
if err != nil {
return err
}
linkTokenAddressSlice, err := result.Slice(1)
if err != nil {
return err
}
linkTokenAddress, err := linkTokenAddressSlice.LoadAddr()
if err != nil {
return err
}
tokenPriceStalenessThreshold, err := result.Int(2)
if err != nil {
return err
}
*s = StaticConfig{
MaxFeeJuelsPerMsg: maxFeeJuelsPerMsg,
LinkToken: linkTokenAddress,
StalenessThreshold: uint32(tokenPriceStalenessThreshold.Uint64()), //nolint:gosec // G115
}
return nil
}
func (s *StaticConfig) FetchResult(ctx context.Context, client ton.APIClientWrapped, block *ton.BlockIDExt, contractAddr *address.Address, _ []interface{}) error {
return ccipcommon.FetchResultHelper(ctx, client, block, contractAddr, StaticConfigGetter, nil, s.FromResult)
}