/
stable-swap.ts
370 lines (349 loc) · 9.43 KB
/
stable-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
import { getProgramAddress } from "@saberhq/solana-contrib";
import type { ProgramAccount } from "@saberhq/token-utils";
import { TOKEN_PROGRAM_ID } from "@saberhq/token-utils";
import type { Connection, TransactionInstruction } from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";
import { SWAP_PROGRAM_ID } from "./constants.js";
import type { IExchange } from "./entities/exchange.js";
import type { StableSwapConfig } from "./instructions/index.js";
import * as instructions from "./instructions/index.js";
import type { StableSwapState } from "./state/index.js";
import { decodeSwap } from "./state/index.js";
import { StableSwapLayout } from "./state/layout.js";
import { loadProgramAccount } from "./util/account.js";
export interface StableSwapInfo {
config: StableSwapConfig;
state: StableSwapState;
}
/**
* Swap token A for token B
* @param userSource
* @param poolSource
* @param poolDestination
* @param userDestination
* @param amountIn
* @param minimumAmountOut
*/
export function createSaberSwapInstruction(
{ config, state }: StableSwapInfo,
args: Pick<
instructions.SwapInstruction,
| "userAuthority"
| "userSource"
| "userDestination"
| "poolSource"
| "poolDestination"
| "amountIn"
| "minimumAmountOut"
>
): TransactionInstruction {
const adminDestination = args.poolDestination.equals(state.tokenA.reserve)
? state.tokenA.adminFeeAccount
: state.tokenB.adminFeeAccount;
return instructions.swapInstruction({
config: config,
...args,
adminDestination,
});
}
/**
* Deposit tokens into the pool.
*/
export function createSaberDepositInstruction(
{ config, state }: StableSwapInfo,
args: Pick<
instructions.DepositInstruction,
| "userAuthority"
| "sourceA"
| "sourceB"
| "poolTokenAccount"
| "tokenAmountA"
| "tokenAmountB"
| "minimumPoolTokenAmount"
>
): TransactionInstruction {
return instructions.depositInstruction({
config: config,
tokenAccountA: state.tokenA.reserve,
tokenAccountB: state.tokenB.reserve,
poolTokenMint: state.poolTokenMint,
...args,
});
}
/**
* Withdraw tokens from the pool
*/
export function createSaberWithdrawInstruction(
{ config, state }: StableSwapInfo,
args: Pick<
instructions.WithdrawInstruction,
| "userAuthority"
| "userAccountA"
| "userAccountB"
| "sourceAccount"
| "poolTokenAmount"
| "minimumTokenA"
| "minimumTokenB"
>
): TransactionInstruction {
return instructions.withdrawInstruction({
config: config,
poolMint: state.poolTokenMint,
tokenAccountA: state.tokenA.reserve,
tokenAccountB: state.tokenB.reserve,
adminFeeAccountA: state.tokenA.adminFeeAccount,
adminFeeAccountB: state.tokenB.adminFeeAccount,
...args,
});
}
/**
* Withdraw tokens from the pool
*/
export function createSaberWithdrawOneInstruction(
{ config, state }: StableSwapInfo,
args: Pick<
instructions.WithdrawOneInstruction,
| "userAuthority"
| "baseTokenAccount"
| "destinationAccount"
| "sourceAccount"
| "poolTokenAmount"
| "minimumTokenAmount"
>
): TransactionInstruction {
const [quoteTokenAccount, adminDestinationAccount] =
args.baseTokenAccount.equals(state.tokenA.reserve)
? [state.tokenB.reserve, state.tokenA.adminFeeAccount]
: [state.tokenA.reserve, state.tokenB.adminFeeAccount];
return instructions.withdrawOneInstruction({
config: config,
poolMint: state.poolTokenMint,
quoteTokenAccount,
adminDestinationAccount,
...args,
});
}
export class StableSwap implements StableSwapInfo {
/**
* Constructor for new StableSwap client object.
* @param config
* @param state
*/
constructor(
readonly config: StableSwapConfig,
readonly state: StableSwapState
) {}
/**
* Get the minimum balance for the token swap account to be rent exempt
*
* @return Number of lamports required
*/
static async getMinBalanceRentForExemptStableSwap(
connection: Connection
): Promise<number> {
return await connection.getMinimumBalanceForRentExemption(
StableSwapLayout.span
);
}
/**
* Load an onchain StableSwap program.
*
* @param connection A {@link Connection} to use.
* @param swapAccount The {@link PublicKey} of the swap account to load. You can obtain this pubkey by visiting [app.saber.so](https://app.saber.so/], navigating to the pool you want to load, and getting the "swap account" key.
* @param programID Address of the onchain StableSwap program.
*/
static async load(
connection: Connection,
swapAccount: PublicKey,
programID: PublicKey = SWAP_PROGRAM_ID
): Promise<StableSwap> {
const data = await loadProgramAccount(connection, swapAccount, programID);
const authority = getSwapAuthorityKey(swapAccount, programID);
return StableSwap.loadWithData(swapAccount, data, authority, programID);
}
/**
* Loads an onchain StableSwap program from an {@link IExchange}.
*
* @param connection
* @param exchange
* @returns
*/
static async loadFromExchange(
connection: Connection,
exchange: IExchange
): Promise<StableSwap> {
return StableSwap.load(
connection,
exchange.swapAccount,
exchange.programID
);
}
/**
* Loads the swap object from a program account.
* @param data
* @returns
*/
static async fromProgramAccount(
data: ProgramAccount<StableSwapState>
): Promise<StableSwap> {
const [authority] = await findSwapAuthorityKey(data.publicKey);
return StableSwap.fromProgramAccountWithAuthority(data, authority);
}
/**
* Loads the swap object from a program account.
* @param data
* @returns
*/
static fromData(data: ProgramAccount<StableSwapState>): StableSwap {
const authority = getSwapAuthorityKey(data.publicKey);
return StableSwap.fromProgramAccountWithAuthority(data, authority);
}
/**
* Loads the swap object from a program account, with the swap authority loaded.
* @param data
* @returns
*/
static fromProgramAccountWithAuthority(
data: ProgramAccount<StableSwapState>,
authority: PublicKey
): StableSwap {
return new StableSwap(
{
swapAccount: data.publicKey,
swapProgramID: SWAP_PROGRAM_ID,
tokenProgramID: TOKEN_PROGRAM_ID,
authority,
},
data.account
);
}
/**
* Loads a StableSwap instance with data.
*
* @param programID The program ID.
* @param swapAccount The address of the swap.
* @param swapAccountData The data of the swapAccount.
* @param authority The swap's authority.
* @returns
*/
static loadWithData(
swapAccount: PublicKey,
swapAccountData: Buffer,
authority: PublicKey,
programID: PublicKey = SWAP_PROGRAM_ID
): StableSwap {
try {
const state = decodeSwap(swapAccountData);
if (!state.isInitialized) {
throw new Error(`Invalid token swap state`);
}
return new StableSwap(
{
swapAccount: swapAccount,
swapProgramID: programID,
tokenProgramID: TOKEN_PROGRAM_ID,
authority,
},
state
);
} catch (e) {
throw Error(e as string);
}
}
/**
* Swap token A for token B
* @param userSource
* @param poolSource
* @param poolDestination
* @param userDestination
* @param amountIn
* @param minimumAmountOut
*/
swap(
args: Pick<
instructions.SwapInstruction,
| "userAuthority"
| "userSource"
| "userDestination"
| "poolSource"
| "poolDestination"
| "amountIn"
| "minimumAmountOut"
>
): TransactionInstruction {
return createSaberSwapInstruction(this, args);
}
/**
* Deposit tokens into the pool.
*/
deposit(
args: Pick<
instructions.DepositInstruction,
| "userAuthority"
| "sourceA"
| "sourceB"
| "poolTokenAccount"
| "tokenAmountA"
| "tokenAmountB"
| "minimumPoolTokenAmount"
>
): TransactionInstruction {
return createSaberDepositInstruction(this, args);
}
/**
* Withdraw tokens from the pool
*/
withdraw(
args: Pick<
instructions.WithdrawInstruction,
| "userAuthority"
| "userAccountA"
| "userAccountB"
| "sourceAccount"
| "poolTokenAmount"
| "minimumTokenA"
| "minimumTokenB"
>
): TransactionInstruction {
return createSaberWithdrawInstruction(this, args);
}
/**
* Withdraw tokens from the pool
*/
withdrawOne(
args: Pick<
instructions.WithdrawOneInstruction,
| "userAuthority"
| "baseTokenAccount"
| "destinationAccount"
| "sourceAccount"
| "poolTokenAmount"
| "minimumTokenAmount"
>
): TransactionInstruction {
return createSaberWithdrawOneInstruction(this, args);
}
}
/**
* Finds the swap authority address that is used to sign transactions on behalf of the swap.
*
* @param swapAccount
* @param swapProgramID
* @returns
*/
export const findSwapAuthorityKey = (
swapAccount: PublicKey,
swapProgramID: PublicKey = SWAP_PROGRAM_ID
): Promise<[PublicKey, number]> =>
PublicKey.findProgramAddress([swapAccount.toBuffer()], swapProgramID);
/**
* Finds the swap authority address that is used to sign transactions on behalf of the swap.
*
* @param swapAccount
* @param swapProgramID
* @returns
*/
export const getSwapAuthorityKey = (
swapAccount: PublicKey,
swapProgramID: PublicKey = SWAP_PROGRAM_ID
): PublicKey => getProgramAddress([swapAccount.toBuffer()], swapProgramID);