/
swap.ts
408 lines (384 loc) · 10.8 KB
/
swap.ts
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
import type { u64 } from "@saberhq/token-utils";
import type { PublicKey, TransactionInstruction } from "@solana/web3.js";
import type { Fees, RawFees } from "../state/index.js";
import { encodeFees, ZERO_FEES } from "../state/index.js";
import type { StableSwapConfig } from "./common.js";
import { buildInstruction } from "./common.js";
import {
DepositIXLayout,
InitializeSwapIXLayout,
SwapIXLayout,
WithdrawIXLayout,
WithdrawOneIXLayout,
} from "./layouts.js";
/**
* Instruction enum.
*/
export enum StableSwapInstruction {
INITIALIZE = 0,
SWAP = 1,
DEPOSIT = 2,
WITHDRAW = 3,
WITHDRAW_ONE = 4,
}
/**
* Info about the tokens to swap.
*/
export interface SwapTokenInfo {
/**
* The account that admin fees go to.
*/
adminFeeAccount: PublicKey;
/**
* Mint of the token.
*/
mint: PublicKey;
/**
* This swap's token reserves.
*/
reserve: PublicKey;
}
export interface InitializeSwapInstruction {
config: StableSwapConfig;
/**
* Account that can manage the swap.
*/
adminAccount: PublicKey;
tokenA: SwapTokenInfo;
tokenB: SwapTokenInfo;
poolTokenMint: PublicKey;
/**
* Destination account for the initial LP tokens.
*/
destinationPoolTokenAccount: PublicKey;
nonce: number;
ampFactor: u64;
fees?: Fees;
isPaused?: boolean;
}
export interface SwapInstruction {
config: StableSwapConfig;
/**
* User source authority
*/
userAuthority: PublicKey;
/**
* User source token account
*/
userSource: PublicKey;
/**
* Swap source token account
*/
poolSource: PublicKey;
/**
* Swap destination token account
*/
poolDestination: PublicKey;
/**
* User destination token account
*/
userDestination: PublicKey;
adminDestination: PublicKey;
amountIn: u64;
minimumAmountOut: u64;
}
export interface DepositInstruction {
config: StableSwapConfig;
/**
* Authority for user account
*/
userAuthority: PublicKey;
/**
* Depositor account for token A
*/
sourceA: PublicKey;
/**
* Depositor account for token B
*/
sourceB: PublicKey;
tokenAccountA: PublicKey;
tokenAccountB: PublicKey;
poolTokenMint: PublicKey;
poolTokenAccount: PublicKey;
tokenAmountA: u64;
tokenAmountB: u64;
minimumPoolTokenAmount: u64;
}
export interface WithdrawInstruction {
config: StableSwapConfig;
/**
* User source authority
*/
userAuthority: PublicKey;
poolMint: PublicKey;
tokenAccountA: PublicKey;
tokenAccountB: PublicKey;
adminFeeAccountA: PublicKey;
adminFeeAccountB: PublicKey;
/**
* Account which is the source of the pool tokens
* that is; the user's pool token account
*/
sourceAccount: PublicKey;
userAccountA: PublicKey;
userAccountB: PublicKey;
poolTokenAmount: u64;
minimumTokenA: u64;
minimumTokenB: u64;
}
export interface WithdrawOneInstruction {
config: StableSwapConfig;
/**
* User source authority
*/
userAuthority: PublicKey;
poolMint: PublicKey;
/**
* User account that holds the LP tokens
*/
sourceAccount: PublicKey;
/**
* Pool account that holds the tokens to withdraw
*/
baseTokenAccount: PublicKey;
/**
* Pool account that holds the other token
*/
quoteTokenAccount: PublicKey;
/**
* User base token account to withdraw to
*/
destinationAccount: PublicKey;
/**
* Admin base token account to send fees to
*/
adminDestinationAccount: PublicKey;
/**
* Amount of pool tokens to burn. User receives an output of token a
* or b based on the percentage of the pool tokens that are returned.
*/
poolTokenAmount: u64;
/**
* Minimum amount of base tokens to receive, prevents excessive slippage
*/
minimumTokenAmount: u64;
}
export const initializeSwapInstructionRaw = ({
config,
adminAccount,
tokenA: {
adminFeeAccount: adminFeeAccountA,
mint: tokenMintA,
reserve: tokenAccountA,
},
tokenB: {
adminFeeAccount: adminFeeAccountB,
mint: tokenMintB,
reserve: tokenAccountB,
},
poolTokenMint,
destinationPoolTokenAccount,
nonce,
ampFactor,
fees,
}: Omit<InitializeSwapInstruction, "fees"> & {
fees: RawFees;
}): TransactionInstruction => {
const keys = [
{ pubkey: config.swapAccount, isSigner: false, isWritable: false },
{ pubkey: config.authority, isSigner: false, isWritable: false },
{ pubkey: adminAccount, isSigner: false, isWritable: false },
{ pubkey: adminFeeAccountA, isSigner: false, isWritable: false },
{ pubkey: adminFeeAccountB, isSigner: false, isWritable: false },
{ pubkey: tokenMintA, isSigner: false, isWritable: false },
{ pubkey: tokenAccountA, isSigner: false, isWritable: false },
{ pubkey: tokenMintB, isSigner: false, isWritable: false },
{ pubkey: tokenAccountB, isSigner: false, isWritable: false },
{ pubkey: poolTokenMint, isSigner: false, isWritable: true },
{ pubkey: destinationPoolTokenAccount, isSigner: false, isWritable: true },
{ pubkey: config.tokenProgramID, isSigner: false, isWritable: false },
];
const data = Buffer.alloc(InitializeSwapIXLayout.span);
InitializeSwapIXLayout.encode(
{
instruction: StableSwapInstruction.INITIALIZE, // InitializeSwap instruction
nonce,
ampFactor: ampFactor.toBuffer(),
fees,
},
data
);
return buildInstruction({
config,
keys,
data,
});
};
export const initializeSwapInstruction = ({
fees = ZERO_FEES,
...args
}: InitializeSwapInstruction): TransactionInstruction => {
return initializeSwapInstructionRaw({ ...args, fees: encodeFees(fees) });
};
export const swapInstruction = ({
config,
userAuthority,
userSource,
poolSource,
poolDestination,
userDestination,
adminDestination,
amountIn,
minimumAmountOut,
}: SwapInstruction): TransactionInstruction => {
const data = Buffer.alloc(SwapIXLayout.span);
SwapIXLayout.encode(
{
instruction: StableSwapInstruction.SWAP, // Swap instruction
amountIn: amountIn.toBuffer(),
minimumAmountOut: minimumAmountOut.toBuffer(),
},
data
);
const keys = [
{ pubkey: config.swapAccount, isSigner: false, isWritable: false },
{ pubkey: config.authority, isSigner: false, isWritable: false },
{ pubkey: userAuthority, isSigner: true, isWritable: false },
{ pubkey: userSource, isSigner: false, isWritable: true },
{ pubkey: poolSource, isSigner: false, isWritable: true },
{ pubkey: poolDestination, isSigner: false, isWritable: true },
{ pubkey: userDestination, isSigner: false, isWritable: true },
{ pubkey: adminDestination, isSigner: false, isWritable: true },
{ pubkey: config.tokenProgramID, isSigner: false, isWritable: false },
];
return buildInstruction({
config,
keys,
data,
});
};
export const depositInstruction = ({
config,
userAuthority,
sourceA,
sourceB,
tokenAccountA,
tokenAccountB,
poolTokenMint,
poolTokenAccount,
tokenAmountA,
tokenAmountB,
minimumPoolTokenAmount,
}: DepositInstruction): TransactionInstruction => {
const data = Buffer.alloc(DepositIXLayout.span);
DepositIXLayout.encode(
{
instruction: StableSwapInstruction.DEPOSIT, // Deposit instruction
tokenAmountA: tokenAmountA.toBuffer(),
tokenAmountB: tokenAmountB.toBuffer(),
minimumPoolTokenAmount: minimumPoolTokenAmount.toBuffer(),
},
data
);
const keys = [
{ pubkey: config.swapAccount, isSigner: false, isWritable: false },
{ pubkey: config.authority, isSigner: false, isWritable: false },
{ pubkey: userAuthority, isSigner: true, isWritable: false },
{ pubkey: sourceA, isSigner: false, isWritable: true },
{ pubkey: sourceB, isSigner: false, isWritable: true },
{ pubkey: tokenAccountA, isSigner: false, isWritable: true },
{ pubkey: tokenAccountB, isSigner: false, isWritable: true },
{ pubkey: poolTokenMint, isSigner: false, isWritable: true },
{ pubkey: poolTokenAccount, isSigner: false, isWritable: true },
{ pubkey: config.tokenProgramID, isSigner: false, isWritable: false },
];
return buildInstruction({
config,
keys,
data,
});
};
export const withdrawInstruction = ({
config,
userAuthority,
poolMint,
sourceAccount,
tokenAccountA,
tokenAccountB,
userAccountA,
userAccountB,
adminFeeAccountA,
adminFeeAccountB,
poolTokenAmount,
minimumTokenA,
minimumTokenB,
}: WithdrawInstruction): TransactionInstruction => {
const data = Buffer.alloc(WithdrawIXLayout.span);
WithdrawIXLayout.encode(
{
instruction: StableSwapInstruction.WITHDRAW, // Withdraw instruction
poolTokenAmount: poolTokenAmount.toBuffer(),
minimumTokenA: minimumTokenA.toBuffer(),
minimumTokenB: minimumTokenB.toBuffer(),
},
data
);
const keys = [
{ pubkey: config.swapAccount, isSigner: false, isWritable: false },
{ pubkey: config.authority, isSigner: false, isWritable: false },
{ pubkey: userAuthority, isSigner: true, isWritable: false },
{ pubkey: poolMint, isSigner: false, isWritable: true },
{ pubkey: sourceAccount, isSigner: false, isWritable: true },
{ pubkey: tokenAccountA, isSigner: false, isWritable: true },
{ pubkey: tokenAccountB, isSigner: false, isWritable: true },
{ pubkey: userAccountA, isSigner: false, isWritable: true },
{ pubkey: userAccountB, isSigner: false, isWritable: true },
{ pubkey: adminFeeAccountA, isSigner: false, isWritable: true },
{ pubkey: adminFeeAccountB, isSigner: false, isWritable: true },
{ pubkey: config.tokenProgramID, isSigner: false, isWritable: false },
];
return buildInstruction({
config,
keys,
data,
});
};
export const withdrawOneInstruction = ({
config,
userAuthority,
poolMint,
sourceAccount,
baseTokenAccount,
quoteTokenAccount,
destinationAccount,
adminDestinationAccount,
poolTokenAmount,
minimumTokenAmount,
}: WithdrawOneInstruction): TransactionInstruction => {
const data = Buffer.alloc(WithdrawOneIXLayout.span);
WithdrawOneIXLayout.encode(
{
instruction: StableSwapInstruction.WITHDRAW_ONE, // Withdraw instruction
poolTokenAmount: poolTokenAmount.toBuffer(),
minimumTokenAmount: minimumTokenAmount.toBuffer(),
},
data
);
const keys = [
{ pubkey: config.swapAccount, isSigner: false, isWritable: false },
{ pubkey: config.authority, isSigner: false, isWritable: false },
{ pubkey: userAuthority, isSigner: true, isWritable: false },
{ pubkey: poolMint, isSigner: false, isWritable: true },
{ pubkey: sourceAccount, isSigner: false, isWritable: true },
{ pubkey: baseTokenAccount, isSigner: false, isWritable: true },
{ pubkey: quoteTokenAccount, isSigner: false, isWritable: true },
{ pubkey: destinationAccount, isSigner: false, isWritable: true },
{ pubkey: adminDestinationAccount, isSigner: false, isWritable: true },
{ pubkey: config.tokenProgramID, isSigner: false, isWritable: false },
];
return buildInstruction({
config,
keys,
data,
});
};