-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swap.go
461 lines (418 loc) · 14.6 KB
/
Swap.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Code generated by https://github.com/dcaf-mocha/anchor-go. DO NOT EDIT.
package whirlpool
import (
"errors"
ag_binary "github.com/gagliardetto/binary"
ag_solanago "github.com/gagliardetto/solana-go"
ag_format "github.com/gagliardetto/solana-go/text/format"
ag_treeout "github.com/gagliardetto/treeout"
)
// Swap is the `swap` instruction.
type Swap struct {
Amount *uint64
OtherAmountThreshold *uint64
SqrtPriceLimit *ag_binary.Uint128
AmountSpecifiedIsInput *bool
AToB *bool
// [0] = [] tokenProgram
//
// [1] = [SIGNER] tokenAuthority
//
// [2] = [WRITE] whirlpool
//
// [3] = [WRITE] tokenOwnerAccountA
//
// [4] = [WRITE] tokenVaultA
//
// [5] = [WRITE] tokenOwnerAccountB
//
// [6] = [WRITE] tokenVaultB
//
// [7] = [WRITE] tickArray0
//
// [8] = [WRITE] tickArray1
//
// [9] = [WRITE] tickArray2
//
// [10] = [] oracle
ag_solanago.AccountMetaSlice `bin:"-"`
}
type SwapAccounts struct {
TokenProgram ag_solanago.PublicKey
TokenAuthority ag_solanago.PublicKey
Whirlpool ag_solanago.PublicKey
TokenOwnerAccountA ag_solanago.PublicKey
TokenVaultA ag_solanago.PublicKey
TokenOwnerAccountB ag_solanago.PublicKey
TokenVaultB ag_solanago.PublicKey
TickArray0 ag_solanago.PublicKey
TickArray1 ag_solanago.PublicKey
TickArray2 ag_solanago.PublicKey
Oracle ag_solanago.PublicKey
}
// NewSwapInstructionBuilder creates a new `Swap` instruction builder.
func NewSwapInstructionBuilder() *Swap {
nd := &Swap{
AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 11),
}
return nd
}
func (inst *Swap) GetSwapAccounts() *SwapAccounts {
res := &SwapAccounts{}
if inst.AccountMetaSlice[0] != nil {
res.TokenProgram = inst.AccountMetaSlice[0].PublicKey
}
if inst.AccountMetaSlice[1] != nil {
res.TokenAuthority = inst.AccountMetaSlice[1].PublicKey
}
if inst.AccountMetaSlice[2] != nil {
res.Whirlpool = inst.AccountMetaSlice[2].PublicKey
}
if inst.AccountMetaSlice[3] != nil {
res.TokenOwnerAccountA = inst.AccountMetaSlice[3].PublicKey
}
if inst.AccountMetaSlice[4] != nil {
res.TokenVaultA = inst.AccountMetaSlice[4].PublicKey
}
if inst.AccountMetaSlice[5] != nil {
res.TokenOwnerAccountB = inst.AccountMetaSlice[5].PublicKey
}
if inst.AccountMetaSlice[6] != nil {
res.TokenVaultB = inst.AccountMetaSlice[6].PublicKey
}
if inst.AccountMetaSlice[7] != nil {
res.TickArray0 = inst.AccountMetaSlice[7].PublicKey
}
if inst.AccountMetaSlice[8] != nil {
res.TickArray1 = inst.AccountMetaSlice[8].PublicKey
}
if inst.AccountMetaSlice[9] != nil {
res.TickArray2 = inst.AccountMetaSlice[9].PublicKey
}
if inst.AccountMetaSlice[10] != nil {
res.Oracle = inst.AccountMetaSlice[10].PublicKey
}
return res
}
// SetAmount sets the "amount" parameter.
func (inst *Swap) SetAmount(amount uint64) *Swap {
inst.Amount = &amount
return inst
}
// SetOtherAmountThreshold sets the "otherAmountThreshold" parameter.
func (inst *Swap) SetOtherAmountThreshold(otherAmountThreshold uint64) *Swap {
inst.OtherAmountThreshold = &otherAmountThreshold
return inst
}
// SetSqrtPriceLimit sets the "sqrtPriceLimit" parameter.
func (inst *Swap) SetSqrtPriceLimit(sqrtPriceLimit ag_binary.Uint128) *Swap {
inst.SqrtPriceLimit = &sqrtPriceLimit
return inst
}
// SetAmountSpecifiedIsInput sets the "amountSpecifiedIsInput" parameter.
func (inst *Swap) SetAmountSpecifiedIsInput(amountSpecifiedIsInput bool) *Swap {
inst.AmountSpecifiedIsInput = &amountSpecifiedIsInput
return inst
}
// SetAToB sets the "aToB" parameter.
func (inst *Swap) SetAToB(aToB bool) *Swap {
inst.AToB = &aToB
return inst
}
// SetTokenProgramAccount sets the "tokenProgram" account.
func (inst *Swap) SetTokenProgramAccount(tokenProgram ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[0] = ag_solanago.Meta(tokenProgram)
return inst
}
// GetTokenProgramAccount gets the "tokenProgram" account.
func (inst *Swap) GetTokenProgramAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(0)
}
// SetTokenAuthorityAccount sets the "tokenAuthority" account.
func (inst *Swap) SetTokenAuthorityAccount(tokenAuthority ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[1] = ag_solanago.Meta(tokenAuthority).SIGNER()
return inst
}
// GetTokenAuthorityAccount gets the "tokenAuthority" account.
func (inst *Swap) GetTokenAuthorityAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(1)
}
// SetWhirlpoolAccount sets the "whirlpool" account.
func (inst *Swap) SetWhirlpoolAccount(whirlpool ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[2] = ag_solanago.Meta(whirlpool).WRITE()
return inst
}
// GetWhirlpoolAccount gets the "whirlpool" account.
func (inst *Swap) GetWhirlpoolAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(2)
}
// SetTokenOwnerAccountAAccount sets the "tokenOwnerAccountA" account.
func (inst *Swap) SetTokenOwnerAccountAAccount(tokenOwnerAccountA ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[3] = ag_solanago.Meta(tokenOwnerAccountA).WRITE()
return inst
}
// GetTokenOwnerAccountAAccount gets the "tokenOwnerAccountA" account.
func (inst *Swap) GetTokenOwnerAccountAAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(3)
}
// SetTokenVaultAAccount sets the "tokenVaultA" account.
func (inst *Swap) SetTokenVaultAAccount(tokenVaultA ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[4] = ag_solanago.Meta(tokenVaultA).WRITE()
return inst
}
// GetTokenVaultAAccount gets the "tokenVaultA" account.
func (inst *Swap) GetTokenVaultAAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(4)
}
// SetTokenOwnerAccountBAccount sets the "tokenOwnerAccountB" account.
func (inst *Swap) SetTokenOwnerAccountBAccount(tokenOwnerAccountB ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[5] = ag_solanago.Meta(tokenOwnerAccountB).WRITE()
return inst
}
// GetTokenOwnerAccountBAccount gets the "tokenOwnerAccountB" account.
func (inst *Swap) GetTokenOwnerAccountBAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(5)
}
// SetTokenVaultBAccount sets the "tokenVaultB" account.
func (inst *Swap) SetTokenVaultBAccount(tokenVaultB ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[6] = ag_solanago.Meta(tokenVaultB).WRITE()
return inst
}
// GetTokenVaultBAccount gets the "tokenVaultB" account.
func (inst *Swap) GetTokenVaultBAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(6)
}
// SetTickArray0Account sets the "tickArray0" account.
func (inst *Swap) SetTickArray0Account(tickArray0 ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[7] = ag_solanago.Meta(tickArray0).WRITE()
return inst
}
// GetTickArray0Account gets the "tickArray0" account.
func (inst *Swap) GetTickArray0Account() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(7)
}
// SetTickArray1Account sets the "tickArray1" account.
func (inst *Swap) SetTickArray1Account(tickArray1 ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[8] = ag_solanago.Meta(tickArray1).WRITE()
return inst
}
// GetTickArray1Account gets the "tickArray1" account.
func (inst *Swap) GetTickArray1Account() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(8)
}
// SetTickArray2Account sets the "tickArray2" account.
func (inst *Swap) SetTickArray2Account(tickArray2 ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[9] = ag_solanago.Meta(tickArray2).WRITE()
return inst
}
// GetTickArray2Account gets the "tickArray2" account.
func (inst *Swap) GetTickArray2Account() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(9)
}
// SetOracleAccount sets the "oracle" account.
func (inst *Swap) SetOracleAccount(oracle ag_solanago.PublicKey) *Swap {
inst.AccountMetaSlice[10] = ag_solanago.Meta(oracle)
return inst
}
// GetOracleAccount gets the "oracle" account.
func (inst *Swap) GetOracleAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice.Get(10)
}
func (inst Swap) Build() *Instruction {
return &Instruction{BaseVariant: ag_binary.BaseVariant{
Impl: inst,
TypeID: Instruction_Swap,
}}
}
// ValidateAndBuild validates the instruction parameters and accounts;
// if there is a validation error, it returns the error.
// Otherwise, it builds and returns the instruction.
func (inst Swap) ValidateAndBuild() (*Instruction, error) {
if err := inst.Validate(); err != nil {
return nil, err
}
return inst.Build(), nil
}
func (inst *Swap) Validate() error {
// Check whether all (required) parameters are set:
{
if inst.Amount == nil {
return errors.New("Amount parameter is not set")
}
if inst.OtherAmountThreshold == nil {
return errors.New("OtherAmountThreshold parameter is not set")
}
if inst.SqrtPriceLimit == nil {
return errors.New("SqrtPriceLimit parameter is not set")
}
if inst.AmountSpecifiedIsInput == nil {
return errors.New("AmountSpecifiedIsInput parameter is not set")
}
if inst.AToB == nil {
return errors.New("AToB parameter is not set")
}
}
// Check whether all (required) accounts are set:
{
if inst.AccountMetaSlice[0] == nil {
return errors.New("accounts.TokenProgram is not set")
}
if inst.AccountMetaSlice[1] == nil {
return errors.New("accounts.TokenAuthority is not set")
}
if inst.AccountMetaSlice[2] == nil {
return errors.New("accounts.Whirlpool is not set")
}
if inst.AccountMetaSlice[3] == nil {
return errors.New("accounts.TokenOwnerAccountA is not set")
}
if inst.AccountMetaSlice[4] == nil {
return errors.New("accounts.TokenVaultA is not set")
}
if inst.AccountMetaSlice[5] == nil {
return errors.New("accounts.TokenOwnerAccountB is not set")
}
if inst.AccountMetaSlice[6] == nil {
return errors.New("accounts.TokenVaultB is not set")
}
if inst.AccountMetaSlice[7] == nil {
return errors.New("accounts.TickArray0 is not set")
}
if inst.AccountMetaSlice[8] == nil {
return errors.New("accounts.TickArray1 is not set")
}
if inst.AccountMetaSlice[9] == nil {
return errors.New("accounts.TickArray2 is not set")
}
if inst.AccountMetaSlice[10] == nil {
return errors.New("accounts.Oracle is not set")
}
}
return nil
}
func (inst *Swap) EncodeToTree(parent ag_treeout.Branches) {
parent.Child(ag_format.Program(ProgramName, ProgramID)).
//
ParentFunc(func(programBranch ag_treeout.Branches) {
programBranch.Child(ag_format.Instruction("Swap")).
//
ParentFunc(func(instructionBranch ag_treeout.Branches) {
// Parameters of the instruction:
instructionBranch.Child("Params[len=5]").ParentFunc(func(paramsBranch ag_treeout.Branches) {
paramsBranch.Child(ag_format.Param(" Amount", *inst.Amount))
paramsBranch.Child(ag_format.Param(" OtherAmountThreshold", *inst.OtherAmountThreshold))
paramsBranch.Child(ag_format.Param(" SqrtPriceLimit", *inst.SqrtPriceLimit))
paramsBranch.Child(ag_format.Param("AmountSpecifiedIsInput", *inst.AmountSpecifiedIsInput))
paramsBranch.Child(ag_format.Param(" AToB", *inst.AToB))
})
// Accounts of the instruction:
instructionBranch.Child("Accounts[len=11]").ParentFunc(func(accountsBranch ag_treeout.Branches) {
accountsBranch.Child(ag_format.Meta(" tokenProgram", inst.AccountMetaSlice.Get(0)))
accountsBranch.Child(ag_format.Meta(" tokenAuthority", inst.AccountMetaSlice.Get(1)))
accountsBranch.Child(ag_format.Meta(" whirlpool", inst.AccountMetaSlice.Get(2)))
accountsBranch.Child(ag_format.Meta("tokenOwnerAccountA", inst.AccountMetaSlice.Get(3)))
accountsBranch.Child(ag_format.Meta(" tokenVaultA", inst.AccountMetaSlice.Get(4)))
accountsBranch.Child(ag_format.Meta("tokenOwnerAccountB", inst.AccountMetaSlice.Get(5)))
accountsBranch.Child(ag_format.Meta(" tokenVaultB", inst.AccountMetaSlice.Get(6)))
accountsBranch.Child(ag_format.Meta(" tickArray0", inst.AccountMetaSlice.Get(7)))
accountsBranch.Child(ag_format.Meta(" tickArray1", inst.AccountMetaSlice.Get(8)))
accountsBranch.Child(ag_format.Meta(" tickArray2", inst.AccountMetaSlice.Get(9)))
accountsBranch.Child(ag_format.Meta(" oracle", inst.AccountMetaSlice.Get(10)))
})
})
})
}
func (obj Swap) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
// Serialize `Amount` param:
err = encoder.Encode(obj.Amount)
if err != nil {
return err
}
// Serialize `OtherAmountThreshold` param:
err = encoder.Encode(obj.OtherAmountThreshold)
if err != nil {
return err
}
// Serialize `SqrtPriceLimit` param:
err = encoder.Encode(obj.SqrtPriceLimit)
if err != nil {
return err
}
// Serialize `AmountSpecifiedIsInput` param:
err = encoder.Encode(obj.AmountSpecifiedIsInput)
if err != nil {
return err
}
// Serialize `AToB` param:
err = encoder.Encode(obj.AToB)
if err != nil {
return err
}
return nil
}
func (obj *Swap) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
// Deserialize `Amount`:
err = decoder.Decode(&obj.Amount)
if err != nil {
return err
}
// Deserialize `OtherAmountThreshold`:
err = decoder.Decode(&obj.OtherAmountThreshold)
if err != nil {
return err
}
// Deserialize `SqrtPriceLimit`:
err = decoder.Decode(&obj.SqrtPriceLimit)
if err != nil {
return err
}
// Deserialize `AmountSpecifiedIsInput`:
err = decoder.Decode(&obj.AmountSpecifiedIsInput)
if err != nil {
return err
}
// Deserialize `AToB`:
err = decoder.Decode(&obj.AToB)
if err != nil {
return err
}
return nil
}
// NewSwapInstruction declares a new Swap instruction with the provided parameters and accounts.
func NewSwapInstruction(
// Parameters:
amount uint64,
otherAmountThreshold uint64,
sqrtPriceLimit ag_binary.Uint128,
amountSpecifiedIsInput bool,
aToB bool,
// Accounts:
tokenProgram ag_solanago.PublicKey,
tokenAuthority ag_solanago.PublicKey,
whirlpool ag_solanago.PublicKey,
tokenOwnerAccountA ag_solanago.PublicKey,
tokenVaultA ag_solanago.PublicKey,
tokenOwnerAccountB ag_solanago.PublicKey,
tokenVaultB ag_solanago.PublicKey,
tickArray0 ag_solanago.PublicKey,
tickArray1 ag_solanago.PublicKey,
tickArray2 ag_solanago.PublicKey,
oracle ag_solanago.PublicKey) *Swap {
return NewSwapInstructionBuilder().
SetAmount(amount).
SetOtherAmountThreshold(otherAmountThreshold).
SetSqrtPriceLimit(sqrtPriceLimit).
SetAmountSpecifiedIsInput(amountSpecifiedIsInput).
SetAToB(aToB).
SetTokenProgramAccount(tokenProgram).
SetTokenAuthorityAccount(tokenAuthority).
SetWhirlpoolAccount(whirlpool).
SetTokenOwnerAccountAAccount(tokenOwnerAccountA).
SetTokenVaultAAccount(tokenVaultA).
SetTokenOwnerAccountBAccount(tokenOwnerAccountB).
SetTokenVaultBAccount(tokenVaultB).
SetTickArray0Account(tickArray0).
SetTickArray1Account(tickArray1).
SetTickArray2Account(tickArray2).
SetOracleAccount(oracle)
}