UNPKG

@bluefin-exchange/bluefin7k-aggregator-sdk

Version:
71 lines (70 loc) 2.62 kB
import { normalizeStructTag, normalizeSuiObjectId } from "@mysten/sui/utils"; import { fetchClient } from "../../config/fetchClient"; import { API_ENDPOINTS } from "../../constants/apiEndpoints"; import { isBluefinXRouting, } from "../../types/aggregator"; export const 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", ]; export async function getQuote({ tokenIn, tokenOut, amountIn, sources = DEFAULT_SOURCES, commissionBps, targetPools, excludedPools, taker, }) { const params = new URLSearchParams({ amount: amountIn, from: normalizeStructTag(tokenIn), to: normalizeStructTag(tokenOut), sources: sources.join(","), }); if (targetPools?.length) { params.append("target_pools", targetPools.map((v) => normalizeSuiObjectId(v)).join(",")); } if (excludedPools?.length) { params.append("excluded_pools", excludedPools.map((v) => normalizeSuiObjectId(v)).join(",")); } if (taker) { params.append("taker", taker); } const response = await fetchClient(`${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 = isBluefinXRouting(quoteResponse) ? 0 : commissionBps; if (quoteResponse.returnAmount && +quoteResponse.returnAmount > 0) { quoteResponse.returnAmountAfterCommissionWithDecimal = ((BigInt(quoteResponse.returnAmountWithDecimal || 0) * BigInt(10_000 - (_commissionBps ?? 0))) / BigInt(10_000)).toString(10); const exp = Math.round(+quoteResponse.returnAmountWithDecimal / +quoteResponse.returnAmount); quoteResponse.returnAmountAfterCommission = (+quoteResponse.returnAmountAfterCommissionWithDecimal / exp).toString(10); } else { quoteResponse.returnAmountAfterCommission = ""; quoteResponse.returnAmountAfterCommissionWithDecimal = ""; } };