UNPKG

@triadxyz/triad-protocol

Version:

<div align="center"> <h1>Triad Protocol</h1> </div>

93 lines (92 loc) 4.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPrice = exports.swap = void 0; const axios_1 = __importDefault(require("axios")); const web3_js_1 = require("@solana/web3.js"); const spl_token_1 = require("@solana/spl-token"); const pda_1 = require("./pda"); const constants_1 = require("./constants"); const swap = ({ connection, wallet, inToken, outToken, amount }) => __awaiter(void 0, void 0, void 0, function* () { const token = TOKENS[inToken]; if (!token) { throw new Error('Token not found'); } const formattedAmountIn = amount * Math.pow(10, token.decimals); 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`); const { data: quoteData } = quoteResponse; const swapResponse = yield axios_1.default.post('https://lite-api.jup.ag/swap/v1/swap-instructions', { quoteResponse: quoteData, userPublicKey: wallet, feeAccount: inToken === constants_1.TRD_MINT.toString() ? getFeeAccount() : undefined }); const { setupInstructions, swapInstruction, addressLookupTableAddresses } = swapResponse.data; return { swapIxs: [deserializeInstruction(swapInstruction)], addressLookupTableAccounts: yield getAddressLookupTableAccounts(connection, addressLookupTableAddresses), setupInstructions: setupInstructions.length > 0 ? [deserializeInstruction(setupInstructions)] : [], outAmount: quoteData.otherAmountThreshold }; }); exports.swap = swap; const getPrice = (token) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; const response = yield axios_1.default.get(`https://lite-api.jup.ag/price/v3?ids=${token}`); return (_b = (_a = response.data[token]) === null || _a === void 0 ? void 0 : _a.usdPrice) !== null && _b !== void 0 ? _b : 0; }); exports.getPrice = getPrice; const deserializeInstruction = (instruction) => { return new web3_js_1.TransactionInstruction({ programId: new web3_js_1.PublicKey(instruction.programId), keys: instruction.accounts.map((key) => ({ pubkey: new web3_js_1.PublicKey(key.pubkey), isSigner: key.isSigner, isWritable: key.isWritable })), data: Buffer.from(instruction.data, 'base64') }); }; const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, void 0, void 0, function* () { const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys.map((key) => new web3_js_1.PublicKey(key))); return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => { const addressLookupTableAddress = keys[index]; if (accountInfo) { const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({ key: new web3_js_1.PublicKey(addressLookupTableAddress), state: web3_js_1.AddressLookupTableAccount.deserialize(accountInfo.data) }); acc.push(addressLookupTableAccount); } return acc; }, new Array()); }); const TOKENS = { So11111111111111111111111111111111111111112: { mint: 'So11111111111111111111111111111111111111112', decimals: 9 }, EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: { mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', decimals: 6 }, t3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV: { mint: 't3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV', decimals: 6 } }; const getFeeAccount = () => { return (0, pda_1.getTokenATA)(new web3_js_1.PublicKey('Hk1r2NUL4LbUhx1agg1w44tyZiNr72mbeLsg6suF5MA4'), constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID); };