@triadxyz/poseidons-protocol 0.4.6 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/poseidons-protocol",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -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;
@@ -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>;
@@ -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
- };