UNPKG

@pythnetwork/price-pusher

Version:
62 lines (61 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertDefined = exports.addLeading0x = exports.customGasChainIds = exports.txSpeeds = void 0; exports.sleep = sleep; exports.removeLeading0x = removeLeading0x; exports.isWsEndpoint = isWsEndpoint; exports.verifyValidOption = verifyValidOption; exports.filterInvalidPriceItems = filterInvalidPriceItems; exports.txSpeeds = ["slow", "standard", "fast"]; exports.customGasChainIds = [137]; async function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } function removeLeading0x(id) { if (id.startsWith("0x")) { return id.substring(2); } return id; } const addLeading0x = (id) => hasLeading0x(id) ? id : `0x${id}`; exports.addLeading0x = addLeading0x; const hasLeading0x = (input) => input.startsWith("0x"); function isWsEndpoint(endpoint) { const url = new URL(endpoint); const protocol = url.protocol; if (protocol === "ws:" || protocol == "wss:") { return true; } return false; } function verifyValidOption(option, validOptions) { if (validOptions.includes(option)) { return option; } const errorString = option + " is not a valid option. Please choose between " + validOptions; throw new Error(errorString); } const assertDefined = (value) => { if (value === undefined) { throw new Error("Assertion failed: value was undefined"); } else { return value; } }; exports.assertDefined = assertDefined; async function filterInvalidPriceItems(hermesClient, priceItems) { const priceMetadata = await hermesClient.getPriceFeeds(); const allPriceIds = priceMetadata.map((priceMetadata) => priceMetadata.id); // Filter out invalid price ids const { existingPriceItems, invalidPriceItems } = priceItems.reduce((acc, item) => { if (allPriceIds.includes(item.id)) { acc.existingPriceItems.push(item); } else { acc.invalidPriceItems.push(item); } return acc; }, { existingPriceItems: [], invalidPriceItems: [] }); return { existingPriceItems, invalidPriceItems }; }