// Price of PIXA on the WPIXA / SWAP.HIVE pool, expressed as PIXA per US Big Mac. // Prices and thresholds below are shared by the preview and the hourly publisher. export const POOL = 'WPIXA:SWAP.HIVE'; /** Under this much traded in a day (in dollars), the day's average says too little and the pool's own price is used. */ export const LEAST_DAY_USD = 2; /** The most the feed moves from the witness's own last feed in one publish. */ export const MOST_STEP = 0.1; /** A feed older than this no longer counts on chain (HIVE_MAX_FEED_AGE_SECONDS), so it doesn't hold the next one back. */ export const FEED_AGE_MS = 7 * 86_400_000; const amount = (text, symbol) => { const match = new RegExp(`^(\\d+(?:\\.\\d+)?) ${symbol}$`).exec(String(text ?? '')); return match ? Number(match[1]) : NaN; }; /** Hive's median price of a HIVE in HBD, an HBD being counted as a dollar as Hive counts it. */ export function hiveUsd(median) { const usd = amount(median?.base, 'HBD') / amount(median?.quote, 'HIVE'); if (!Number.isFinite(usd) || usd < 0.001 || usd > 100) throw new Error('Hive’s median price is missing or out of range.'); return usd; } /** * A PIXA in HIVE from Pixa Markets' answer for its pools: the day's trades (HIVE paid over WPIXA bought, both ways) when * at least LEAST_DAY_USD changed hands, else the pool's price as it stands. */ export function pixaInHive(pools, usdPerHive) { const pool = (Array.isArray(pools?.pools) ? pools.pools : []).find((one) => one?.venue === 'hive-engine' && one?.id === POOL && one?.exists !== false); if (!pool) throw new Error(`Pixa Markets has no ${POOL} pool.`); const day = pool.volume?.day; const pixa = Number(day?.base); const hive = Number(day?.quote); const traded = Number.isFinite(pixa) && Number.isFinite(hive) && pixa > 0 && hive > 0 ? hive * usdPerHive : 0; if (traded >= LEAST_DAY_USD) return { hive: hive / pixa, from: 'traded', tradedUsd: traded, since: day.since ?? null }; const price = Number(pool.price); if (!Number.isFinite(price) || price <= 0) throw new Error('The pool has no price.'); return { hive: price, from: 'pool', tradedUsd: traded, since: null }; } /** PIXA to a PXS: a Big Mac over a PIXA's dollars. */ export function ratioFor(bigMacUsd, pixaUsd) { if (!Number.isFinite(pixaUsd) || pixaUsd < 0.0001 || pixaUsd > 10) throw new Error('The PIXA price is out of range.'); const ratio = bigMacUsd / pixaUsd; if (!Number.isFinite(ratio) || ratio < 0.1 || ratio > 1_000_000) throw new Error('The PIXA to PXS ratio is out of range.'); return ratio; } /** The witness's own feed now, as PIXA to a PXS, or null when it has none that still counts. */ export function ownRatio(witness, now = Date.now()) { const rate = witness?.pxs_exchange_rate ?? witness?.hbd_exchange_rate; const ratio = amount(rate?.quote, 'PIXA') / amount(rate?.base, 'PXS'); const at = Date.parse(`${witness?.last_pxs_exchange_update ?? witness?.last_hbd_exchange_update ?? ''}Z`); return Number.isFinite(ratio) && ratio > 0 && Number.isFinite(at) && now - at <= FEED_AGE_MS ? ratio : null; } /** The ratio to publish: the target, but no more than MOST_STEP from the last feed in one go. */ export function stepped(last, target, most = MOST_STEP) { if (last === null) return target; return Math.min(last * (1 + most), Math.max(last / (1 + most), target)); }