UNPKG

@parifi/synthetix-sdk-ts

Version:

A Typescript SDK for interactions with the Synthetix protocol

121 lines (119 loc) 4.69 kB
// src/utils/market.ts import { encodeAbiParameters, parseUnits } from "viem"; // src/constants/common.ts import { zeroAddress } from "viem"; var DISABLED_MARKETS = { 84532: [3, 6300], 8453: [6300] }; var CUSTOM_DECIMALS = { [421614 /* ARBITUM_SEPOLIA */]: { 2: 6, USDC: 6 }, [42161 /* ARBITRUM */]: { 2: 6, USDC: 6 }, [84532 /* BASE_SEPOLIA */]: { 1: 6, USDC: 6 }, [8453 /* BASE */]: { 1: 6, USDC: 6 } }; // src/utils/market.ts var Market = class { constructor(synthetixSdk) { this.disabledMarkets = []; this.sdk = synthetixSdk; this.marketsById = /* @__PURE__ */ new Map(); this.marketsByName = /* @__PURE__ */ new Map(); this.marketsBySymbol = /* @__PURE__ */ new Map(); if (synthetixSdk.rpcConfig.chainId in DISABLED_MARKETS) { this.disabledMarkets = DISABLED_MARKETS[synthetixSdk.rpcConfig.chainId]; } } /** * Look up the market_id and market_name for a market. If only one is provided, * the other is resolved. If both are provided, they are checked for consistency. * @param marketIdOrName Id or name of the market to resolve */ async resolveMarket(marketIdOrName) { if (!this.sdk.resolveMarketNames && typeof marketIdOrName === "number") { console.log("Resolve markets set to false. Returning market id without validating..."); return { resolvedMarketName: "Unresolved Market", resolvedMarketId: Number(marketIdOrName) }; } const market = await this.getMarket(marketIdOrName); if (!market?.marketName || market?.marketId === void 0) throw new Error(`Market not found for ${marketIdOrName}`); return { resolvedMarketName: market.marketName, resolvedMarketId: market.marketId }; } /** * Format the size of a synth for an order. This is used for synths whose base asset * does not use 18 decimals. For example, USDC uses 6 decimals, so we need to handle size * differently from other assets. * @param size The size as an ether value (e.g. 100). * @param marketId The id of the market. * @returns The formatted size in wei. (e.g. 100 = 100000000000000000000) */ async formatSize(size, marketId) { return parseUnits(size.toString(), CUSTOM_DECIMALS[this.sdk.rpcConfig.chainId][marketId] || 18); } async prepareOracleCallsWithPriceId(priceFeedIds) { if (!priceFeedIds.length) { return []; } const stalenessTolerance = 30n; const updateData = await this.sdk.pyth.getPriceFeedsUpdateData(priceFeedIds); const signedRequiredData = encodeAbiParameters( [ { type: "uint8", name: "updateType" }, { type: "uint64", name: "stalenessTolerance" }, { type: "bytes32[]", name: "priceIds" }, { type: "bytes[]", name: "updateData" } ], [1, stalenessTolerance, priceFeedIds, updateData] ); const pythWrapper = await this.sdk.contracts.getPythErc7412WrapperInstance(); const dataVerificationTx = this.sdk.utils.generateDataVerificationTx(pythWrapper.address, signedRequiredData); return [{ ...dataVerificationTx, value: 0n, requireSuccess: false }]; } /** * Prepare a call to the external node with oracle updates for the specified market names. * The result can be passed as the first argument to a multicall function to improve performance * of ERC-7412 calls. If no market names are provided, all markets are fetched. This is useful for * read functions since the user does not pay gas for those oracle calls, and reduces RPC calls and * runtime. * @param {number[]} marketIds An array of market ids to fetch prices for. If not provided, all markets are fetched * @returns {Promise<Call3Value[]>} objects representing the target contract, call data, value, requireSuccess flag and other necessary details for executing the function in the blockchain. */ async prepareOracleCall(marketIds = []) { let priceFeedIds = []; if (marketIds.length != 0) { const marketSymbols = []; marketIds.forEach((marketId) => { const marketSymbol = this.marketsById.get(marketId)?.symbol; if (!marketSymbol) return; marketSymbols.push(marketSymbol); }); marketSymbols.forEach((marketSymbol) => { const feedId = this.sdk.pyth.priceFeedIds.get(marketSymbol); if (!feedId) return; priceFeedIds.push(feedId); }); } else { priceFeedIds = Array.from(this.sdk.pyth.priceFeedIds.values()); } return this.prepareOracleCallsWithPriceId(priceFeedIds); } async getMarket(marketIdOrName) { throw new Error("Method not implemented. " + marketIdOrName); } }; export { Market }; //# sourceMappingURL=market.mjs.map