@thru/programs 0.3.16 → 0.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/amm/index.ts CHANGED
@@ -85,6 +85,7 @@ export const AMM_ERROR_LABELS: Record<number, string> = {
85
85
  17: 'Liquidity bounds',
86
86
  18: 'Vault mismatch',
87
87
  19: 'LP mint mismatch',
88
+ 20: 'Slippage exceeded',
88
89
  };
89
90
 
90
91
  const PUBKEY_LENGTH = 32;
@@ -169,6 +170,8 @@ export interface AddLiquidityArgs {
169
170
  tokenProgramAccountBytes: Uint8Array;
170
171
  maxAmountMintOne: bigint;
171
172
  maxAmountMintTwo: bigint;
173
+ /** Inclusive minimum LP output; zero explicitly disables this bound. */
174
+ minLpOut: bigint;
172
175
  }
173
176
 
174
177
  export interface WithdrawLiquidityArgs {
@@ -182,6 +185,10 @@ export interface WithdrawLiquidityArgs {
182
185
  lpMintAccountBytes: Uint8Array;
183
186
  tokenProgramAccountBytes: Uint8Array;
184
187
  lpAmount: bigint;
188
+ /** Inclusive minimum token-one output; zero explicitly disables this bound. */
189
+ minAmountOneOut: bigint;
190
+ /** Inclusive minimum token-two output; zero explicitly disables this bound. */
191
+ minAmountTwoOut: bigint;
185
192
  }
186
193
 
187
194
  export interface SwapArgs {
@@ -194,6 +201,8 @@ export interface SwapArgs {
194
201
  lpMintAccountBytes: Uint8Array;
195
202
  tokenProgramAccountBytes: Uint8Array;
196
203
  amountIn: bigint;
204
+ /** Inclusive minimum swap output; zero explicitly disables this bound. */
205
+ minAmountOut: bigint;
197
206
  }
198
207
 
199
208
  export function sortAmmMints(
@@ -292,6 +301,7 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
292
301
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
293
302
  assertU64(args.maxAmountMintOne, 'maxAmountMintOne');
294
303
  assertU64(args.maxAmountMintTwo, 'maxAmountMintTwo');
304
+ assertU64(args.minLpOut, 'minLpOut');
295
305
  const payload = new AmmAddLiquidityInstructionBuilder()
296
306
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
297
307
  .set_depositor_account_idx(accountIndex(context, args.depositorAccountBytes))
@@ -304,6 +314,7 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
304
314
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
305
315
  .set_max_amount_mint_one(args.maxAmountMintOne)
306
316
  .set_max_amount_mint_two(args.maxAmountMintTwo)
317
+ .set_min_lp_out(args.minLpOut)
307
318
  .build();
308
319
  return buildAmmInstruction('add_liquidity', payload);
309
320
  };
@@ -312,6 +323,8 @@ export function createAddLiquidityInstruction(args: AddLiquidityArgs): Instructi
312
323
  export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs): InstructionData {
313
324
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
314
325
  assertU64(args.lpAmount, 'lpAmount');
326
+ assertU64(args.minAmountOneOut, 'minAmountOneOut');
327
+ assertU64(args.minAmountTwoOut, 'minAmountTwoOut');
315
328
  const payload = new AmmWithdrawLiquidityInstructionBuilder()
316
329
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
317
330
  .set_withdrawer_account_idx(accountIndex(context, args.withdrawerAccountBytes))
@@ -323,6 +336,8 @@ export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs):
323
336
  .set_lp_mint_account_idx(accountIndex(context, args.lpMintAccountBytes))
324
337
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
325
338
  .set_lp_amount(args.lpAmount)
339
+ .set_min_amount_one_out(args.minAmountOneOut)
340
+ .set_min_amount_two_out(args.minAmountTwoOut)
326
341
  .build();
327
342
  return buildAmmInstruction('withdraw_liquidity', payload);
328
343
  };
@@ -331,6 +346,7 @@ export function createWithdrawLiquidityInstruction(args: WithdrawLiquidityArgs):
331
346
  export function createSwapInstruction(args: SwapArgs): InstructionData {
332
347
  return async (context: AccountLookupContext): Promise<Uint8Array> => {
333
348
  assertU64(args.amountIn, 'amountIn');
349
+ assertU64(args.minAmountOut, 'minAmountOut');
334
350
  const payload = new AmmSwapInstructionBuilder()
335
351
  .set_pool_account_idx(accountIndex(context, args.poolAccountBytes))
336
352
  .set_user_transfer_authority_idx(accountIndex(context, args.userTransferAuthorityBytes))
@@ -341,6 +357,7 @@ export function createSwapInstruction(args: SwapArgs): InstructionData {
341
357
  .set_lp_mint_account_idx(accountIndex(context, args.lpMintAccountBytes))
342
358
  .set_token_program_account_idx(accountIndex(context, args.tokenProgramAccountBytes))
343
359
  .set_amount_in(args.amountIn)
360
+ .set_min_amount_out(args.minAmountOut)
344
361
  .build();
345
362
  return buildAmmInstruction('swap', payload);
346
363
  };
@@ -400,7 +417,7 @@ function assertU16(value: number, label: string): void {
400
417
  }
401
418
 
402
419
  function assertU64(value: bigint, label: string): void {
403
- if (value < 0n || value > 0xffffffffffffffffn) {
420
+ if (typeof value !== 'bigint' || value < 0n || value > 0xffffffffffffffffn) {
404
421
  throw new Error(`${label} must be between 0 and 18446744073709551615`);
405
422
  }
406
423
  }