UNPKG

@frakt-protocol/frakt-sdk

Version:

Frakt SDK for interacting with frakt.xyz protocols

107 lines (106 loc) 6.13 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.deriveMetadataPubkeyFromMint = exports.returnAnchorMultiRewardStaking = exports.getNftCreators = exports.shortenAddress = exports.getTokenAccountBalance = exports.getTokenAccount = exports.parseTokenAccount = exports.decodeSplTokenAccountData = exports.getOutputAmount = exports.getInputAmount = void 0; const anchor_1 = require("@project-serum/anchor"); const raydium_sdk_1 = require("@raydium-io/raydium-sdk"); const common_1 = require("../common"); const multi_reward_staking_json_1 = __importDefault(require("./idl/multi_reward_staking.json")); const getInputAmount = ({ poolKeys, poolInfo, receiveToken, receiveAmount, payToken, slippage = new raydium_sdk_1.Percent(1, 100), }) => { try { const amountOut = new raydium_sdk_1.TokenAmount(new raydium_sdk_1.Token(receiveToken.address, receiveToken.decimals, receiveToken.symbol, receiveToken.name), receiveAmount, false); const { amountIn, maxAmountIn, priceImpact } = raydium_sdk_1.Liquidity.computeAmountIn({ poolKeys, poolInfo, amountOut, currencyIn: payToken, slippage, }); return { amountIn: amountIn.toSignificant(), maxAmountIn: maxAmountIn.toSignificant(), priceImpact: priceImpact.toSignificant(), }; } catch (err) { console.error(err); } return { amountIn: '', maxAmountIn: '', priceImpact: '', }; }; exports.getInputAmount = getInputAmount; const getOutputAmount = ({ poolKeys, poolInfo, payToken, payAmount, receiveToken, slippage = new raydium_sdk_1.Percent(1, 100), }) => { try { const amountIn = new raydium_sdk_1.TokenAmount(new raydium_sdk_1.Token(payToken.address, payToken.decimals, payToken.symbol, payToken.name), payAmount, false); const { amountOut, minAmountOut, priceImpact } = raydium_sdk_1.Liquidity.computeAmountOut({ poolKeys, poolInfo, amountIn, currencyOut: receiveToken, slippage, }); return { amountOut: amountOut.toSignificant(), minAmountOut: minAmountOut.toSignificant(), priceImpact: priceImpact.toSignificant(), }; } catch (err) { console.error(err); } return { amountOut: '', minAmountOut: '', priceImpact: '', }; }; exports.getOutputAmount = getOutputAmount; const decodeSplTokenAccountData = (tokenAccountDataEncoded) => raydium_sdk_1.SPL_ACCOUNT_LAYOUT.decode(tokenAccountDataEncoded); exports.decodeSplTokenAccountData = decodeSplTokenAccountData; const parseTokenAccount = ({ tokenAccountPubkey, tokenAccountEncoded }) => tokenAccountEncoded ? { pubkey: tokenAccountPubkey, accountInfo: (0, exports.decodeSplTokenAccountData)(tokenAccountEncoded.data), } : null; exports.parseTokenAccount = parseTokenAccount; const getTokenAccount = ({ tokenMint, owner, connection, }) => __awaiter(void 0, void 0, void 0, function* () { const tokenAccountPubkey = yield raydium_sdk_1.Spl.getAssociatedTokenAccount({ mint: tokenMint, owner, }); const tokenAccountEncoded = yield connection.getAccountInfo(tokenAccountPubkey); return (0, exports.parseTokenAccount)({ tokenAccountPubkey, tokenAccountEncoded }); }); exports.getTokenAccount = getTokenAccount; const getTokenAccountBalance = (lpTokenAccountInfo, lpDecimals) => { var _a; return ((_a = lpTokenAccountInfo === null || lpTokenAccountInfo === void 0 ? void 0 : lpTokenAccountInfo.accountInfo) === null || _a === void 0 ? void 0 : _a.amount.toNumber()) / Math.pow(10, lpDecimals) || 0; }; exports.getTokenAccountBalance = getTokenAccountBalance; const shortenAddress = (address, chars = 4) => `${address.slice(0, chars)}...${address.slice(-chars)}`; exports.shortenAddress = shortenAddress; const getNftCreators = (nft) => { var _a, _b, _c, _d; return ((_d = (_c = (_b = (_a = nft === null || nft === void 0 ? void 0 : nft.metadata) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.creators) === null || _c === void 0 ? void 0 : _c.filter(({ verified }) => verified)) === null || _d === void 0 ? void 0 : _d.map(({ address }) => address)) || []; }; exports.getNftCreators = getNftCreators; const returnAnchorMultiRewardStaking = (programId, connection) => __awaiter(void 0, void 0, void 0, function* () { return new anchor_1.Program(multi_reward_staking_json_1.default, programId, new anchor_1.AnchorProvider(connection, (0, common_1.createFakeWallet)(), anchor_1.AnchorProvider.defaultOptions())); }); exports.returnAnchorMultiRewardStaking = returnAnchorMultiRewardStaking; const deriveMetadataPubkeyFromMint = (nftMint) => __awaiter(void 0, void 0, void 0, function* () { let metadata_program = new anchor_1.web3.PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'); const encoder = new TextEncoder(); const [metadataPubkey] = yield anchor_1.web3.PublicKey.findProgramAddress([encoder.encode('metadata'), metadata_program.toBuffer(), nftMint.toBuffer()], metadata_program); return metadataPubkey; }); exports.deriveMetadataPubkeyFromMint = deriveMetadataPubkeyFromMint;