|
|
|
|
@ -2,12 +2,13 @@ const {
|
|
|
|
|
updateCachedPrice, |
|
|
|
|
loadCache |
|
|
|
|
} = require('./price_engine'); |
|
|
|
|
const { fetchPair } = require('../backend/providers/klingex'); |
|
|
|
|
|
|
|
|
|
async function fetchJson(url) { |
|
|
|
|
const res = await fetch(url, { |
|
|
|
|
headers: { |
|
|
|
|
'accept': 'application/json', |
|
|
|
|
'user-agent': 'otb-oracle/0.2' |
|
|
|
|
'user-agent': 'otb-oracle/0.3' |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
@ -18,16 +19,10 @@ async function fetchJson(url) {
|
|
|
|
|
return res.json(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function fetchCoinGeckoSimplePrice() { |
|
|
|
|
const ids = [ |
|
|
|
|
'usd-coin', |
|
|
|
|
'ethereum', |
|
|
|
|
'ether-1', |
|
|
|
|
'etica' |
|
|
|
|
].join(','); |
|
|
|
|
|
|
|
|
|
async function fetchCoinGeckoSimplePrice(ids) { |
|
|
|
|
const joined = Array.isArray(ids) ? ids.join(',') : String(ids || ''); |
|
|
|
|
const url = |
|
|
|
|
`https://api.coingecko.com/api/v3/simple/price?ids=${encodeURIComponent(ids)}&vs_currencies=usd,cad`; |
|
|
|
|
`https://api.coingecko.com/api/v3/simple/price?ids=${encodeURIComponent(joined)}&vs_currencies=usd,cad`; |
|
|
|
|
|
|
|
|
|
return fetchJson(url); |
|
|
|
|
} |
|
|
|
|
@ -37,16 +32,23 @@ async function fetchCoinPaprikaTicker(coinId) {
|
|
|
|
|
return fetchJson(url); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getCadPerUsd(coingeckoData) { |
|
|
|
|
if (coingeckoData?.['usd-coin']?.cad && Number.isFinite(Number(coingeckoData['usd-coin'].cad))) { |
|
|
|
|
return Number(coingeckoData['usd-coin'].cad); |
|
|
|
|
function hasFinitePrice(value) { |
|
|
|
|
return value !== null && value !== undefined && Number.isFinite(Number(value)) && Number(value) > 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getCachedCadPerUsd() { |
|
|
|
|
try { |
|
|
|
|
const cache = loadCache(); |
|
|
|
|
const cachedCad = cache?.assets?.USDC_ARB?.price_cad; |
|
|
|
|
if (cachedCad && Number.isFinite(Number(cachedCad))) { |
|
|
|
|
return Number(cachedCad); |
|
|
|
|
const usdc = cache?.assets?.USDC_ARB || {}; |
|
|
|
|
const usd = Number(usdc.price_usd); |
|
|
|
|
const cad = Number(usdc.price_cad); |
|
|
|
|
|
|
|
|
|
if (hasFinitePrice(usd) && hasFinitePrice(cad)) { |
|
|
|
|
return cad / usd; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (hasFinitePrice(cad)) { |
|
|
|
|
return cad; |
|
|
|
|
} |
|
|
|
|
} catch (_) { |
|
|
|
|
} |
|
|
|
|
@ -54,11 +56,7 @@ function getCadPerUsd(coingeckoData) {
|
|
|
|
|
return 1.38; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function hasFinitePrice(value) { |
|
|
|
|
return value !== null && value !== undefined && Number.isFinite(Number(value)) && Number(value) > 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function maybeUpdateFromCoinGecko(pairKey, cgKey, sourceData, now) { |
|
|
|
|
function maybeUpdateFromCoinGecko(pairKey, sourceData, now, sourceStatus = 'primary') { |
|
|
|
|
if (!sourceData || !hasFinitePrice(sourceData.usd) || !hasFinitePrice(sourceData.cad)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
@ -67,14 +65,14 @@ function maybeUpdateFromCoinGecko(pairKey, cgKey, sourceData, now) {
|
|
|
|
|
price_usd: Number(sourceData.usd), |
|
|
|
|
price_cad: Number(sourceData.cad), |
|
|
|
|
source: 'coingecko', |
|
|
|
|
source_status: 'primary', |
|
|
|
|
source_status: sourceStatus, |
|
|
|
|
updated_at: now |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function maybeUpdateFromCoinPaprika(pairKey, tickerData, cadPerUsd, now) { |
|
|
|
|
function maybeUpdateFromCoinPaprika(pairKey, tickerData, cadPerUsd, now, sourceStatus = 'primary') { |
|
|
|
|
const usd = Number(tickerData?.quotes?.USD?.price); |
|
|
|
|
|
|
|
|
|
if (!hasFinitePrice(usd)) { |
|
|
|
|
@ -87,74 +85,85 @@ function maybeUpdateFromCoinPaprika(pairKey, tickerData, cadPerUsd, now) {
|
|
|
|
|
price_usd: usd, |
|
|
|
|
price_cad: cad, |
|
|
|
|
source: 'coinpaprika', |
|
|
|
|
source_status: 'fallback', |
|
|
|
|
source_status: sourceStatus, |
|
|
|
|
updated_at: now |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function main() { |
|
|
|
|
const now = new Date().toISOString(); |
|
|
|
|
const updatedPairs = []; |
|
|
|
|
|
|
|
|
|
let cgData = {}; |
|
|
|
|
async function updateFromPaprika(pairKey, coinId, cadPerUsd, now, updatedPairs, sourceStatus = 'primary') { |
|
|
|
|
try { |
|
|
|
|
cgData = await fetchCoinGeckoSimplePrice(); |
|
|
|
|
const ticker = await fetchCoinPaprikaTicker(coinId); |
|
|
|
|
if (maybeUpdateFromCoinPaprika(pairKey, ticker, cadPerUsd, now, sourceStatus)) { |
|
|
|
|
updatedPairs.push(pairKey); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} catch (err) { |
|
|
|
|
console.error(`CoinGecko fetch failed: ${err.message}`); |
|
|
|
|
cgData = {}; |
|
|
|
|
console.error(`CoinPaprika ${pairKey} fetch failed: ${err.message}`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const cadPerUsd = getCadPerUsd(cgData); |
|
|
|
|
|
|
|
|
|
if (maybeUpdateFromCoinGecko('USDC_ARB', 'usd-coin', cgData['usd-coin'], now)) { |
|
|
|
|
updatedPairs.push('USDC_ARB'); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (maybeUpdateFromCoinGecko('ETH_ETH', 'ethereum', cgData['ethereum'], now)) { |
|
|
|
|
updatedPairs.push('ETH_ETH'); |
|
|
|
|
async function updateETHOFromKlingEx(now, updatedPairs, cadPerUsd) { |
|
|
|
|
try { |
|
|
|
|
const r = await fetchPair('ETHO-USDT'); |
|
|
|
|
const usd = Number(r.price); |
|
|
|
|
|
|
|
|
|
if (!hasFinitePrice(usd)) { |
|
|
|
|
throw new Error(`KlingEx returned non-usable ETHO price: ${r.price}`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let ethoUpdated = maybeUpdateFromCoinGecko('ETHO_ETHO', 'ether-1', cgData['ether-1'], now); |
|
|
|
|
if (!ethoUpdated) { |
|
|
|
|
try { |
|
|
|
|
const pap = await fetchCoinPaprikaTicker('etho-ethoprotocol'); |
|
|
|
|
if (maybeUpdateFromCoinPaprika('ETHO_ETHO', pap, cadPerUsd, now)) { |
|
|
|
|
const cad = Number((usd * cadPerUsd).toFixed(8)); |
|
|
|
|
|
|
|
|
|
updateCachedPrice('ETHO_ETHO', { |
|
|
|
|
price_usd: usd, |
|
|
|
|
price_cad: cad, |
|
|
|
|
source: r.source || 'klingex', |
|
|
|
|
source_status: 'primary', |
|
|
|
|
updated_at: now |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
updatedPairs.push('ETHO_ETHO'); |
|
|
|
|
ethoUpdated = true; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} catch (err) { |
|
|
|
|
console.error(`Coinpaprika ETHO fallback failed: ${err.message}`); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
updatedPairs.push('ETHO_ETHO'); |
|
|
|
|
console.error(`KlingEx ETHO fetch failed: ${err.message}`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let etiUpdated = maybeUpdateFromCoinGecko('ETI_ETICA', 'etica', cgData['etica'], now); |
|
|
|
|
if (!etiUpdated) { |
|
|
|
|
// Last-resort fallback only.
|
|
|
|
|
try { |
|
|
|
|
const pap = await fetchCoinPaprikaTicker('eti-etica'); |
|
|
|
|
if (maybeUpdateFromCoinPaprika('ETI_ETICA', pap, cadPerUsd, now)) { |
|
|
|
|
updatedPairs.push('ETI_ETICA'); |
|
|
|
|
etiUpdated = true; |
|
|
|
|
const cgData = await fetchCoinGeckoSimplePrice(['ether-1']); |
|
|
|
|
if (maybeUpdateFromCoinGecko('ETHO_ETHO', cgData['ether-1'], now, 'fallback')) { |
|
|
|
|
updatedPairs.push('ETHO_ETHO'); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.error('CoinGecko ETHO fallback returned no usable ether-1 price'); |
|
|
|
|
} catch (err) { |
|
|
|
|
console.error(`Coinpaprika ETI fallback failed: ${err.message}`); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
updatedPairs.push('ETI_ETICA'); |
|
|
|
|
console.error(`CoinGecko ETHO fallback failed: ${err.message}`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
const papEgaz = await fetchCoinPaprikaTicker('egaz-egaz'); |
|
|
|
|
if (maybeUpdateFromCoinPaprika('EGAZ_ETICA', papEgaz, cadPerUsd, now)) { |
|
|
|
|
updatedPairs.push('EGAZ_ETICA'); |
|
|
|
|
} |
|
|
|
|
} catch (err) { |
|
|
|
|
console.error(`Coinpaprika EGAZ fetch failed: ${err.message}`); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function main() { |
|
|
|
|
const now = new Date().toISOString(); |
|
|
|
|
const updatedPairs = []; |
|
|
|
|
let cadPerUsd = getCachedCadPerUsd(); |
|
|
|
|
|
|
|
|
|
await updateFromPaprika('USDC_ARB', 'usdc-usd-coin', cadPerUsd, now, updatedPairs, 'primary'); |
|
|
|
|
|
|
|
|
|
cadPerUsd = getCachedCadPerUsd(); |
|
|
|
|
|
|
|
|
|
await updateFromPaprika('ETH_ETH', 'eth-ethereum', cadPerUsd, now, updatedPairs, 'primary'); |
|
|
|
|
|
|
|
|
|
// ETHO uses KlingEx primary; CoinGecko only as last fallback.
|
|
|
|
|
await updateETHOFromKlingEx(now, updatedPairs, cadPerUsd); |
|
|
|
|
|
|
|
|
|
await updateFromPaprika('ETI_ETICA', 'eti-etica', cadPerUsd, now, updatedPairs, 'fallback'); |
|
|
|
|
await updateFromPaprika('EGAZ_ETICA', 'egaz-egaz', cadPerUsd, now, updatedPairs, 'fallback'); |
|
|
|
|
|
|
|
|
|
console.log(JSON.stringify({ |
|
|
|
|
ok: true, |
|
|
|
|
fetched_at: now, |
|
|
|
|
|