2 changed files with 111 additions and 23 deletions
@ -0,0 +1,88 @@
|
||||
const { |
||||
updateCachedPrice |
||||
} = require('./price_engine'); |
||||
|
||||
async function fetchJson(url) { |
||||
const res = await fetch(url, { |
||||
headers: { |
||||
'accept': 'application/json', |
||||
'user-agent': 'otb-oracle/0.1' |
||||
} |
||||
}); |
||||
|
||||
if (!res.ok) { |
||||
throw new Error(`HTTP ${res.status} from ${url}`); |
||||
} |
||||
|
||||
return res.json(); |
||||
} |
||||
|
||||
async function fetchCoinGeckoSimplePrice() { |
||||
const ids = [ |
||||
'usd-coin', |
||||
'ethereum', |
||||
'etho-protocol', |
||||
'etica' |
||||
].join(','); |
||||
|
||||
const url = |
||||
`https://api.coingecko.com/api/v3/simple/price?ids=${encodeURIComponent(ids)}&vs_currencies=usd,cad`; |
||||
|
||||
return fetchJson(url); |
||||
} |
||||
|
||||
async function main() { |
||||
const data = await fetchCoinGeckoSimplePrice(); |
||||
const now = new Date().toISOString(); |
||||
|
||||
if (data['usd-coin']) { |
||||
updateCachedPrice('USDC_ARB', { |
||||
price_usd: data['usd-coin'].usd ?? null, |
||||
price_cad: data['usd-coin'].cad ?? null, |
||||
source: 'coingecko', |
||||
source_status: 'primary', |
||||
updated_at: now |
||||
}); |
||||
} |
||||
|
||||
if (data['ethereum']) { |
||||
updateCachedPrice('ETH_ETH', { |
||||
price_usd: data['ethereum'].usd ?? null, |
||||
price_cad: data['ethereum'].cad ?? null, |
||||
source: 'coingecko', |
||||
source_status: 'primary', |
||||
updated_at: now |
||||
}); |
||||
} |
||||
|
||||
if (data['etho-protocol']) { |
||||
updateCachedPrice('ETHO_ETHO', { |
||||
price_usd: data['etho-protocol'].usd ?? null, |
||||
price_cad: data['etho-protocol'].cad ?? null, |
||||
source: 'coingecko', |
||||
source_status: 'primary', |
||||
updated_at: now |
||||
}); |
||||
} |
||||
|
||||
if (data['etica']) { |
||||
updateCachedPrice('ETI_ETICA', { |
||||
price_usd: data['etica'].usd ?? null, |
||||
price_cad: data['etica'].cad ?? null, |
||||
source: 'coingecko', |
||||
source_status: 'primary', |
||||
updated_at: now |
||||
}); |
||||
} |
||||
|
||||
console.log(JSON.stringify({ |
||||
ok: true, |
||||
fetched_at: now, |
||||
updated_pairs: ['USDC_ARB', 'ETH_ETH', 'ETHO_ETHO', 'ETI_ETICA'] |
||||
}, null, 2)); |
||||
} |
||||
|
||||
main().catch(err => { |
||||
console.error(`fetch_prices.js failed: ${err.message}`); |
||||
process.exit(1); |
||||
}); |
||||
Loading…
Reference in new issue