@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/dist/amm/index.cjs +235 -28
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +34 -6
- package/dist/amm/index.d.ts +34 -6
- package/dist/amm/index.js +235 -28
- package/dist/amm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/amm/abi/thru/program/amm/types.ts +282 -23
- package/src/amm/index.test.ts +93 -3
- package/src/amm/index.ts +18 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thru/programs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.17",
|
|
4
4
|
"description": "Typed TypeScript clients for Thru's core on-chain programs: token, AMM, CLOB, oracle, multicall, passkey manager, and others.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://thru.org/docs/sdks/web-packages/programs/",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@thru/sdk": "0.3.
|
|
87
|
+
"@thru/sdk": "0.3.17"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@types/node": "^25.9.1",
|
|
@@ -12,11 +12,28 @@ type __TnIrNode =
|
|
|
12
12
|
readonly left: __TnIrNode;
|
|
13
13
|
readonly right: __TnIrNode;
|
|
14
14
|
}
|
|
15
|
+
| {
|
|
16
|
+
readonly op: "sub";
|
|
17
|
+
readonly left: __TnIrNode;
|
|
18
|
+
readonly right: __TnIrNode;
|
|
19
|
+
}
|
|
15
20
|
| {
|
|
16
21
|
readonly op: "mul";
|
|
17
22
|
readonly left: __TnIrNode;
|
|
18
23
|
readonly right: __TnIrNode;
|
|
19
24
|
}
|
|
25
|
+
| {
|
|
26
|
+
readonly op:
|
|
27
|
+
| "div"
|
|
28
|
+
| "mod"
|
|
29
|
+
| "bitAnd"
|
|
30
|
+
| "bitOr"
|
|
31
|
+
| "bitXor"
|
|
32
|
+
| "leftShift"
|
|
33
|
+
| "rightShift";
|
|
34
|
+
readonly left: __TnIrNode;
|
|
35
|
+
readonly right: __TnIrNode;
|
|
36
|
+
}
|
|
20
37
|
| {
|
|
21
38
|
readonly op: "align";
|
|
22
39
|
readonly alignment: number;
|
|
@@ -314,6 +331,32 @@ function __tnCheckedAdd(lhs: bigint, rhs: bigint): bigint {
|
|
|
314
331
|
return (sum as unknown) as bigint;
|
|
315
332
|
}
|
|
316
333
|
|
|
334
|
+
function __tnCheckedSub(lhs: bigint, rhs: bigint): bigint {
|
|
335
|
+
if (__tnHasNativeBigInt) {
|
|
336
|
+
const result = (lhs as bigint) - (rhs as bigint);
|
|
337
|
+
if (result < BigInt(0)) {
|
|
338
|
+
__tnRaiseIrError(
|
|
339
|
+
"tn.ir.overflow",
|
|
340
|
+
"IR runtime detected negative size via subtraction"
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
const left = lhs as unknown as number;
|
|
346
|
+
const right = rhs as unknown as number;
|
|
347
|
+
const diff = left - right;
|
|
348
|
+
if (diff < 0 || !Number.isFinite(diff)) {
|
|
349
|
+
__tnRaiseIrError(
|
|
350
|
+
"tn.ir.overflow",
|
|
351
|
+
"IR runtime detected invalid subtraction result"
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
if (!Number.isSafeInteger(diff)) {
|
|
355
|
+
__tnWarnOnce("[thru-net] Precision loss while polyfilling BigInt subtraction");
|
|
356
|
+
}
|
|
357
|
+
return (diff as unknown) as bigint;
|
|
358
|
+
}
|
|
359
|
+
|
|
317
360
|
function __tnCheckedMul(lhs: bigint, rhs: bigint): bigint {
|
|
318
361
|
if (__tnHasNativeBigInt) {
|
|
319
362
|
const result = (lhs as bigint) * (rhs as bigint);
|
|
@@ -342,6 +385,74 @@ function __tnCheckedMul(lhs: bigint, rhs: bigint): bigint {
|
|
|
342
385
|
return (product as unknown) as bigint;
|
|
343
386
|
}
|
|
344
387
|
|
|
388
|
+
function __tnCheckedDiv(lhs: bigint, rhs: bigint): bigint {
|
|
389
|
+
if (__tnBigIntEquals(rhs, __tnToBigInt(0))) {
|
|
390
|
+
__tnRaiseIrError("tn.ir.overflow", "IR runtime division by zero");
|
|
391
|
+
}
|
|
392
|
+
if (__tnHasNativeBigInt) return (lhs as bigint) / (rhs as bigint);
|
|
393
|
+
const quotient = Math.floor((lhs as unknown as number) / (rhs as unknown as number));
|
|
394
|
+
return (quotient as unknown) as bigint;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function __tnCheckedMod(lhs: bigint, rhs: bigint): bigint {
|
|
398
|
+
if (__tnBigIntEquals(rhs, __tnToBigInt(0))) {
|
|
399
|
+
__tnRaiseIrError("tn.ir.overflow", "IR runtime modulo by zero");
|
|
400
|
+
}
|
|
401
|
+
if (__tnHasNativeBigInt) return (lhs as bigint) % (rhs as bigint);
|
|
402
|
+
return (((lhs as unknown as number) % (rhs as unknown as number)) as unknown) as bigint;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function __tnBitwise(
|
|
406
|
+
lhs: bigint,
|
|
407
|
+
rhs: bigint,
|
|
408
|
+
op: "and" | "or" | "xor"
|
|
409
|
+
): bigint {
|
|
410
|
+
if (__tnHasNativeBigInt) {
|
|
411
|
+
if (op === "and") return (lhs as bigint) & (rhs as bigint);
|
|
412
|
+
if (op === "or") return (lhs as bigint) | (rhs as bigint);
|
|
413
|
+
return (lhs as bigint) ^ (rhs as bigint);
|
|
414
|
+
}
|
|
415
|
+
const left = lhs as unknown as number;
|
|
416
|
+
const right = rhs as unknown as number;
|
|
417
|
+
const maxU32 = 0xffffffff;
|
|
418
|
+
if (
|
|
419
|
+
!Number.isInteger(left) ||
|
|
420
|
+
!Number.isInteger(right) ||
|
|
421
|
+
left < 0 ||
|
|
422
|
+
right < 0 ||
|
|
423
|
+
left > maxU32 ||
|
|
424
|
+
right > maxU32
|
|
425
|
+
) {
|
|
426
|
+
__tnRaiseIrError(
|
|
427
|
+
"tn.ir.overflow",
|
|
428
|
+
"IR runtime bitwise operation requires BigInt for values outside u32 range"
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
const result = op === "and" ? left & right : op === "or" ? left | right : left ^ right;
|
|
432
|
+
return ((result >>> 0) as unknown) as bigint;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function __tnCheckedShift(
|
|
436
|
+
lhs: bigint,
|
|
437
|
+
rhs: bigint,
|
|
438
|
+
direction: "left" | "right"
|
|
439
|
+
): bigint {
|
|
440
|
+
const amount = __tnBigIntToNumber(rhs, "IR shift amount");
|
|
441
|
+
if (amount < 0 || amount >= 64 || !Number.isInteger(amount)) {
|
|
442
|
+
__tnRaiseIrError("tn.ir.overflow", "IR runtime invalid shift amount");
|
|
443
|
+
}
|
|
444
|
+
if (__tnHasNativeBigInt) {
|
|
445
|
+
const shift = BigInt(amount);
|
|
446
|
+
return direction === "left" ? (lhs as bigint) << shift : (lhs as bigint) >> shift;
|
|
447
|
+
}
|
|
448
|
+
const value = lhs as unknown as number;
|
|
449
|
+
const result = direction === "left" ? value * 2 ** amount : Math.floor(value / 2 ** amount);
|
|
450
|
+
if (!Number.isSafeInteger(result)) {
|
|
451
|
+
__tnWarnOnce("[thru-net] Precision loss while polyfilling BigInt shift");
|
|
452
|
+
}
|
|
453
|
+
return (result as unknown) as bigint;
|
|
454
|
+
}
|
|
455
|
+
|
|
345
456
|
function __tnAlign(value: bigint, alignment: number): bigint {
|
|
346
457
|
if (alignment <= 1) return value;
|
|
347
458
|
const alignBig = __tnToBigInt(alignment);
|
|
@@ -640,6 +751,9 @@ function __tnEvalIrNode(
|
|
|
640
751
|
case "const":
|
|
641
752
|
return node.value;
|
|
642
753
|
case "field": {
|
|
754
|
+
if (node.param === "__buffer_size" && ctx.buffer) {
|
|
755
|
+
return __tnToBigInt(ctx.buffer.length);
|
|
756
|
+
}
|
|
643
757
|
const val = ctx.params[node.param];
|
|
644
758
|
if (val === undefined) {
|
|
645
759
|
const prefix = ctx.typeName ? `${ctx.typeName}: ` : "";
|
|
@@ -660,11 +774,56 @@ function __tnEvalIrNode(
|
|
|
660
774
|
);
|
|
661
775
|
return __tnCheckedAdd(left, right);
|
|
662
776
|
}
|
|
777
|
+
case "sub":
|
|
778
|
+
return __tnCheckedSub(
|
|
779
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
780
|
+
__tnEvalIrNode(node.right, ctx, baseOffset)
|
|
781
|
+
);
|
|
663
782
|
case "mul":
|
|
664
783
|
return __tnCheckedMul(
|
|
665
784
|
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
666
785
|
__tnEvalIrNode(node.right, ctx, baseOffset)
|
|
667
786
|
);
|
|
787
|
+
case "div":
|
|
788
|
+
return __tnCheckedDiv(
|
|
789
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
790
|
+
__tnEvalIrNode(node.right, ctx, baseOffset)
|
|
791
|
+
);
|
|
792
|
+
case "mod":
|
|
793
|
+
return __tnCheckedMod(
|
|
794
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
795
|
+
__tnEvalIrNode(node.right, ctx, baseOffset)
|
|
796
|
+
);
|
|
797
|
+
case "bitAnd":
|
|
798
|
+
return __tnBitwise(
|
|
799
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
800
|
+
__tnEvalIrNode(node.right, ctx, baseOffset),
|
|
801
|
+
"and"
|
|
802
|
+
);
|
|
803
|
+
case "bitOr":
|
|
804
|
+
return __tnBitwise(
|
|
805
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
806
|
+
__tnEvalIrNode(node.right, ctx, baseOffset),
|
|
807
|
+
"or"
|
|
808
|
+
);
|
|
809
|
+
case "bitXor":
|
|
810
|
+
return __tnBitwise(
|
|
811
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
812
|
+
__tnEvalIrNode(node.right, ctx, baseOffset),
|
|
813
|
+
"xor"
|
|
814
|
+
);
|
|
815
|
+
case "leftShift":
|
|
816
|
+
return __tnCheckedShift(
|
|
817
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
818
|
+
__tnEvalIrNode(node.right, ctx, baseOffset),
|
|
819
|
+
"left"
|
|
820
|
+
);
|
|
821
|
+
case "rightShift":
|
|
822
|
+
return __tnCheckedShift(
|
|
823
|
+
__tnEvalIrNode(node.left, ctx, baseOffset),
|
|
824
|
+
__tnEvalIrNode(node.right, ctx, baseOffset),
|
|
825
|
+
"right"
|
|
826
|
+
);
|
|
668
827
|
case "align":
|
|
669
828
|
return __tnAlign(__tnEvalIrNode(node.node, ctx, baseOffset), node.alignment);
|
|
670
829
|
case "switch": {
|
|
@@ -797,7 +956,7 @@ __tnRegisterDynamicValidate("Pubkey", (buffer) => { const result = Pubkey.valida
|
|
|
797
956
|
|
|
798
957
|
const __tn_ir_AmmAddLiquidityInstruction = {
|
|
799
958
|
typeName: "AmmAddLiquidityInstruction",
|
|
800
|
-
root: { op: "const", value:
|
|
959
|
+
root: { op: "const", value: 42n }
|
|
801
960
|
} as const;
|
|
802
961
|
|
|
803
962
|
export class AmmAddLiquidityInstruction {
|
|
@@ -1019,6 +1178,24 @@ export class AmmAddLiquidityInstruction {
|
|
|
1019
1178
|
this.set_max_amount_mint_two(value);
|
|
1020
1179
|
}
|
|
1021
1180
|
|
|
1181
|
+
get_min_lp_out(): bigint {
|
|
1182
|
+
const offset = 34;
|
|
1183
|
+
return this.view.getBigUint64(offset, true); /* little-endian */
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
set_min_lp_out(value: bigint): void {
|
|
1187
|
+
const offset = 34;
|
|
1188
|
+
this.view.setBigUint64(offset, value, true); /* little-endian */
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
get min_lp_out(): bigint {
|
|
1192
|
+
return this.get_min_lp_out();
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
set min_lp_out(value: bigint) {
|
|
1196
|
+
this.set_min_lp_out(value);
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1022
1199
|
private static __tnFootprintInternal(__tnParams: Record<string, bigint>): bigint {
|
|
1023
1200
|
return __tnEvalFootprint(__tn_ir_AmmAddLiquidityInstruction.root, { params: __tnParams });
|
|
1024
1201
|
}
|
|
@@ -1049,12 +1226,12 @@ export class AmmAddLiquidityInstruction {
|
|
|
1049
1226
|
}
|
|
1050
1227
|
|
|
1051
1228
|
static validate(buffer: Uint8Array, _opts?: { params?: never }): { ok: boolean; code?: string; consumed?: number } {
|
|
1052
|
-
if (buffer.length <
|
|
1053
|
-
return { ok: true, consumed:
|
|
1229
|
+
if (buffer.length < 42) return { ok: false, code: "tn.buffer_too_small", consumed: 42 };
|
|
1230
|
+
return { ok: true, consumed: 42 };
|
|
1054
1231
|
}
|
|
1055
1232
|
|
|
1056
|
-
static new(pool_account_idx: number, depositor_account_idx: number, depositor_token_one_account_idx: number, depositor_token_two_account_idx: number, depositor_lp_account_idx: number, vault_one_account_idx: number, vault_two_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, max_amount_mint_one: bigint, max_amount_mint_two: bigint): AmmAddLiquidityInstruction {
|
|
1057
|
-
const buffer = new Uint8Array(
|
|
1233
|
+
static new(pool_account_idx: number, depositor_account_idx: number, depositor_token_one_account_idx: number, depositor_token_two_account_idx: number, depositor_lp_account_idx: number, vault_one_account_idx: number, vault_two_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, max_amount_mint_one: bigint, max_amount_mint_two: bigint, min_lp_out: bigint): AmmAddLiquidityInstruction {
|
|
1234
|
+
const buffer = new Uint8Array(42);
|
|
1058
1235
|
const view = new DataView(buffer.buffer);
|
|
1059
1236
|
|
|
1060
1237
|
let offset = 0;
|
|
@@ -1069,6 +1246,7 @@ export class AmmAddLiquidityInstruction {
|
|
|
1069
1246
|
view.setUint16(16, token_program_account_idx, true); /* token_program_account_idx (little-endian) */
|
|
1070
1247
|
view.setBigUint64(18, max_amount_mint_one, true); /* max_amount_mint_one (little-endian) */
|
|
1071
1248
|
view.setBigUint64(26, max_amount_mint_two, true); /* max_amount_mint_two (little-endian) */
|
|
1249
|
+
view.setBigUint64(34, min_lp_out, true); /* min_lp_out (little-endian) */
|
|
1072
1250
|
|
|
1073
1251
|
return new AmmAddLiquidityInstruction(buffer);
|
|
1074
1252
|
}
|
|
@@ -1092,7 +1270,7 @@ export class AmmAddLiquidityInstructionBuilder {
|
|
|
1092
1270
|
private view: DataView;
|
|
1093
1271
|
|
|
1094
1272
|
constructor() {
|
|
1095
|
-
this.buffer = new Uint8Array(
|
|
1273
|
+
this.buffer = new Uint8Array(42);
|
|
1096
1274
|
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
1097
1275
|
}
|
|
1098
1276
|
|
|
@@ -1153,6 +1331,12 @@ export class AmmAddLiquidityInstructionBuilder {
|
|
|
1153
1331
|
return this;
|
|
1154
1332
|
}
|
|
1155
1333
|
|
|
1334
|
+
set_min_lp_out(value: bigint): this {
|
|
1335
|
+
const cast = __tnToBigInt(value);
|
|
1336
|
+
this.view.setBigUint64(34, cast, true);
|
|
1337
|
+
return this;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1156
1340
|
build(): Uint8Array {
|
|
1157
1341
|
return this.buffer.slice();
|
|
1158
1342
|
}
|
|
@@ -1318,7 +1502,7 @@ __tnRegisterDynamicValidate("AmmError", (buffer) => { const result = AmmError.va
|
|
|
1318
1502
|
|
|
1319
1503
|
const __tn_ir_AmmSwapInstruction = {
|
|
1320
1504
|
typeName: "AmmSwapInstruction",
|
|
1321
|
-
root: { op: "const", value:
|
|
1505
|
+
root: { op: "const", value: 32n }
|
|
1322
1506
|
} as const;
|
|
1323
1507
|
|
|
1324
1508
|
export class AmmSwapInstruction {
|
|
@@ -1504,6 +1688,24 @@ export class AmmSwapInstruction {
|
|
|
1504
1688
|
this.set_amount_in(value);
|
|
1505
1689
|
}
|
|
1506
1690
|
|
|
1691
|
+
get_min_amount_out(): bigint {
|
|
1692
|
+
const offset = 24;
|
|
1693
|
+
return this.view.getBigUint64(offset, true); /* little-endian */
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
set_min_amount_out(value: bigint): void {
|
|
1697
|
+
const offset = 24;
|
|
1698
|
+
this.view.setBigUint64(offset, value, true); /* little-endian */
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
get min_amount_out(): bigint {
|
|
1702
|
+
return this.get_min_amount_out();
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
set min_amount_out(value: bigint) {
|
|
1706
|
+
this.set_min_amount_out(value);
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1507
1709
|
private static __tnFootprintInternal(__tnParams: Record<string, bigint>): bigint {
|
|
1508
1710
|
return __tnEvalFootprint(__tn_ir_AmmSwapInstruction.root, { params: __tnParams });
|
|
1509
1711
|
}
|
|
@@ -1534,12 +1736,12 @@ export class AmmSwapInstruction {
|
|
|
1534
1736
|
}
|
|
1535
1737
|
|
|
1536
1738
|
static validate(buffer: Uint8Array, _opts?: { params?: never }): { ok: boolean; code?: string; consumed?: number } {
|
|
1537
|
-
if (buffer.length <
|
|
1538
|
-
return { ok: true, consumed:
|
|
1739
|
+
if (buffer.length < 32) return { ok: false, code: "tn.buffer_too_small", consumed: 32 };
|
|
1740
|
+
return { ok: true, consumed: 32 };
|
|
1539
1741
|
}
|
|
1540
1742
|
|
|
1541
|
-
static new(pool_account_idx: number, user_transfer_authority_idx: number, user_input_account_idx: number, user_output_account_idx: number, vault_input_account_idx: number, vault_output_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, amount_in: bigint): AmmSwapInstruction {
|
|
1542
|
-
const buffer = new Uint8Array(
|
|
1743
|
+
static new(pool_account_idx: number, user_transfer_authority_idx: number, user_input_account_idx: number, user_output_account_idx: number, vault_input_account_idx: number, vault_output_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, amount_in: bigint, min_amount_out: bigint): AmmSwapInstruction {
|
|
1744
|
+
const buffer = new Uint8Array(32);
|
|
1543
1745
|
const view = new DataView(buffer.buffer);
|
|
1544
1746
|
|
|
1545
1747
|
let offset = 0;
|
|
@@ -1552,6 +1754,7 @@ export class AmmSwapInstruction {
|
|
|
1552
1754
|
view.setUint16(12, lp_mint_account_idx, true); /* lp_mint_account_idx (little-endian) */
|
|
1553
1755
|
view.setUint16(14, token_program_account_idx, true); /* token_program_account_idx (little-endian) */
|
|
1554
1756
|
view.setBigUint64(16, amount_in, true); /* amount_in (little-endian) */
|
|
1757
|
+
view.setBigUint64(24, min_amount_out, true); /* min_amount_out (little-endian) */
|
|
1555
1758
|
|
|
1556
1759
|
return new AmmSwapInstruction(buffer);
|
|
1557
1760
|
}
|
|
@@ -1575,7 +1778,7 @@ export class AmmSwapInstructionBuilder {
|
|
|
1575
1778
|
private view: DataView;
|
|
1576
1779
|
|
|
1577
1780
|
constructor() {
|
|
1578
|
-
this.buffer = new Uint8Array(
|
|
1781
|
+
this.buffer = new Uint8Array(32);
|
|
1579
1782
|
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
1580
1783
|
}
|
|
1581
1784
|
|
|
@@ -1625,6 +1828,12 @@ export class AmmSwapInstructionBuilder {
|
|
|
1625
1828
|
return this;
|
|
1626
1829
|
}
|
|
1627
1830
|
|
|
1831
|
+
set_min_amount_out(value: bigint): this {
|
|
1832
|
+
const cast = __tnToBigInt(value);
|
|
1833
|
+
this.view.setBigUint64(24, cast, true);
|
|
1834
|
+
return this;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1628
1837
|
build(): Uint8Array {
|
|
1629
1838
|
return this.buffer.slice();
|
|
1630
1839
|
}
|
|
@@ -1650,7 +1859,7 @@ __tnRegisterDynamicValidate("AmmSwapInstruction", (buffer) => { const result = A
|
|
|
1650
1859
|
|
|
1651
1860
|
const __tn_ir_AmmWithdrawLiquidityInstruction = {
|
|
1652
1861
|
typeName: "AmmWithdrawLiquidityInstruction",
|
|
1653
|
-
root: { op: "const", value:
|
|
1862
|
+
root: { op: "const", value: 42n }
|
|
1654
1863
|
} as const;
|
|
1655
1864
|
|
|
1656
1865
|
export class AmmWithdrawLiquidityInstruction {
|
|
@@ -1854,6 +2063,42 @@ export class AmmWithdrawLiquidityInstruction {
|
|
|
1854
2063
|
this.set_lp_amount(value);
|
|
1855
2064
|
}
|
|
1856
2065
|
|
|
2066
|
+
get_min_amount_one_out(): bigint {
|
|
2067
|
+
const offset = 26;
|
|
2068
|
+
return this.view.getBigUint64(offset, true); /* little-endian */
|
|
2069
|
+
}
|
|
2070
|
+
|
|
2071
|
+
set_min_amount_one_out(value: bigint): void {
|
|
2072
|
+
const offset = 26;
|
|
2073
|
+
this.view.setBigUint64(offset, value, true); /* little-endian */
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
get min_amount_one_out(): bigint {
|
|
2077
|
+
return this.get_min_amount_one_out();
|
|
2078
|
+
}
|
|
2079
|
+
|
|
2080
|
+
set min_amount_one_out(value: bigint) {
|
|
2081
|
+
this.set_min_amount_one_out(value);
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
get_min_amount_two_out(): bigint {
|
|
2085
|
+
const offset = 34;
|
|
2086
|
+
return this.view.getBigUint64(offset, true); /* little-endian */
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
set_min_amount_two_out(value: bigint): void {
|
|
2090
|
+
const offset = 34;
|
|
2091
|
+
this.view.setBigUint64(offset, value, true); /* little-endian */
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
get min_amount_two_out(): bigint {
|
|
2095
|
+
return this.get_min_amount_two_out();
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
set min_amount_two_out(value: bigint) {
|
|
2099
|
+
this.set_min_amount_two_out(value);
|
|
2100
|
+
}
|
|
2101
|
+
|
|
1857
2102
|
private static __tnFootprintInternal(__tnParams: Record<string, bigint>): bigint {
|
|
1858
2103
|
return __tnEvalFootprint(__tn_ir_AmmWithdrawLiquidityInstruction.root, { params: __tnParams });
|
|
1859
2104
|
}
|
|
@@ -1884,12 +2129,12 @@ export class AmmWithdrawLiquidityInstruction {
|
|
|
1884
2129
|
}
|
|
1885
2130
|
|
|
1886
2131
|
static validate(buffer: Uint8Array, _opts?: { params?: never }): { ok: boolean; code?: string; consumed?: number } {
|
|
1887
|
-
if (buffer.length <
|
|
1888
|
-
return { ok: true, consumed:
|
|
2132
|
+
if (buffer.length < 42) return { ok: false, code: "tn.buffer_too_small", consumed: 42 };
|
|
2133
|
+
return { ok: true, consumed: 42 };
|
|
1889
2134
|
}
|
|
1890
2135
|
|
|
1891
|
-
static new(pool_account_idx: number, withdrawer_account_idx: number, withdrawer_token_one_account_idx: number, withdrawer_token_two_account_idx: number, withdrawer_lp_account_idx: number, vault_one_account_idx: number, vault_two_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, lp_amount: bigint): AmmWithdrawLiquidityInstruction {
|
|
1892
|
-
const buffer = new Uint8Array(
|
|
2136
|
+
static new(pool_account_idx: number, withdrawer_account_idx: number, withdrawer_token_one_account_idx: number, withdrawer_token_two_account_idx: number, withdrawer_lp_account_idx: number, vault_one_account_idx: number, vault_two_account_idx: number, lp_mint_account_idx: number, token_program_account_idx: number, lp_amount: bigint, min_amount_one_out: bigint, min_amount_two_out: bigint): AmmWithdrawLiquidityInstruction {
|
|
2137
|
+
const buffer = new Uint8Array(42);
|
|
1893
2138
|
const view = new DataView(buffer.buffer);
|
|
1894
2139
|
|
|
1895
2140
|
let offset = 0;
|
|
@@ -1903,6 +2148,8 @@ export class AmmWithdrawLiquidityInstruction {
|
|
|
1903
2148
|
view.setUint16(14, lp_mint_account_idx, true); /* lp_mint_account_idx (little-endian) */
|
|
1904
2149
|
view.setUint16(16, token_program_account_idx, true); /* token_program_account_idx (little-endian) */
|
|
1905
2150
|
view.setBigUint64(18, lp_amount, true); /* lp_amount (little-endian) */
|
|
2151
|
+
view.setBigUint64(26, min_amount_one_out, true); /* min_amount_one_out (little-endian) */
|
|
2152
|
+
view.setBigUint64(34, min_amount_two_out, true); /* min_amount_two_out (little-endian) */
|
|
1906
2153
|
|
|
1907
2154
|
return new AmmWithdrawLiquidityInstruction(buffer);
|
|
1908
2155
|
}
|
|
@@ -1926,7 +2173,7 @@ export class AmmWithdrawLiquidityInstructionBuilder {
|
|
|
1926
2173
|
private view: DataView;
|
|
1927
2174
|
|
|
1928
2175
|
constructor() {
|
|
1929
|
-
this.buffer = new Uint8Array(
|
|
2176
|
+
this.buffer = new Uint8Array(42);
|
|
1930
2177
|
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
1931
2178
|
}
|
|
1932
2179
|
|
|
@@ -1981,6 +2228,18 @@ export class AmmWithdrawLiquidityInstructionBuilder {
|
|
|
1981
2228
|
return this;
|
|
1982
2229
|
}
|
|
1983
2230
|
|
|
2231
|
+
set_min_amount_one_out(value: bigint): this {
|
|
2232
|
+
const cast = __tnToBigInt(value);
|
|
2233
|
+
this.view.setBigUint64(26, cast, true);
|
|
2234
|
+
return this;
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
set_min_amount_two_out(value: bigint): this {
|
|
2238
|
+
const cast = __tnToBigInt(value);
|
|
2239
|
+
this.view.setBigUint64(34, cast, true);
|
|
2240
|
+
return this;
|
|
2241
|
+
}
|
|
2242
|
+
|
|
1984
2243
|
build(): Uint8Array {
|
|
1985
2244
|
return this.buffer.slice();
|
|
1986
2245
|
}
|
|
@@ -3678,21 +3937,21 @@ export class AmmInstruction {
|
|
|
3678
3937
|
{
|
|
3679
3938
|
name: "add_liquidity",
|
|
3680
3939
|
tag: 1,
|
|
3681
|
-
payloadSize:
|
|
3940
|
+
payloadSize: 42,
|
|
3682
3941
|
payloadType: "AmmInstruction::payload::add_liquidity",
|
|
3683
3942
|
createPayloadBuilder: () => __tnMaybeCallBuilder(AmmAddLiquidityInstruction),
|
|
3684
3943
|
},
|
|
3685
3944
|
{
|
|
3686
3945
|
name: "withdraw_liquidity",
|
|
3687
3946
|
tag: 2,
|
|
3688
|
-
payloadSize:
|
|
3947
|
+
payloadSize: 42,
|
|
3689
3948
|
payloadType: "AmmInstruction::payload::withdraw_liquidity",
|
|
3690
3949
|
createPayloadBuilder: () => __tnMaybeCallBuilder(AmmWithdrawLiquidityInstruction),
|
|
3691
3950
|
},
|
|
3692
3951
|
{
|
|
3693
3952
|
name: "swap",
|
|
3694
3953
|
tag: 3,
|
|
3695
|
-
payloadSize:
|
|
3954
|
+
payloadSize: 32,
|
|
3696
3955
|
payloadType: "AmmInstruction::payload::swap",
|
|
3697
3956
|
createPayloadBuilder: () => __tnMaybeCallBuilder(AmmSwapInstruction),
|
|
3698
3957
|
},
|
|
@@ -3930,7 +4189,7 @@ export class AmmInstructionBuilder {
|
|
|
3930
4189
|
}
|
|
3931
4190
|
|
|
3932
4191
|
private __tnAssign_discriminant(value: number): void {
|
|
3933
|
-
this.__tnField_discriminant = value
|
|
4192
|
+
this.__tnField_discriminant = value;
|
|
3934
4193
|
this.__tnInvalidate();
|
|
3935
4194
|
}
|
|
3936
4195
|
|
|
@@ -5524,7 +5783,7 @@ export class AmmEventBuilder {
|
|
|
5524
5783
|
}
|
|
5525
5784
|
|
|
5526
5785
|
private __tnAssign_event_type(value: number): void {
|
|
5527
|
-
this.__tnField_event_type = value
|
|
5786
|
+
this.__tnField_event_type = value;
|
|
5528
5787
|
this.__tnInvalidate();
|
|
5529
5788
|
}
|
|
5530
5789
|
|
package/src/amm/index.test.ts
CHANGED
|
@@ -7,10 +7,13 @@ import {
|
|
|
7
7
|
AMM_INSTRUCTION_ADD_LIQUIDITY,
|
|
8
8
|
AMM_INSTRUCTION_INIT_POOL,
|
|
9
9
|
AMM_INSTRUCTION_SWAP,
|
|
10
|
+
AMM_INSTRUCTION_WITHDRAW_LIQUIDITY,
|
|
11
|
+
AmmInstruction,
|
|
10
12
|
AMM_POOL_METADATA_SIZE,
|
|
11
13
|
createAddLiquidityInstruction,
|
|
12
14
|
createInitPoolInstruction,
|
|
13
15
|
createSwapInstruction,
|
|
16
|
+
createWithdrawLiquidityInstruction,
|
|
14
17
|
deriveAmmPoolAddresses,
|
|
15
18
|
parseAmmPoolMetadata,
|
|
16
19
|
quoteAmmSwapExactIn,
|
|
@@ -166,7 +169,7 @@ describe('amm helpers', () => {
|
|
|
166
169
|
]);
|
|
167
170
|
});
|
|
168
171
|
|
|
169
|
-
it('
|
|
172
|
+
it('round-trips bounded instructions with the packed C wire layout', async () => {
|
|
170
173
|
const accounts = Array.from({ length: 10 }, (_, idx) => key(idx + 1));
|
|
171
174
|
const indexes = Object.fromEntries(accounts.map((account, idx) => [bytesToHex(account), idx + 2]));
|
|
172
175
|
const addLiquidity = await createAddLiquidityInstruction({
|
|
@@ -181,6 +184,7 @@ describe('amm helpers', () => {
|
|
|
181
184
|
tokenProgramAccountBytes: accounts[8],
|
|
182
185
|
maxAmountMintOne: 5_000_000n,
|
|
183
186
|
maxAmountMintTwo: 10_000_000n,
|
|
187
|
+
minLpOut: 4_000_000n,
|
|
184
188
|
})(context(indexes));
|
|
185
189
|
const swap = await createSwapInstruction({
|
|
186
190
|
poolAccountBytes: accounts[0],
|
|
@@ -192,19 +196,105 @@ describe('amm helpers', () => {
|
|
|
192
196
|
lpMintAccountBytes: accounts[6],
|
|
193
197
|
tokenProgramAccountBytes: accounts[7],
|
|
194
198
|
amountIn: 1_234_567n,
|
|
199
|
+
minAmountOut: 765_432n,
|
|
200
|
+
})(context(indexes));
|
|
201
|
+
const withdraw = await createWithdrawLiquidityInstruction({
|
|
202
|
+
poolAccountBytes: accounts[0],
|
|
203
|
+
withdrawerAccountBytes: accounts[1],
|
|
204
|
+
withdrawerTokenOneAccountBytes: accounts[2],
|
|
205
|
+
withdrawerTokenTwoAccountBytes: accounts[3],
|
|
206
|
+
withdrawerLpAccountBytes: accounts[4],
|
|
207
|
+
vaultOneAccountBytes: accounts[5],
|
|
208
|
+
vaultTwoAccountBytes: accounts[6],
|
|
209
|
+
lpMintAccountBytes: accounts[7],
|
|
210
|
+
tokenProgramAccountBytes: accounts[8],
|
|
211
|
+
lpAmount: 1_000n,
|
|
212
|
+
minAmountOneOut: 222n,
|
|
213
|
+
minAmountTwoOut: 333n,
|
|
195
214
|
})(context(indexes));
|
|
196
215
|
|
|
197
|
-
expect(addLiquidity.length).toBe(
|
|
198
|
-
expect(swap.length).toBe(
|
|
216
|
+
expect(addLiquidity.length).toBe(46);
|
|
217
|
+
expect(swap.length).toBe(36);
|
|
218
|
+
expect(withdraw.length).toBe(46);
|
|
199
219
|
expect(Array.from(addLiquidity.slice(0, 4))).toEqual([
|
|
200
220
|
AMM_INSTRUCTION_ADD_LIQUIDITY, 0, 0, 0,
|
|
201
221
|
]);
|
|
202
222
|
expect(Array.from(swap.slice(0, 4))).toEqual([
|
|
203
223
|
AMM_INSTRUCTION_SWAP, 0, 0, 0,
|
|
204
224
|
]);
|
|
225
|
+
expect(Array.from(withdraw.slice(0, 4))).toEqual([
|
|
226
|
+
AMM_INSTRUCTION_WITHDRAW_LIQUIDITY, 0, 0, 0,
|
|
227
|
+
]);
|
|
205
228
|
expect(new DataView(addLiquidity.buffer).getBigUint64(22, true)).toBe(5_000_000n);
|
|
206
229
|
expect(new DataView(addLiquidity.buffer).getBigUint64(30, true)).toBe(10_000_000n);
|
|
207
230
|
expect(new DataView(swap.buffer).getBigUint64(20, true)).toBe(1_234_567n);
|
|
231
|
+
expect(new DataView(addLiquidity.buffer).getBigUint64(38, true)).toBe(4_000_000n);
|
|
232
|
+
expect(new DataView(swap.buffer).getBigUint64(28, true)).toBe(765_432n);
|
|
233
|
+
expect(new DataView(withdraw.buffer).getBigUint64(22, true)).toBe(1_000n);
|
|
234
|
+
expect(new DataView(withdraw.buffer).getBigUint64(30, true)).toBe(222n);
|
|
235
|
+
expect(new DataView(withdraw.buffer).getBigUint64(38, true)).toBe(333n);
|
|
236
|
+
expect(AmmInstruction.from_array(addLiquidity)?.payload().asAddLiquidity()?.min_lp_out)
|
|
237
|
+
.toBe(4_000_000n);
|
|
238
|
+
expect(AmmInstruction.from_array(swap)?.payload().asSwap()?.min_amount_out)
|
|
239
|
+
.toBe(765_432n);
|
|
240
|
+
const withdrawal = AmmInstruction.from_array(withdraw)?.payload().asWithdrawLiquidity();
|
|
241
|
+
expect(withdrawal?.min_amount_one_out).toBe(222n);
|
|
242
|
+
expect(withdrawal?.min_amount_two_out).toBe(333n);
|
|
243
|
+
for (const [instruction, oldSize] of [[addLiquidity, 38], [swap, 28], [withdraw, 30]] as const) {
|
|
244
|
+
expect(() => AmmInstruction.from_array(instruction.subarray(0, oldSize))?.payload()).toThrow();
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('minimum output input bounds', () => {
|
|
249
|
+
const account = key(1);
|
|
250
|
+
const args = {
|
|
251
|
+
poolAccountBytes: account,
|
|
252
|
+
depositorAccountBytes: account,
|
|
253
|
+
depositorTokenOneAccountBytes: account,
|
|
254
|
+
depositorTokenTwoAccountBytes: account,
|
|
255
|
+
depositorLpAccountBytes: account,
|
|
256
|
+
withdrawerAccountBytes: account,
|
|
257
|
+
withdrawerTokenOneAccountBytes: account,
|
|
258
|
+
withdrawerTokenTwoAccountBytes: account,
|
|
259
|
+
withdrawerLpAccountBytes: account,
|
|
260
|
+
userTransferAuthorityBytes: account,
|
|
261
|
+
userInputAccountBytes: account,
|
|
262
|
+
userOutputAccountBytes: account,
|
|
263
|
+
vaultOneAccountBytes: account,
|
|
264
|
+
vaultTwoAccountBytes: account,
|
|
265
|
+
vaultInputAccountBytes: account,
|
|
266
|
+
vaultOutputAccountBytes: account,
|
|
267
|
+
lpMintAccountBytes: account,
|
|
268
|
+
tokenProgramAccountBytes: account,
|
|
269
|
+
maxAmountMintOne: 1n,
|
|
270
|
+
maxAmountMintTwo: 1n,
|
|
271
|
+
lpAmount: 1n,
|
|
272
|
+
amountIn: 1n,
|
|
273
|
+
minLpOut: 0n,
|
|
274
|
+
minAmountOneOut: 0n,
|
|
275
|
+
minAmountTwoOut: 0n,
|
|
276
|
+
minAmountOut: 0n,
|
|
277
|
+
};
|
|
278
|
+
const lookup = context({ [bytesToHex(account)]: 0 });
|
|
279
|
+
const bounds = [
|
|
280
|
+
{ create: createAddLiquidityInstruction, field: 'minLpOut', offset: 38 },
|
|
281
|
+
{ create: createWithdrawLiquidityInstruction, field: 'minAmountOneOut', offset: 30 },
|
|
282
|
+
{ create: createWithdrawLiquidityInstruction, field: 'minAmountTwoOut', offset: 38 },
|
|
283
|
+
{ create: createSwapInstruction, field: 'minAmountOut', offset: 28 },
|
|
284
|
+
] as const;
|
|
285
|
+
|
|
286
|
+
it.each(bounds)('encodes zero and maximum u64 for $field without truncation', async ({ create, field, offset }) => {
|
|
287
|
+
for (const minimum of [0n, 0xffffffffffffffffn]) {
|
|
288
|
+
const data = await create({ ...args, [field]: minimum })(lookup);
|
|
289
|
+
expect(new DataView(data.buffer).getBigUint64(offset, true)).toBe(minimum);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it.each(bounds)('rejects missing and out-of-range $field', async ({ create, field }) => {
|
|
294
|
+
for (const minimum of [undefined, -1n, 0x10000000000000000n]) {
|
|
295
|
+
await expect(create({ ...args, [field]: minimum })(lookup)).rejects.toThrow(field);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
208
298
|
});
|
|
209
299
|
|
|
210
300
|
it('parses pool metadata', () => {
|