@triadxyz/poseidons-protocol 0.4.5 → 0.4.7
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 +5 -35
- package/dist/types/idl_poseidons_protocol.json +91 -2
- package/dist/types/poseidons_protocol.d.ts +149 -2
- package/package.json +1 -1
- package/dist/utils/getTokenBalance.d.ts +0 -3
- package/dist/utils/getTokenBalance.js +0 -47
- package/dist/utils/swap.d.ts +0 -16
- package/dist/utils/swap.js +0 -108
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
|
@@ -17,9 +17,6 @@ const chest_1 = __importDefault(require("./chest"));
|
|
|
17
17
|
const wheel_1 = __importDefault(require("./wheel"));
|
|
18
18
|
const sendVersionedTransaction_1 = __importDefault(require("../utils/sendVersionedTransaction"));
|
|
19
19
|
const pda_1 = require("../utils/pda");
|
|
20
|
-
const getTokenBalance_1 = require("../utils/getTokenBalance");
|
|
21
|
-
const constants_1 = require("../utils/constants");
|
|
22
|
-
const swap_1 = require("../utils/swap");
|
|
23
20
|
class Game {
|
|
24
21
|
constructor(program, rpcOptions) {
|
|
25
22
|
this.program = program;
|
|
@@ -33,42 +30,12 @@ class Game {
|
|
|
33
30
|
* @param spins - The number of spins to add
|
|
34
31
|
* @returns The transaction signature or versioned transaction
|
|
35
32
|
*/
|
|
36
|
-
addSpin({ users, refer }) {
|
|
33
|
+
addSpin({ users, refer, isUsdc }) {
|
|
37
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
35
|
const ixs = [];
|
|
39
36
|
const addressLookupTableAccounts = [];
|
|
40
37
|
for (const user of users) {
|
|
41
38
|
const userPDA = (0, pda_1.getUserPDA)(user.user);
|
|
42
|
-
const userSolBalance = yield (0, getTokenBalance_1.getBalanceSol)(user.user.toBase58(), this.program.provider.connection);
|
|
43
|
-
const totalCost = user.spins * 0.0015;
|
|
44
|
-
const feeBuffer = Math.max(totalCost * 0.1, 0.0001);
|
|
45
|
-
const totalCostWithFees = totalCost + feeBuffer;
|
|
46
|
-
if (userSolBalance !== undefined && userSolBalance < totalCostWithFees) {
|
|
47
|
-
const solNeeded = totalCostWithFees - userSolBalance;
|
|
48
|
-
const solPrice = yield (0, swap_1.getPrice)(constants_1.SOL_MINT.toString());
|
|
49
|
-
const usdcNeeded = solNeeded * solPrice * 1.05;
|
|
50
|
-
const userUsdcBalance = yield (0, getTokenBalance_1.getTokenBalance)(constants_1.USDC_MINT.toString(), user.user.toBase58(), this.program.provider.connection);
|
|
51
|
-
if (userUsdcBalance < usdcNeeded) {
|
|
52
|
-
throw new Error(`The user does not have enough USDC. Necessary: ${usdcNeeded.toFixed(2)} USDC, Available: ${userUsdcBalance.toFixed(2)} USDC`);
|
|
53
|
-
}
|
|
54
|
-
const { swapIxs, closeAccountIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
|
|
55
|
-
connection: this.program.provider.connection,
|
|
56
|
-
wallet: user.user.toBase58(),
|
|
57
|
-
inToken: constants_1.USDC_MINT.toString(),
|
|
58
|
-
outToken: constants_1.SOL_MINT.toString(),
|
|
59
|
-
amount: usdcNeeded,
|
|
60
|
-
payer: this.rpcOptions.payer.toBase58()
|
|
61
|
-
});
|
|
62
|
-
if (swapIxs.length === 0) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
ixs.push(...setupInstructions);
|
|
66
|
-
ixs.push(...swapIxs);
|
|
67
|
-
if (closeAccountIxs && closeAccountIxs.length > 0) {
|
|
68
|
-
ixs.push(...closeAccountIxs);
|
|
69
|
-
}
|
|
70
|
-
addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
|
|
71
|
-
}
|
|
72
39
|
try {
|
|
73
40
|
yield this.program.account.user.fetch(userPDA, this.rpcOptions.commitment);
|
|
74
41
|
}
|
|
@@ -82,7 +49,10 @@ class Game {
|
|
|
82
49
|
.instruction());
|
|
83
50
|
}
|
|
84
51
|
ixs.push(yield this.program.methods
|
|
85
|
-
.addSpin(
|
|
52
|
+
.addSpin({
|
|
53
|
+
isUsdc,
|
|
54
|
+
spins: new anchor_1.BN(user.spins)
|
|
55
|
+
})
|
|
86
56
|
.accounts({
|
|
87
57
|
signer: this.program.provider.publicKey,
|
|
88
58
|
user: userPDA
|
|
@@ -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": {
|
|
@@ -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: {
|
package/package.json
CHANGED
|
@@ -1,3 +0,0 @@
|
|
|
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>;
|
|
@@ -1,47 +0,0 @@
|
|
|
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;
|
package/dist/utils/swap.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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>;
|
package/dist/utils/swap.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
};
|