@parifi/sdk
Version:
Parifi SDK with common utility functions
191 lines (183 loc) • 6.41 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/pyth/index.ts
var pyth_exports = {};
__export(pyth_exports, {
Pyth: () => Pyth
});
module.exports = __toCommonJS(pyth_exports);
// src/pyth/pyth.ts
var import_axios = __toESM(require("axios"));
// src/common/constants.ts
var import_decimal = require("decimal.js");
var PRECISION_MULTIPLIER = new import_decimal.Decimal("10000");
var DEVIATION_PRECISION_MULTIPLIER = new import_decimal.Decimal(10).pow(12);
var SECONDS_IN_A_YEAR = new import_decimal.Decimal(365 * 24 * 60 * 60);
var MAX_FEE = new import_decimal.Decimal(1e7);
var WAD = new import_decimal.Decimal(10).pow(18);
var DECIMAL_10 = new import_decimal.Decimal(10);
var DECIMAL_ZERO = new import_decimal.Decimal(0);
var BIGINT_ZERO = BigInt(0);
var ONE_GWEI = 1e9;
var DEFAULT_GAS_PRICE = 2 * ONE_GWEI;
// src/common/helpers.ts
var import_viem = require("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
var import_decimal2 = __toESM(require("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 import_axios.default.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 import_decimal2.default(10).pow(pythExponent);
return new import_decimal2.default(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;
};
// src/pyth/index.ts
var Pyth = class {
constructor(pythConfig) {
this.pythConfig = pythConfig;
this.pythClient = {};
}
async initPyth() {
this.pythClient = await getPythClient(
this.pythConfig.pythEndpoint,
this.pythConfig.username,
this.pythConfig.password,
this.pythConfig.isStable
);
}
async getVaaPriceUpdateData(priceIds) {
return await getVaaPriceUpdateData(priceIds, this.pythClient);
}
formatPythPrice(pythPrice, pythExponent) {
return formatPythPrice(pythPrice, pythExponent);
}
async getLatestPricesFromPyth(priceIds) {
return await getLatestPricesFromPyth(priceIds, this.pythClient);
}
async getLatestFormattedPrice(priceIds) {
return await getLatestFormattedPrice(priceIds, this.pythClient);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Pyth
});
//# sourceMappingURL=index.js.map