UNPKG

@parifi/sdk

Version:

Parifi SDK with common utility functions

130 lines (125 loc) 3.93 kB
// src/pyth/pyth.ts import axios from "axios"; // src/common/constants.ts import { Decimal } from "decimal.js"; var PRECISION_MULTIPLIER = new Decimal("10000"); var DEVIATION_PRECISION_MULTIPLIER = new Decimal(10).pow(12); var SECONDS_IN_A_YEAR = new Decimal(365 * 24 * 60 * 60); var MAX_FEE = new Decimal(1e7); var WAD = new Decimal(10).pow(18); var DECIMAL_10 = new Decimal(10); var DECIMAL_ZERO = new Decimal(0); var BIGINT_ZERO = BigInt(0); var ONE_GWEI = 1e9; var DEFAULT_GAS_PRICE = 2 * ONE_GWEI; // src/common/helpers.ts import { formatEther, parseEther } from "viem"; var getUniqueValuesFromArray = (originalArray) => { const uniqueArray = []; const seenValues = /* @__PURE__ */ new Set(); originalArray.forEach((item) => { if (!seenValues.has(item)) { uniqueArray.push(item); seenValues.add(item); } }); return uniqueArray; }; // src/pyth/pyth.ts import Decimal2 from "decimal.js"; // src/pyth/pythMapper.ts var mapPythPriceResponseToInterface = (response) => { return response.map((item) => ({ id: item.id || "", price: { price: item.price.price || "0", conf: item.price.conf || "0", expo: item.price.expo || 0, publish_time: item.price.publish_time || 0 }, ema_price: { price: item.ema_price.price || "0", conf: item.ema_price.conf || "0", expo: item.ema_price.expo || 0, publish_time: item.ema_price.publish_time || 0 } })); }; // src/pyth/pyth.ts var getPythClient = async (pythServiceEndpoint, pythServiceUsername, pythServicePassword, isStable = true) => { try { const config = { baseURL: pythServiceEndpoint || (isStable ? "https://hermes.pyth.network" : "https://hermes-beta.pyth.network"), auth: pythServiceUsername && pythServicePassword ? { username: pythServiceUsername, password: pythServicePassword } : void 0 }; return axios.create(config); } catch (error) { console.log("Error when creating Pyth instance:", error); throw error; } }; var getVaaPriceUpdateData = async (priceIds, pythClient) => { const uniquePriceIds = getUniqueValuesFromArray(priceIds); let priceUpdateData = []; if (pythClient) { try { const response = await pythClient.get("/api/latest_vaas", { params: { ids: uniquePriceIds } }); priceUpdateData = response.data; } catch (error) { console.log("Error fetching data from Pyth", error); throw error; } } return priceUpdateData.map((vaa) => "0x" + Buffer.from(vaa, "base64").toString("hex")); }; var formatPythPrice = (pythPrice, pythExponent) => { const adjustedFactor = new Decimal2(10).pow(pythExponent); return new Decimal2(pythPrice).mul(adjustedFactor); }; var getLatestFormattedPrice = async (priceIds, pythClient) => { let formattedPrices = []; const priceData = await getLatestPricesFromPyth(priceIds, pythClient); priceData.map((pythPrice) => { const formattedPrice = formatPythPrice(Number(pythPrice.price.price), pythPrice.price.expo); formattedPrices.push({ priceId: `0x${pythPrice.id}`, formattedPrice }); }); return formattedPrices; }; var getLatestPricesFromPyth = async (priceIds, pythClient) => { const uniquePriceIds = getUniqueValuesFromArray(priceIds); const pythPriceResponses = []; if (pythClient) { try { const response = await pythClient.get("/api/latest_price_feeds", { params: { ids: uniquePriceIds, verbose: false, binary: false } }); return mapPythPriceResponseToInterface(response.data); } catch (error) { console.log("Error fetching latest prices from Pyth", error); throw error; } } return pythPriceResponses; }; export { formatPythPrice, getLatestFormattedPrice, getLatestPricesFromPyth, getPythClient, getVaaPriceUpdateData }; //# sourceMappingURL=pyth.mjs.map