UNPKG

@renec-foundation/redex-sdk

Version:

Typescript SDK to interact with Orca's Whirlpool program.

96 lines (95 loc) 4.21 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.getTokenVaultAccountInfos = exports.getRewardInfos = exports.getNativeMintInfo = exports.getTokenMintInfos = void 0; const bn_js_1 = __importDefault(require("bn.js")); const spl_token_1 = require("@solana/spl-token"); const __1 = require(".."); function getTokenMintInfos(fetcher, data, refresh) { return __awaiter(this, void 0, void 0, function* () { const mintA = data.tokenMintA; const infoA = mintA.equals(spl_token_1.NATIVE_MINT) ? yield getNativeMintInfo() : yield fetcher.getMintInfo(mintA, refresh); if (!infoA) { throw new Error(`Unable to fetch MintInfo for mint - ${mintA}`); } const mintB = data.tokenMintB; const infoB = mintB.equals(spl_token_1.NATIVE_MINT) ? yield getNativeMintInfo() : yield fetcher.getMintInfo(mintB, refresh); if (!infoB) { throw new Error(`Unable to fetch MintInfo for mint - ${mintB}`); } return [ Object.assign({ mint: mintA }, infoA), Object.assign({ mint: mintB }, infoB), ]; }); } exports.getTokenMintInfos = getTokenMintInfos; function getNativeMintInfo() { return __awaiter(this, void 0, void 0, function* () { const nativeMint = { mintAuthority: null, supply: new bn_js_1.default(0), decimals: 9, isInitialized: true, freezeAuthority: null, mint: spl_token_1.NATIVE_MINT, }; return nativeMint; }); } exports.getNativeMintInfo = getNativeMintInfo; function getRewardInfos(fetcher, data, refresh) { return __awaiter(this, void 0, void 0, function* () { const rewardInfos = []; for (const rewardInfo of data.rewardInfos) { rewardInfos.push(yield getRewardInfo(fetcher, rewardInfo, refresh)); } return rewardInfos; }); } exports.getRewardInfos = getRewardInfos; function getRewardInfo(fetcher, data, refresh) { return __awaiter(this, void 0, void 0, function* () { const rewardInfo = Object.assign(Object.assign({}, data), { initialized: false, vaultAmount: new bn_js_1.default(0) }); if (__1.PoolUtil.isRewardInitialized(data)) { const vaultInfo = yield fetcher.getTokenInfo(data.vault, refresh); if (!vaultInfo) { throw new Error(`Unable to fetch TokenAccountInfo for vault - ${data.vault}`); } rewardInfo.initialized = true; rewardInfo.vaultAmount = vaultInfo.amount; } return rewardInfo; }); } function getTokenVaultAccountInfos(fetcher, data, refresh) { return __awaiter(this, void 0, void 0, function* () { const vaultA = data.tokenVaultA; const vaultInfoA = yield fetcher.getTokenInfo(vaultA, refresh); if (!vaultInfoA) { throw new Error(`Unable to fetch TokenAccountInfo for vault - ${vaultA}`); } const vaultB = data.tokenVaultB; const vaultInfoB = yield fetcher.getTokenInfo(vaultB, refresh); if (!vaultInfoB) { throw new Error(`Unable to fetch TokenAccountInfo for vault - ${vaultB}`); } return [vaultInfoA, vaultInfoB]; }); } exports.getTokenVaultAccountInfos = getTokenVaultAccountInfos;