UNPKG

@bluefin-exchange/bluefin7k-aggregator-sdk

Version:
75 lines (74 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_SOURCES = void 0; exports.getQuote = getQuote; const utils_1 = require("@mysten/sui/utils"); const fetchClient_1 = require("../../config/fetchClient"); const apiEndpoints_1 = require("../../constants/apiEndpoints"); const aggregator_1 = require("../../types/aggregator"); exports.DEFAULT_SOURCES = [ "suiswap", "turbos", "cetus", "bluemove", "kriya", "kriya_v3", "aftermath", "deepbook_v3", "flowx", "flowx_v3", "bluefin", "springsui", "obric", "stsui", "steamm", "steamm_oracle_quoter", "magma", "haedal_pmm", "momentum", ]; async function getQuote({ tokenIn, tokenOut, amountIn, sources = exports.DEFAULT_SOURCES, commissionBps, targetPools, excludedPools, taker, }) { const params = new URLSearchParams({ amount: amountIn, from: (0, utils_1.normalizeStructTag)(tokenIn), to: (0, utils_1.normalizeStructTag)(tokenOut), sources: sources.join(","), }); if (targetPools?.length) { params.append("target_pools", targetPools.map((v) => (0, utils_1.normalizeSuiObjectId)(v)).join(",")); } if (excludedPools?.length) { params.append("excluded_pools", excludedPools.map((v) => (0, utils_1.normalizeSuiObjectId)(v)).join(",")); } if (taker) { params.append("taker", taker); } const response = await (0, fetchClient_1.fetchClient)(`${apiEndpoints_1.API_ENDPOINTS.MAIN}/quote?${params}`); if (!response.ok) { let responseText = "Failed to fetch aggregator quote"; try { responseText = await response.text(); } catch { responseText = "Failed to fetch aggregator quote"; } throw new Error(responseText); } const quoteResponse = (await response.json()); computeReturnAmountAfterCommission(quoteResponse, commissionBps); return quoteResponse; } const computeReturnAmountAfterCommission = (quoteResponse, commissionBps) => { const _commissionBps = (0, aggregator_1.isBluefinXRouting)(quoteResponse) ? 0 : commissionBps; if (quoteResponse.returnAmount && +quoteResponse.returnAmount > 0) { quoteResponse.returnAmountAfterCommissionWithDecimal = ((BigInt(quoteResponse.returnAmountWithDecimal || 0) * BigInt(10000 - (_commissionBps ?? 0))) / BigInt(10000)).toString(10); const exp = Math.round(+quoteResponse.returnAmountWithDecimal / +quoteResponse.returnAmount); quoteResponse.returnAmountAfterCommission = (+quoteResponse.returnAmountAfterCommissionWithDecimal / exp).toString(10); } else { quoteResponse.returnAmountAfterCommission = ""; quoteResponse.returnAmountAfterCommissionWithDecimal = ""; } };