UNPKG

@marinade.finance/kamino-sdk

Version:
135 lines 6.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JupService = void 0; const web3_js_1 = require("@solana/web3.js"); const axios_1 = __importDefault(require("axios")); const api_1 = require("@jup-ag/api"); class JupService { _connection; _cluster; constructor(connection, cluster) { this._connection = connection; this._cluster = cluster; } static getSwapTransactions = async (route, walletPublicKey, wrapUnwrapSOL = true, asLegacyTransaction) => { const res = await axios_1.default.post('https://quote-api.jup.ag/v4/swap', { // route from /quote api route, // user public key to be used for the swap userPublicKey: walletPublicKey.toString(), // auto wrap and unwrap SOL. default is true wrapUnwrapSOL, asLegacyTransaction, }); return res.data; }; // the amounts has to be in lamports static getBestRoute = async (amount, inputMint, outputMint, slippageBps, mode = 'ExactIn', asLegacyTransaction) => { const params = { inputMint: inputMint.toString(), outputMint: outputMint.toString(), amount: amount.ceil().toString(), slippageBps, onlyDirectRoutes: false, asLegacyTransaction, mode, }; const res = await axios_1.default.get('https://quote-api.jup.ag/v4/quote', { params }); return res.data.data[0]; }; // the amounts has to be in lamports static getAllRoutesV4 = async (amount, inputMint, outputMint, slippageBps, mode = 'ExactIn', asLegacyTransaction) => { const params = { inputMint: inputMint.toString(), outputMint: outputMint.toString(), amount: amount.ceil().toString(), slippageBps, onlyDirectRoutes: false, asLegacyTransaction, mode, }; const res = await axios_1.default.get('https://quote-api.jup.ag/v4/quote', { params }); return res.data.data; }; // the amounts has to be in lamports static getBestRouteV6 = async (userPublicKey, amount, inputMint, outputMint, slippageBps, asLegacyTransaction, maxAccounts) => { try { const jupiterQuoteApi = (0, api_1.createJupiterApiClient)(); // config is optional // quote-api.jup.ag/v6/quote?inputMint=7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj&outputMint=mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So&amount=71101983&slippageBps=10&onlyDirectRoutes=false&asLegacyTransaction=false&maxAccounts=33 const params = { inputMint: inputMint.toString(), outputMint: outputMint.toString(), amount: amount.floor().toNumber(), slippageBps, onlyDirectRoutes: false, asLegacyTransaction, maxAccounts, }; console.log('getBestRouteV6 params', JSON.stringify(params)); const res = await axios_1.default.get('https://quote-api.jup.ag/v6/quote', { params }); const transaction = await jupiterQuoteApi.swapPost({ swapRequest: { quoteResponse: res.data, userPublicKey: userPublicKey.toString(), wrapAndUnwrapSol: false, }, }); return transaction; } catch (error) { console.log('getBestRouteV6 error', error); throw error; } }; async getPrice(inputMint, outputMint) { const params = { ids: inputMint.toString(), vsToken: outputMint.toString(), vsAmount: 1, }; // BONK token if (outputMint.toString() === 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263') { params.vsAmount = 100; } const res = await axios_1.default.get('https://quote-api.jup.ag/v4/price', { params }); return res.data.data[inputMint.toString()].price; } static buildTransactionsFromSerialized = (serializedTransactions) => { return serializedTransactions.filter(Boolean).map((tx) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return web3_js_1.Transaction.from(Buffer.from(tx, 'base64')); }); }; static deserealizeVersionedTransactions = async (connection, serializedTransactions) => { const filtered = serializedTransactions.filter(Boolean); const result = []; let lookupTablesAddresses = []; for (let i = 0; i < filtered.length; i++) { const tx = filtered[i]; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion // safe to use as it is filtered above at 120 line const buffer = Buffer.from(tx, 'base64'); const versionedTx = web3_js_1.VersionedTransaction.deserialize(buffer); const { addressTableLookups } = versionedTx.message; lookupTablesAddresses = [...lookupTablesAddresses, ...addressTableLookups.map((item) => item.accountKey)]; const lookupTableAccountsRequests = addressTableLookups.map((item) => { return JupService.getLookupTableAccount(connection, item.accountKey); }); const lookupTableAccounts = await Promise.all(lookupTableAccountsRequests); const decompiledMessage = web3_js_1.TransactionMessage.decompile(versionedTx.message, { // @ts-ignore addressLookupTableAccounts: lookupTableAccounts, }); result.push(decompiledMessage); } return { txMessage: result, lookupTablesAddresses }; }; static getLookupTableAccount = async (connection, address) => { return connection.getAddressLookupTable(new web3_js_1.PublicKey(address)).then((res) => res.value); }; } exports.JupService = JupService; //# sourceMappingURL=JupService.js.map