diff --git a/oracle/fetch_prices.js b/oracle/fetch_prices.js new file mode 100644 index 0000000..07edc6f --- /dev/null +++ b/oracle/fetch_prices.js @@ -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); +}); diff --git a/oracle/price_cache.json b/oracle/price_cache.json index ba6f0f5..f1ff8f8 100644 --- a/oracle/price_cache.json +++ b/oracle/price_cache.json @@ -1,29 +1,29 @@ { "version": "0.1", - "updated_at": null, - "status": "empty", + "updated_at": "2026-03-15T00:06:52.407Z", + "status": "fresh", "assets": { "USDC_ARB": { "symbol": "USDC", "chain": "arbitrum", - "price_usd": null, - "price_cad": null, - "source": null, - "source_status": null, - "updated_at": null, - "age_seconds": null, - "stale": true + "price_usd": 1, + "price_cad": 1.38, + "source": "coingecko", + "source_status": "primary", + "updated_at": "2026-03-15T00:06:52.404Z", + "age_seconds": 0, + "stale": false }, "ETH_ETH": { "symbol": "ETH", "chain": "ethereum", - "price_usd": null, - "price_cad": null, - "source": null, - "source_status": null, - "updated_at": null, - "age_seconds": null, - "stale": true + "price_usd": 2095.26, + "price_cad": 2893.66, + "source": "coingecko", + "source_status": "primary", + "updated_at": "2026-03-15T00:06:52.404Z", + "age_seconds": 0, + "stale": false }, "ETHO_ETHO": { "symbol": "ETHO", @@ -50,13 +50,13 @@ "ETI_ETICA": { "symbol": "ETI", "chain": "etica", - "price_usd": null, - "price_cad": null, - "source": null, - "source_status": null, - "updated_at": null, - "age_seconds": null, - "stale": true + "price_usd": 0.050224, + "price_cad": 0.069362, + "source": "coingecko", + "source_status": "primary", + "updated_at": "2026-03-15T00:06:52.404Z", + "age_seconds": 0, + "stale": false } } }