@triadxyz/poseidons-protocol 0.4.4 → 0.4.6
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/game/index.d.ts +2 -1
- package/dist/game/index.js +7 -3
- package/dist/game/wheel.d.ts +5 -2
- package/dist/game/wheel.js +11 -8
- package/dist/types/idl_poseidons_protocol.json +99 -2
- package/dist/types/poseidons_protocol.d.ts +157 -2
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.js +3 -1
- package/dist/utils/getTokenBalance.d.ts +3 -0
- package/dist/utils/getTokenBalance.js +47 -0
- package/dist/utils/swap.d.ts +16 -0
- package/dist/utils/swap.js +108 -0
- package/package.json +2 -2
package/dist/game/index.d.ts
CHANGED
|
@@ -17,11 +17,12 @@ export default class Game {
|
|
|
17
17
|
* @param spins - The number of spins to add
|
|
18
18
|
* @returns The transaction signature or versioned transaction
|
|
19
19
|
*/
|
|
20
|
-
addSpin({ users, refer }: {
|
|
20
|
+
addSpin({ users, refer, isUsdc }: {
|
|
21
21
|
users: {
|
|
22
22
|
user: PublicKey;
|
|
23
23
|
spins: number;
|
|
24
24
|
}[];
|
|
25
25
|
refer: PublicKey;
|
|
26
|
+
isUsdc: boolean;
|
|
26
27
|
}): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
27
28
|
}
|
package/dist/game/index.js
CHANGED
|
@@ -30,9 +30,10 @@ class Game {
|
|
|
30
30
|
* @param spins - The number of spins to add
|
|
31
31
|
* @returns The transaction signature or versioned transaction
|
|
32
32
|
*/
|
|
33
|
-
addSpin({ users, refer }) {
|
|
33
|
+
addSpin({ users, refer, isUsdc }) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
35
|
const ixs = [];
|
|
36
|
+
const addressLookupTableAccounts = [];
|
|
36
37
|
for (const user of users) {
|
|
37
38
|
const userPDA = (0, pda_1.getUserPDA)(user.user);
|
|
38
39
|
try {
|
|
@@ -48,14 +49,17 @@ class Game {
|
|
|
48
49
|
.instruction());
|
|
49
50
|
}
|
|
50
51
|
ixs.push(yield this.program.methods
|
|
51
|
-
.addSpin(
|
|
52
|
+
.addSpin({
|
|
53
|
+
isUsdc,
|
|
54
|
+
spins: new anchor_1.BN(user.spins)
|
|
55
|
+
})
|
|
52
56
|
.accounts({
|
|
53
57
|
signer: this.program.provider.publicKey,
|
|
54
58
|
user: userPDA
|
|
55
59
|
})
|
|
56
60
|
.instruction());
|
|
57
61
|
}
|
|
58
|
-
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
62
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
|
|
59
63
|
});
|
|
60
64
|
}
|
|
61
65
|
}
|
package/dist/game/wheel.d.ts
CHANGED
|
@@ -44,10 +44,13 @@ export default class Wheel {
|
|
|
44
44
|
* @param available - The available quantity of the prize
|
|
45
45
|
* @returns The transaction signature or versioned transaction
|
|
46
46
|
*/
|
|
47
|
-
addWheelPrize(
|
|
47
|
+
addWheelPrize(prizes: {
|
|
48
48
|
id: number;
|
|
49
49
|
available: number;
|
|
50
|
-
|
|
50
|
+
rangeMin: number;
|
|
51
|
+
rangeMax: number;
|
|
52
|
+
wheelId: number;
|
|
53
|
+
}[]): Promise<string | import("@solana/web3.js").VersionedTransaction>;
|
|
51
54
|
/**
|
|
52
55
|
* Spin a wheel
|
|
53
56
|
* @param id - The id of the wheel
|
package/dist/game/wheel.js
CHANGED
|
@@ -81,20 +81,23 @@ class Wheel {
|
|
|
81
81
|
* @param available - The available quantity of the prize
|
|
82
82
|
* @returns The transaction signature or versioned transaction
|
|
83
83
|
*/
|
|
84
|
-
addWheelPrize(
|
|
84
|
+
addWheelPrize(prizes) {
|
|
85
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
const ixs = [
|
|
87
|
-
|
|
86
|
+
const ixs = [];
|
|
87
|
+
for (const prize of prizes) {
|
|
88
|
+
ixs.push(yield this.program.methods
|
|
88
89
|
.addWheelPrize({
|
|
89
|
-
id,
|
|
90
|
-
available: new anchor_1.BN(available)
|
|
90
|
+
id: prize.id,
|
|
91
|
+
available: new anchor_1.BN(prize.available),
|
|
92
|
+
rangeMin: new anchor_1.BN(prize.rangeMin),
|
|
93
|
+
rangeMax: new anchor_1.BN(prize.rangeMax)
|
|
91
94
|
})
|
|
92
95
|
.accounts({
|
|
93
96
|
signer: this.program.provider.publicKey,
|
|
94
|
-
wheel: (0, pda_1.getWheelPDA)(
|
|
97
|
+
wheel: (0, pda_1.getWheelPDA)(prize.wheelId)
|
|
95
98
|
})
|
|
96
|
-
.instruction()
|
|
97
|
-
|
|
99
|
+
.instruction());
|
|
100
|
+
}
|
|
98
101
|
return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
|
|
99
102
|
});
|
|
100
103
|
}
|
|
@@ -290,6 +290,75 @@
|
|
|
290
290
|
"name": "user",
|
|
291
291
|
"writable": true
|
|
292
292
|
},
|
|
293
|
+
{
|
|
294
|
+
"name": "mint",
|
|
295
|
+
"writable": true,
|
|
296
|
+
"address": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
"name": "user_ata",
|
|
300
|
+
"writable": true,
|
|
301
|
+
"pda": {
|
|
302
|
+
"seeds": [
|
|
303
|
+
{
|
|
304
|
+
"kind": "account",
|
|
305
|
+
"path": "signer"
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
"kind": "account",
|
|
309
|
+
"path": "token_program"
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
"kind": "account",
|
|
313
|
+
"path": "mint"
|
|
314
|
+
}
|
|
315
|
+
],
|
|
316
|
+
"program": {
|
|
317
|
+
"kind": "const",
|
|
318
|
+
"value": [
|
|
319
|
+
140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
|
|
320
|
+
13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
|
|
321
|
+
219, 233, 248, 89
|
|
322
|
+
]
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
"name": "squads_ata",
|
|
328
|
+
"writable": true,
|
|
329
|
+
"pda": {
|
|
330
|
+
"seeds": [
|
|
331
|
+
{
|
|
332
|
+
"kind": "account",
|
|
333
|
+
"path": "squads"
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
"kind": "account",
|
|
337
|
+
"path": "token_program"
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
"kind": "account",
|
|
341
|
+
"path": "mint"
|
|
342
|
+
}
|
|
343
|
+
],
|
|
344
|
+
"program": {
|
|
345
|
+
"kind": "const",
|
|
346
|
+
"value": [
|
|
347
|
+
140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
|
|
348
|
+
13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
|
|
349
|
+
219, 233, 248, 89
|
|
350
|
+
]
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
"name": "token_program",
|
|
356
|
+
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
"name": "associated_token_program",
|
|
360
|
+
"address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
|
|
361
|
+
},
|
|
293
362
|
{
|
|
294
363
|
"name": "system_program",
|
|
295
364
|
"address": "11111111111111111111111111111111"
|
|
@@ -297,8 +366,12 @@
|
|
|
297
366
|
],
|
|
298
367
|
"args": [
|
|
299
368
|
{
|
|
300
|
-
"name": "
|
|
301
|
-
"type":
|
|
369
|
+
"name": "args",
|
|
370
|
+
"type": {
|
|
371
|
+
"defined": {
|
|
372
|
+
"name": "AddSpinArgs"
|
|
373
|
+
}
|
|
374
|
+
}
|
|
302
375
|
}
|
|
303
376
|
]
|
|
304
377
|
},
|
|
@@ -1104,6 +1177,22 @@
|
|
|
1104
1177
|
]
|
|
1105
1178
|
}
|
|
1106
1179
|
},
|
|
1180
|
+
{
|
|
1181
|
+
"name": "AddSpinArgs",
|
|
1182
|
+
"type": {
|
|
1183
|
+
"kind": "struct",
|
|
1184
|
+
"fields": [
|
|
1185
|
+
{
|
|
1186
|
+
"name": "is_usdc",
|
|
1187
|
+
"type": "bool"
|
|
1188
|
+
},
|
|
1189
|
+
{
|
|
1190
|
+
"name": "spins",
|
|
1191
|
+
"type": "u64"
|
|
1192
|
+
}
|
|
1193
|
+
]
|
|
1194
|
+
}
|
|
1195
|
+
},
|
|
1107
1196
|
{
|
|
1108
1197
|
"name": "AddWheelPrizeArgs",
|
|
1109
1198
|
"type": {
|
|
@@ -1116,6 +1205,14 @@
|
|
|
1116
1205
|
{
|
|
1117
1206
|
"name": "available",
|
|
1118
1207
|
"type": "u64"
|
|
1208
|
+
},
|
|
1209
|
+
{
|
|
1210
|
+
"name": "range_min",
|
|
1211
|
+
"type": "u64"
|
|
1212
|
+
},
|
|
1213
|
+
{
|
|
1214
|
+
"name": "range_max",
|
|
1215
|
+
"type": "u64"
|
|
1119
1216
|
}
|
|
1120
1217
|
]
|
|
1121
1218
|
}
|
|
@@ -395,6 +395,133 @@ export type PoseidonsProtocol = {
|
|
|
395
395
|
name: 'user';
|
|
396
396
|
writable: true;
|
|
397
397
|
},
|
|
398
|
+
{
|
|
399
|
+
name: 'mint';
|
|
400
|
+
writable: true;
|
|
401
|
+
address: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
name: 'userAta';
|
|
405
|
+
writable: true;
|
|
406
|
+
pda: {
|
|
407
|
+
seeds: [
|
|
408
|
+
{
|
|
409
|
+
kind: 'account';
|
|
410
|
+
path: 'signer';
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
kind: 'account';
|
|
414
|
+
path: 'tokenProgram';
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
kind: 'account';
|
|
418
|
+
path: 'mint';
|
|
419
|
+
}
|
|
420
|
+
];
|
|
421
|
+
program: {
|
|
422
|
+
kind: 'const';
|
|
423
|
+
value: [
|
|
424
|
+
140,
|
|
425
|
+
151,
|
|
426
|
+
37,
|
|
427
|
+
143,
|
|
428
|
+
78,
|
|
429
|
+
36,
|
|
430
|
+
137,
|
|
431
|
+
241,
|
|
432
|
+
187,
|
|
433
|
+
61,
|
|
434
|
+
16,
|
|
435
|
+
41,
|
|
436
|
+
20,
|
|
437
|
+
142,
|
|
438
|
+
13,
|
|
439
|
+
131,
|
|
440
|
+
11,
|
|
441
|
+
90,
|
|
442
|
+
19,
|
|
443
|
+
153,
|
|
444
|
+
218,
|
|
445
|
+
255,
|
|
446
|
+
16,
|
|
447
|
+
132,
|
|
448
|
+
4,
|
|
449
|
+
142,
|
|
450
|
+
123,
|
|
451
|
+
216,
|
|
452
|
+
219,
|
|
453
|
+
233,
|
|
454
|
+
248,
|
|
455
|
+
89
|
|
456
|
+
];
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
},
|
|
460
|
+
{
|
|
461
|
+
name: 'squadsAta';
|
|
462
|
+
writable: true;
|
|
463
|
+
pda: {
|
|
464
|
+
seeds: [
|
|
465
|
+
{
|
|
466
|
+
kind: 'account';
|
|
467
|
+
path: 'squads';
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
kind: 'account';
|
|
471
|
+
path: 'tokenProgram';
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
kind: 'account';
|
|
475
|
+
path: 'mint';
|
|
476
|
+
}
|
|
477
|
+
];
|
|
478
|
+
program: {
|
|
479
|
+
kind: 'const';
|
|
480
|
+
value: [
|
|
481
|
+
140,
|
|
482
|
+
151,
|
|
483
|
+
37,
|
|
484
|
+
143,
|
|
485
|
+
78,
|
|
486
|
+
36,
|
|
487
|
+
137,
|
|
488
|
+
241,
|
|
489
|
+
187,
|
|
490
|
+
61,
|
|
491
|
+
16,
|
|
492
|
+
41,
|
|
493
|
+
20,
|
|
494
|
+
142,
|
|
495
|
+
13,
|
|
496
|
+
131,
|
|
497
|
+
11,
|
|
498
|
+
90,
|
|
499
|
+
19,
|
|
500
|
+
153,
|
|
501
|
+
218,
|
|
502
|
+
255,
|
|
503
|
+
16,
|
|
504
|
+
132,
|
|
505
|
+
4,
|
|
506
|
+
142,
|
|
507
|
+
123,
|
|
508
|
+
216,
|
|
509
|
+
219,
|
|
510
|
+
233,
|
|
511
|
+
248,
|
|
512
|
+
89
|
|
513
|
+
];
|
|
514
|
+
};
|
|
515
|
+
};
|
|
516
|
+
},
|
|
517
|
+
{
|
|
518
|
+
name: 'tokenProgram';
|
|
519
|
+
address: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
name: 'associatedTokenProgram';
|
|
523
|
+
address: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
|
|
524
|
+
},
|
|
398
525
|
{
|
|
399
526
|
name: 'systemProgram';
|
|
400
527
|
address: '11111111111111111111111111111111';
|
|
@@ -402,8 +529,12 @@ export type PoseidonsProtocol = {
|
|
|
402
529
|
];
|
|
403
530
|
args: [
|
|
404
531
|
{
|
|
405
|
-
name: '
|
|
406
|
-
type:
|
|
532
|
+
name: 'args';
|
|
533
|
+
type: {
|
|
534
|
+
defined: {
|
|
535
|
+
name: 'addSpinArgs';
|
|
536
|
+
};
|
|
537
|
+
};
|
|
407
538
|
}
|
|
408
539
|
];
|
|
409
540
|
},
|
|
@@ -1395,6 +1526,22 @@ export type PoseidonsProtocol = {
|
|
|
1395
1526
|
];
|
|
1396
1527
|
};
|
|
1397
1528
|
},
|
|
1529
|
+
{
|
|
1530
|
+
name: 'addSpinArgs';
|
|
1531
|
+
type: {
|
|
1532
|
+
kind: 'struct';
|
|
1533
|
+
fields: [
|
|
1534
|
+
{
|
|
1535
|
+
name: 'isUsdc';
|
|
1536
|
+
type: 'bool';
|
|
1537
|
+
},
|
|
1538
|
+
{
|
|
1539
|
+
name: 'spins';
|
|
1540
|
+
type: 'u64';
|
|
1541
|
+
}
|
|
1542
|
+
];
|
|
1543
|
+
};
|
|
1544
|
+
},
|
|
1398
1545
|
{
|
|
1399
1546
|
name: 'addWheelPrizeArgs';
|
|
1400
1547
|
type: {
|
|
@@ -1407,6 +1554,14 @@ export type PoseidonsProtocol = {
|
|
|
1407
1554
|
{
|
|
1408
1555
|
name: 'available';
|
|
1409
1556
|
type: 'u64';
|
|
1557
|
+
},
|
|
1558
|
+
{
|
|
1559
|
+
name: 'rangeMin';
|
|
1560
|
+
type: 'u64';
|
|
1561
|
+
},
|
|
1562
|
+
{
|
|
1563
|
+
name: 'rangeMax';
|
|
1564
|
+
type: 'u64';
|
|
1410
1565
|
}
|
|
1411
1566
|
];
|
|
1412
1567
|
};
|
package/dist/utils/constants.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CORE_POSEIDON_COLLECTION = exports.POSEIDON_VAULT_PROGRAM_ID = void 0;
|
|
3
|
+
exports.USDC_MINT = exports.CORE_POSEIDON_COLLECTION = exports.POSEIDON_VAULT_PROGRAM_ID = exports.SOL_MINT = void 0;
|
|
4
4
|
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
exports.SOL_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111112');
|
|
5
6
|
exports.POSEIDON_VAULT_PROGRAM_ID = new web3_js_1.PublicKey('E65MHJjJT2ihTLgMfHdWtsNYMA6iJsCjraf7UrxU2sDP');
|
|
6
7
|
exports.CORE_POSEIDON_COLLECTION = new web3_js_1.PublicKey('69CLccefLRmvDSAJP7Er632dvn878qkpdcnvq5ZUspSm');
|
|
8
|
+
exports.USDC_MINT = new web3_js_1.PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Connection } from '@solana/web3.js';
|
|
2
|
+
export declare function getTokenBalance(tokenAddress: string, wallet: string, connection: Connection): Promise<number>;
|
|
3
|
+
export declare function getBalanceSol(wallet: string, connection: Connection): Promise<number | undefined>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getBalanceSol = exports.getTokenBalance = void 0;
|
|
13
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
14
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
15
|
+
const formatBigNumber = (number, decimals = 6) => Number(number.toString()) / Math.pow(10, decimals);
|
|
16
|
+
function getTokenBalance(tokenAddress, wallet, connection) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
if (!wallet)
|
|
19
|
+
return 0;
|
|
20
|
+
try {
|
|
21
|
+
const token = yield (0, spl_token_1.getAssociatedTokenAddress)(new web3_js_1.PublicKey(tokenAddress), new web3_js_1.PublicKey(wallet));
|
|
22
|
+
const mint = yield (0, spl_token_1.getMint)(connection, new web3_js_1.PublicKey(tokenAddress));
|
|
23
|
+
const account = yield (0, spl_token_1.getAccount)(connection, token);
|
|
24
|
+
const formattedAmount = formatBigNumber(account.amount, mint.decimals);
|
|
25
|
+
return formattedAmount;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.getTokenBalance = getTokenBalance;
|
|
33
|
+
function getBalanceSol(wallet, connection) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
if (!wallet)
|
|
36
|
+
return;
|
|
37
|
+
try {
|
|
38
|
+
const balanceSol = yield connection.getBalance(new web3_js_1.PublicKey(wallet));
|
|
39
|
+
const amountInSol = balanceSol / web3_js_1.LAMPORTS_PER_SOL;
|
|
40
|
+
return amountInSol;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
/* empty */
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
exports.getBalanceSol = getBalanceSol;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, Connection, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
export declare const swap: ({ connection, wallet, inToken, outToken, amount, payer }: {
|
|
3
|
+
connection: Connection;
|
|
4
|
+
wallet: string;
|
|
5
|
+
inToken: string;
|
|
6
|
+
outToken: string;
|
|
7
|
+
amount: number;
|
|
8
|
+
payer: string;
|
|
9
|
+
}) => Promise<{
|
|
10
|
+
swapIxs: TransactionInstruction[];
|
|
11
|
+
closeAccountIxs: TransactionInstruction[];
|
|
12
|
+
addressLookupTableAccounts: AddressLookupTableAccount[];
|
|
13
|
+
setupInstructions: any[];
|
|
14
|
+
outAmount: any;
|
|
15
|
+
}>;
|
|
16
|
+
export declare const getPrice: (token: string) => Promise<any>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getPrice = exports.swap = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
19
|
+
const pda_1 = require("./pda");
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
|
+
const swap = ({ connection, wallet, inToken, outToken, amount, payer }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
try {
|
|
23
|
+
const token = TOKENS[inToken];
|
|
24
|
+
if (!token) {
|
|
25
|
+
throw new Error('Token not found');
|
|
26
|
+
}
|
|
27
|
+
const formattedAmountIn = Math.round(amount * Math.pow(10, token.decimals));
|
|
28
|
+
const quoteResponse = yield axios_1.default.get(`https://lite-api.jup.ag/swap/v1/quote?inputMint=${inToken}&outputMint=${outToken}&amount=${formattedAmountIn}&slippageBps=20&onlyDirectRoutes=true&platformFeeBps=60`);
|
|
29
|
+
const { data: quoteData } = quoteResponse;
|
|
30
|
+
const swapResponse = yield axios_1.default.post('https://lite-api.jup.ag/swap/v1/swap-instructions', {
|
|
31
|
+
quoteResponse: quoteData,
|
|
32
|
+
userPublicKey: wallet,
|
|
33
|
+
payer,
|
|
34
|
+
feeAccount: inToken === constants_1.USDC_MINT.toString() ? getFeeAccount() : undefined,
|
|
35
|
+
wrapAndUnwrapSol: false
|
|
36
|
+
});
|
|
37
|
+
const { setupInstructions, swapInstruction, addressLookupTableAddresses } = swapResponse.data;
|
|
38
|
+
const swapIxs = [
|
|
39
|
+
deserializeInstruction(swapInstruction)
|
|
40
|
+
];
|
|
41
|
+
const closeAccountIxs = [];
|
|
42
|
+
if (outToken === constants_1.SOL_MINT.toString()) {
|
|
43
|
+
const wsolATA = (0, pda_1.getTokenATA)(new web3_js_1.PublicKey(wallet), constants_1.SOL_MINT, spl_token_1.TOKEN_PROGRAM_ID);
|
|
44
|
+
closeAccountIxs.push((0, spl_token_1.createCloseAccountInstruction)(wsolATA, new web3_js_1.PublicKey(wallet), new web3_js_1.PublicKey(wallet), [], spl_token_1.TOKEN_PROGRAM_ID));
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
swapIxs,
|
|
48
|
+
closeAccountIxs,
|
|
49
|
+
addressLookupTableAccounts: yield getAddressLookupTableAccounts(connection, addressLookupTableAddresses),
|
|
50
|
+
setupInstructions: setupInstructions.length > 0
|
|
51
|
+
? [...setupInstructions.map(deserializeInstruction)]
|
|
52
|
+
: [],
|
|
53
|
+
outAmount: quoteData.otherAmountThreshold
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
exports.swap = swap;
|
|
61
|
+
const getPrice = (token) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
const response = yield axios_1.default.get(`https://lite-api.jup.ag/price/v3?ids=${token}`);
|
|
64
|
+
return (_b = (_a = response.data[token]) === null || _a === void 0 ? void 0 : _a.usdPrice) !== null && _b !== void 0 ? _b : 0;
|
|
65
|
+
});
|
|
66
|
+
exports.getPrice = getPrice;
|
|
67
|
+
const deserializeInstruction = (instruction) => {
|
|
68
|
+
return new web3_js_1.TransactionInstruction({
|
|
69
|
+
programId: new web3_js_1.PublicKey(instruction.programId),
|
|
70
|
+
keys: instruction.accounts.map((key) => ({
|
|
71
|
+
pubkey: new web3_js_1.PublicKey(key.pubkey),
|
|
72
|
+
isSigner: key.isSigner,
|
|
73
|
+
isWritable: key.isWritable
|
|
74
|
+
})),
|
|
75
|
+
data: Buffer.from(instruction.data, 'base64')
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
|
+
const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys.map((key) => new web3_js_1.PublicKey(key)));
|
|
80
|
+
return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => {
|
|
81
|
+
const addressLookupTableAddress = keys[index];
|
|
82
|
+
if (accountInfo) {
|
|
83
|
+
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
84
|
+
key: new web3_js_1.PublicKey(addressLookupTableAddress),
|
|
85
|
+
state: web3_js_1.AddressLookupTableAccount.deserialize(new Uint8Array(accountInfo.data))
|
|
86
|
+
});
|
|
87
|
+
acc.push(addressLookupTableAccount);
|
|
88
|
+
}
|
|
89
|
+
return acc;
|
|
90
|
+
}, new Array());
|
|
91
|
+
});
|
|
92
|
+
const TOKENS = {
|
|
93
|
+
So11111111111111111111111111111111111111112: {
|
|
94
|
+
mint: 'So11111111111111111111111111111111111111112',
|
|
95
|
+
decimals: 9
|
|
96
|
+
},
|
|
97
|
+
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
|
|
98
|
+
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
99
|
+
decimals: 6
|
|
100
|
+
},
|
|
101
|
+
t3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV: {
|
|
102
|
+
mint: 't3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV',
|
|
103
|
+
decimals: 6
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const getFeeAccount = () => {
|
|
107
|
+
return (0, pda_1.getTokenATA)(new web3_js_1.PublicKey('Hk1r2NUL4LbUhx1agg1w44tyZiNr72mbeLsg6suF5MA4'), constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID);
|
|
108
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@triadxyz/poseidons-protocol",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"bs58": "6.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
+
"@types/bn.js": "^5.1.0",
|
|
33
34
|
"@typescript-eslint/eslint-plugin": "6.7.3",
|
|
34
35
|
"@typescript-eslint/parser": "6.7.3",
|
|
35
|
-
"@types/bn.js": "^5.1.0",
|
|
36
36
|
"eslint": "8.50.0",
|
|
37
37
|
"eslint-config-prettier": "9.0.0",
|
|
38
38
|
"eslint-plugin-prettier": "5.0.0",
|