@prism-hq/prism-ag
Version:
Prism Aggregator
50 lines (49 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.whirlPoolsRoute = void 0;
const sdk_1 = require("@orca-so/sdk");
const whirlpools_prism_sdk_1 = require("whirlpools-prism-sdk");
const spl_token_1 = require("@solana/spl-token");
function whirlPoolsRoute(fromCoin, toCoin, fromCoinAmount, option, liquidityData, settings) {
let whirlpoolData = liquidityData.whirlpoolsData[option.poolAddress.toBase58()];
let whirlpool = whirlpoolData.whirlpool;
let aToB = fromCoin.mintAddress === whirlpool.tokenMintA.toBase58();
let tickArrays = [];
if (aToB)
tickArrays = whirlpoolData.tickArraysAtoB;
else
tickArrays = whirlpoolData.tickArraysBtoA;
let swapQuoteParams = {
whirlpoolData: whirlpool,
tokenAmount: new spl_token_1.u64(fromCoinAmount * 10 ** fromCoin.decimals),
otherAmountThreshold: whirlpools_prism_sdk_1.SwapUtils.getDefaultOtherAmountThreshold(true),
sqrtPriceLimit: whirlpools_prism_sdk_1.SwapUtils.getDefaultSqrtPriceLimit(aToB),
aToB: aToB,
amountSpecifiedIsInput: true,
tickArrays: tickArrays,
};
let swapQuote = (0, whirlpools_prism_sdk_1.swapQuoteWithParams)(swapQuoteParams, new sdk_1.Percentage(new spl_token_1.u64(1), new spl_token_1.u64(1)));
let received = +(swapQuote.estimatedAmountOut.toNumber() / 10 ** toCoin.decimals).toFixed(toCoin.decimals);
let priceImpact = 0;
let totalFees = (settings.owner.fee + settings.host.fee) / 100;
let fees = {};
fees[toCoin.symbol] = received * totalFees / 100;
let amountWithFees = received * (1 - totalFees / 100);
return {
from: fromCoin.symbol,
amountIn: fromCoinAmount,
to: toCoin.symbol,
amountOut: received,
amountWithFees: amountWithFees,
minimumReceived: amountWithFees * (1 - settings.slippage / 100),
provider: "Whirlpools",
fees: fees,
priceImpact: priceImpact,
routeData: {
whirlPool: Object.assign(Object.assign({}, whirlpoolData), { ticks: tickArrays, otherAmountThreshold: whirlpools_prism_sdk_1.SwapUtils.getDefaultOtherAmountThreshold(true), sqrtPriceLimit: whirlpools_prism_sdk_1.SwapUtils.getDefaultSqrtPriceLimit(aToB), aToB: aToB, amountSpecifiedIsInput: true }),
fromCoin: fromCoin,
toCoin: toCoin,
}
};
}
exports.whirlPoolsRoute = whirlPoolsRoute;