UNPKG

@parifi/synthetix-sdk-ts

Version:

A Typescript SDK for interactions with the Synthetix protocol

317 lines (313 loc) 14.7 kB
// src/pyth/index.ts import axios from "axios"; // src/constants/protocol.ts var PUBLIC_PYTH_ENDPOINT = "https://hermes.pyth.network"; var DEFAULT_PYTH_TIMEOUT = 15e3; // src/constants/common.ts import { zeroAddress } from "viem"; 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/constants/pythPriceIds.ts var SYMBOL_TO_PYTH_FEED = /* @__PURE__ */ new Map([ // Markets ["BTC", "0xc9d8b075a5c69303365ae23633d4e085199bf5c520a3b90fed1322a0342ffc33"], ["SOL", "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"], ["WIF", "0x4ca4beeca86f0d164160323817a4e42b10010a724c2217c6ee41b54cd4cc61fc"], ["ETH", "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"], ["AAVE", "0x2b9ab1e972a281585084148ba1389800799bd4be63b957507db1349314e47445"], ["ADA", "0x2a01deaec9e51a579277b34b122399984d0bbf57e2458a7e42fecd2829867a0d"], ["ARB", "0x3fa4252848f9f0a1480be62745a4629d9eb1322aebab8a791e344b3b9c1adcf5"], ["AVAX", "0x93da3352f9f1d105fdfe4971cfa80e9dd777bfc5d0f683ebb6e1294b92137bb7"], ["BCH", "0x3dd2b63686a450ec7290df3a1e0b583c0481f651351edfa7636f39aed55cf8a3"], ["BNB", "0x2f95862b045670cd22bee3114c39763a4a08beeb663b145d283c31d7d1101c4f"], ["CRV", "0xa19d04ac696c7a6616d291c7e5d1377cc8be437c327b75adb5dc1bad745fcae8"], ["DOGE", "0xdcef50dd0a4cd2dcc17e45df1676dcb336a11a61c69df7a0299b0150c672d25c"], ["DYDX", "0x6489800bb8974169adfe35937bf6736507097d13c190d760c557108c7e93a81b"], ["GMX", "0xb962539d0fcb272a494d65ea56f94851c2bcf8823935da05bd628916e2e9edbf"], ["LINK", "0x8ac0c70fff57e9aefdf5edf44b51d62c2d433653cbb2cf5cc06bb115af04d221"], ["LTC", "0x6e3f3fa8253588df9326580180233eb791e03b443a3ba7a1d892e73874e19a54"], ["MKR", "0x9375299e31c0deb9c6bc378e6329aab44cb48ec655552a70d4b9050346a30378"], ["NEAR", "0xc415de8d2eba7db216527dff4b60e8f3a5311c740dadb233e13e12547e226750"], ["OP", "0x385f64d993f7b77d8182ed5003d97c60aa3361f3cecfe711544d2d59165e9bdf"], ["ORDI", "0x193c739db502aadcef37c2589738b1e37bdb257d58cf1ab3c7ebc8e6df4e3ec0"], ["PEPE", "0xd69731a2e74ac1ce884fc3890f7ee324b6deb66147055249568869ed700882e4"], ["POL", "0xffd11c5a1cfd42f80afb2df4d9f264c15f956d68153335374ec10722edd70472"], ["PYTH", "0x0bbf28e9a841a1cc788f6a361b17ca072d0ea3098a1e5df1c3922d06719579ff"], ["RUNE", "0x5fcf71143bb70d41af4fa9aa1287e2efd3c5911cee59f909f915c9f61baacb1e"], ["SHIB", "0xf0d57deca57b3da2fe63a493f4c25925fdfd8edf834b20f93e1f84dbd1504d4a"], ["STX", "0xec7a775f46379b5e943c3526b1c8d54cd49749176b0b98e02dde68d1bd335c17"], ["TIA", "0x09f7c1d7dfbb7df2b8fe3d3d87ee94a2259d212da4f30c1f0540d066dfa44723"], ["UNI", "0x78d185a741d07edb3412b09008b7c5cfb9bbbd7d568bf00ba737b456ba171501"], ["XLM", "0xb7a8eba68a997cd0210c2e1e4ee811ad2d174b3611c22d9ebf16f4cb7e9ba850"], ["XRP", "0xec5d399846a9209f3fe5881d70aae9268c94339ff9817e8d18ff19fa05eea1c8"], ["ATOM", "0xb00b60f88b03a6a625a8d1c048c3f66653edf217439983d037e7222c4e612819"], ["BLUR", "0x856aac602516addee497edf6f50d39e8c95ae5fb0da1ed434a8c2ab9c3e877e9"], ["BOME", "0x30e4780570973e438fdb3f1b7ad22618b2fc7333b65c7853a7ca144c39052f7a"], ["BONK", "0x72b021217ca3fe68922a19aaf990109cb9d84e9ad004b4d2025ad6f529314419"], ["COMP", "0x4a8e42861cabc5ecb50996f92e7cfa2bce3fd0a2423b0c44c9b423fb2bd25478"], ["DOT", "0xca3eed9b267293f6595901c734c7525ce8ef49adafe8284606ceb307afa2ca5b"], ["EIGEN", "0xc65db025687356496e8653d0d6608eec64ce2d96e2e28c530e574f0e4f712380"], ["ENA", "0xb7910ba7322db020416fcac28b48c01212fd9cc8fbcbaf7d30477ed8605f6bd4"], ["EOS", "0x06ade621dbc31ed0fc9255caaab984a468abe84164fb2ccc76f02a4636d97e31"], ["ETC", "0x7f5cc8d963fc5b3d2ae41fe5685ada89fd4f14b435f8050f28c7fd409f40c2d8"], ["ETHBTC", "0xc96458d393fe9deb7a7d63a0ac41e2898a67a7750dbd166673279e06c868df0a"], ["ETHFI", "0xb27578a9654246cb0a2950842b92330e9ace141c52b63829cc72d5c45a5a595a"], ["FIL", "0x150ac9b959aee0051e4091f0ef5216d941f590e1c5e7f91cf7635b5c11628c0e"], ["FLOW", "0x2fb245b9a84554a0f15aa123cbb5f64cd263b59e9a87d80148cbffab50c69f30"], ["FTM", "0x5c6c0d2386e3352356c3ab84434fafb5ea067ac2678a38a338c4a69ddc4bdb0c"], ["FXS", "0x735f591e4fed988cd38df74d8fcedecf2fe8d9111664e0fd500db9aa78b316b1"], ["GALA", "0x0781209c28fda797616212b7f94d77af3a01f3e94a5d421760aef020cf2bcb51"], ["GRT", "0x4d1f8dae0d96236fb98e8f47471a366ec3b1732b47041781934ca3a9bb2f35e7"], ["ICP", "0xc9907d786c5821547777780a1e4f89484f3417cb14dd244f2b0a34ea7a554d67"], ["IMX", "0x941320a8989414874de5aa2fc340a75d5ed91fdff1613dd55f83844d52ea63a2"], ["INJ", "0x7a5bc1d2b56ad029048cd63964b3ad2776eadf812edc1a43a31406cb54bff592"], ["IO", "0x82595d1509b770fa52681e260af4dda9752b87316d7c048535d8ead3fa856eb1"], ["JTO", "0xb43660a5f790c69354b0729a5ef9d50d68f1df92107540210b9cccba1f947cc2"], ["JUP", "0x0a0408d619e9380abad35060f9192039ed5042fa6f82301d0e48bb52be830996"], ["LDO", "0xc63e2a7f37a04e5e614c07238bedb25dcc38927fba8fe890597a593c0b2fa4ad"], ["MEME", "0xcd2cee36951a571e035db0dfad138e6ecdb06b517cc3373cd7db5d3609b7927c"], ["MEW", "0x514aed52ca5294177f20187ae883cec4a018619772ddce41efcc36a6448f5d5d"], ["NOT", "0x75ec6f04d4bded6afdc1440689be4402dd1e23d2ff2c21e081871eb2739ceb36"], ["PENDLE", "0x9a4df90b25497f66b1afb012467e316e801ca3d839456db028892fe8c70c8016"], ["PEOPLE", "0xb7fe919d83815ca6074c82a3286b1cd6ffb7d3136e323cd2b1ef706cfc7e5945"], ["POPCAT", "0xb9312a7ee50e189ef045aa3c7842e099b061bd9bdc99ac645956c3b660dc8cce"], ["RENDER", "0x3d4a2bd9535be6ce8059d75eadeba507b043257321aa544717c56fa19b49e35d"], ["SATS", "0x40440d18fb5ad809e2825ce7dfc035cfa57135c13062a04addafe0c7f54425e0"], ["SEI", "0x53614f1cb0c031d4af66c04cb9c756234adad0e1cee85303795091499a4084eb"], ["SNX", "0x39d020f60982ed892abbcd4a06a276a9f9b7bfbce003204c110b6e488f502da3"], ["STRK", "0x6a182399ff70ccf3e06024898942028204125a819e519a335ffa4579e66cd870"], ["SUI", "0x23d7315113f5b1d3ba7a83604c44b94d79f4fd69af77f804fc7f920a6dc65744"], ["SUSHI", "0x26e4f737fde0263a9eea10ae63ac36dcedab2aaf629261a994e1eeb6ee0afe53"], ["TAO", "0x410f41de235f2db824e562ea7ab2d3d3d4ff048316c61d629c0b93f58584e1af"], ["TON", "0x8963217838ab4cf5cadc172203c1f0b763fbaa45f346d8ee50ba994bbcac3026"], ["TRX", "0x67aed5a24fdad045475e7195c98a98aea119c763f272d4523f5bac93a4f33c2b"], ["W", "0xeff7446475e218517566ea99e72a4abec2e1bd8498b43b7d8331e29dcb059389"], ["WLD", "0xd6835ad1f773de4a378115eb6824bd0c0e42d84d1c84d9750e853fb6b6c7794a"], ["YFI", "0x425f4b198ab2504936886c1e93511bb6720fbcf2045a4f3c0723bb213846022f"], ["ZRO", "0x3bd860bea28bf982fa06bcf358118064bb114086cc03993bd76197eaab0b8018"], ["ALGO", "0xfa17ceaf30d19ba51112fdcc750cc83454776f47fb0112e4af07f15f4bb1ebc0"], ["APT", "0x03ae4db29ed4ae33d323568895aa00337e658e348b37509f5372ae51f0af00d5"], ["ARKM", "0x7677dd124dee46cfcd46ff03cf405fb0ed94b1f49efbea3444aadbda939a7ad3"], ["AXL", "0x60144b1d5c9e9851732ad1d9760e3485ef80be39b984f6bf60f82b28a2b7f126"], ["AXS", "0xb7e3904c08ddd9c0c10c6d207d390fd19e87eb6aab96304f571ed94caebdefa0"], ["BAL", "0x07ad7b4a7662d19a6bc675f6b467172d2f3947fa653ca97555a9b20236406628"], ["GOAT", "0xf7731dc812590214d3eb4343bfb13d1b4cfa9b1d4e020644b5d5d8e07d60c66c"], ["MOODENG", "0xffff73128917a90950cd0473fd2551d7cd274fd5a6cc45641881bbcc6ee73417"], ["SAFE", "0x7b3576858506a94fad3a9cc55e32934f0c3931150fe3a3c7b83558dbae5b8e38"], ["PNUT", "0x116da895807f81f6b5c5f01b109376e7f6834dc8b51365ab7cdfa66634340e54"], ["CAT", "0xdf7b724fae4b9ecfd6081effb1ea3bb3b5c250c3641cf903b4d8fb10f4f452df"], ["DEGEN", "0x9c93e4a22c56885af427ac4277437e756e7ec403fbc892f975d497383bb33560"], ["SLERF", "0x1a483c4a63876d286991ac0d6e090298db42e88c3826b6e0cff89daca498eed5"], ["AERO", "0x9db37f4d5654aad3e37e2e14ffd8d53265fb3026d1d8f91146539eebaa2ef45f"], ["CHILLGUY", "0xd98869edbb4a0d2803dc1054405bceb1ddc546bfc9a3d0e31bb0e0448e6181e1"], ["MORPHO", "0x5b2a4c542d4a74dd11784079ef337c0403685e3114ba0d9909b5c7a7e06fdc42"], ["SOLETH", "0xde87506dabfadbef89af2d5d796ebae80ddaea240fc7667aa808fce3629cd8fb"], // Collaterals ["USDC", "0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a"], ["USDE", "0x6ec879b1e9963de5ee97e9c8710b742d6228252a5e2ca12d4ae81d7fe5ee8c5d"], ["DAI", "0xb0948a5e5313200c632b51bb5ca32f6de0d36e9950a942d19751e833f70dabfd"] ]); // src/pyth/index.ts import { EvmPriceServiceConnection } from "@pythnetwork/pyth-evm-js"; import { formatUnits } from "viem"; var Pyth = class { constructor(synthetixSdk, pythConfig) { /** * The function accepts an array of priceIds and publish Timestamp and * returns the priceUpdateData from Pyth for price ids array * @param priceIds Pyth Price IDs array * @param publishTime Publish time for benchmark data * @returns Encoded Pyth price update data */ this.getVaaPriceUpdateData = async (priceIds, publishTime) => { let priceUpdateData = []; try { const response = await this.pythClient.get(`/v2/updates/price/${publishTime}`, { params: { ids: priceIds, encoding: "hex" } }); if (response.status == 200) { const responseData = response.data; priceUpdateData = responseData.binary?.data ?? []; } else { throw new Error("Error fetching data from Pyth"); } } catch (error) { this.sdk.logger.error("Error fetching data from Pyth", error); throw error; } const updateData = priceUpdateData.map((vaa) => "0x" + vaa); return updateData; }; this.sdk = synthetixSdk; this.pythClient = {}; this.pythConfig = pythConfig; let pythEndpoint; if (pythConfig != void 0) { pythEndpoint = pythConfig.pythEndpoint ?? PUBLIC_PYTH_ENDPOINT; } else { pythEndpoint = PUBLIC_PYTH_ENDPOINT; } this.pythConnection = new EvmPriceServiceConnection(pythEndpoint, { timeout: DEFAULT_PYTH_TIMEOUT }); this.priceFeedIds = /* @__PURE__ */ new Map(); } async initPyth() { this.pythClient = await this.getPythClient(); } /** * Get Pyth client instance * @returns Pyth client object based on the params provided */ async getPythClient() { let pythServiceEndpoint, pythServiceUsername, pythServicePassword; if (this.pythConfig == void 0 || this.pythConfig.pythEndpoint == void 0) { pythServiceEndpoint = PUBLIC_PYTH_ENDPOINT; } else { pythServiceEndpoint = this.pythConfig.pythEndpoint; pythServiceUsername = this.pythConfig.username; pythServicePassword = this.pythConfig.password; } try { if (pythServiceEndpoint) { if (pythServiceUsername && pythServicePassword) { return axios.create({ baseURL: pythServiceEndpoint, auth: { username: pythServiceUsername, password: pythServicePassword }, timeout: this.pythConfig?.cacheTtl ?? DEFAULT_PYTH_TIMEOUT }); } else { return axios.create({ baseURL: pythServiceEndpoint }); } } else { return axios.create({ baseURL: PUBLIC_PYTH_ENDPOINT, timeout: this.pythConfig?.cacheTtl ?? DEFAULT_PYTH_TIMEOUT }); } } catch (error) { console.error("Error when creating Pyth instance:", error); throw error; } } /** * Update the price feed IDs for the Pyth price service. * Additionally sets a lookup for feedId to symbol. * @param updatedPriceFeeds Dictionary of feed IDs to update */ updatePriceFeedIds(updatedPriceFeeds) { updatedPriceFeeds.forEach((feed) => { this.priceFeedIds.set(feed.symbol, feed.feedId); }); } /** * Fetch the latest Pyth price data for a list of feed ids. This is the most reliable way to * specify the exact feed you want to fetch data for. * @param feedIds An array of price feed ids to fetch the data for */ async getPriceFromIds(feedIds) { const pythPrice = await this.pythConnection.getLatestPriceFeeds(feedIds); return pythPrice; } /** * Fetch the latest Pyth price data for a list of market symbols. This * function is the same as ``getPriceFromIds`` but uses the symbol * to fetch the feed id from the lookup table. * @param symbols Array of token symbols to get the price details */ async getPriceFromSymbols(symbols) { const feedIds = []; symbols.forEach((tokenSymbol) => { const feedId = this.priceFeedIds.get(tokenSymbol); if (feedId != void 0) { feedIds.push(feedId); } }); return this.getPriceFromIds(feedIds); } /** * Gets price update data which then can be submitted to Pyth contract to update the prices. * This will throw an axios error if there is a network problem or the price service returns * a non-ok response (e.g: Invalid price ids) * @param priceIds Array of hex-encoded price ids. * @returns Array of price update data. */ async getPriceFeedsUpdateData(feedIds) { const priceUpdateData = await this.pythConnection.getPriceFeedsUpdateData(feedIds); return priceUpdateData; } /** * * @param priceId Pyth price id of the token symbol to fetch the price for * @param publishTime Publish time for benchmark data * @returns */ async getFormattedPrice(priceId, publishTime) { let pythPrice; if (publishTime != void 0) { pythPrice = await this.pythConnection.getPriceFeed(priceId, publishTime); } else { const price2 = await this.pythConnection.getLatestPriceFeeds([priceId]); if (price2 != void 0 && price2.length > 0) { pythPrice = price2[0]; } else { throw new Error("Error while fetching Pyth price"); } } const priceUnchecked = pythPrice.getPriceUnchecked(); const price = Number(formatUnits(BigInt(priceUnchecked.price), Math.abs(priceUnchecked.expo))); if (price == 0) { throw new Error("Invalid price: 0"); } return price; } /** * * @param priceId Pyth price id of the token symbol to fetch the price for * @param publishTime Publish time for benchmark data * @returns */ async getFormattedPriceSymbol(symbol, publishTime) { const feedId = this.priceFeedIds.get(symbol); if (feedId != void 0) { return this.getFormattedPrice(feedId, publishTime); } else { throw new Error("Invalid token symbol"); } } /** * Returns a mapping of token symbol to price ids from constants * @returns A map of Token symbol to price id */ getPriceIdsFromConstants() { return SYMBOL_TO_PYTH_FEED; } }; export { Pyth }; //# sourceMappingURL=index.mjs.map