Browse Source

Add initial CoinGecko cache fetcher

main
def 1 week ago
parent
commit
337f4a8cd2
  1. 88
      oracle/fetch_prices.js
  2. 46
      oracle/price_cache.json

88
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);
});

46
oracle/price_cache.json

@ -1,29 +1,29 @@
{ {
"version": "0.1", "version": "0.1",
"updated_at": null, "updated_at": "2026-03-15T00:06:52.407Z",
"status": "empty", "status": "fresh",
"assets": { "assets": {
"USDC_ARB": { "USDC_ARB": {
"symbol": "USDC", "symbol": "USDC",
"chain": "arbitrum", "chain": "arbitrum",
"price_usd": null, "price_usd": 1,
"price_cad": null, "price_cad": 1.38,
"source": null, "source": "coingecko",
"source_status": null, "source_status": "primary",
"updated_at": null, "updated_at": "2026-03-15T00:06:52.404Z",
"age_seconds": null, "age_seconds": 0,
"stale": true "stale": false
}, },
"ETH_ETH": { "ETH_ETH": {
"symbol": "ETH", "symbol": "ETH",
"chain": "ethereum", "chain": "ethereum",
"price_usd": null, "price_usd": 2095.26,
"price_cad": null, "price_cad": 2893.66,
"source": null, "source": "coingecko",
"source_status": null, "source_status": "primary",
"updated_at": null, "updated_at": "2026-03-15T00:06:52.404Z",
"age_seconds": null, "age_seconds": 0,
"stale": true "stale": false
}, },
"ETHO_ETHO": { "ETHO_ETHO": {
"symbol": "ETHO", "symbol": "ETHO",
@ -50,13 +50,13 @@
"ETI_ETICA": { "ETI_ETICA": {
"symbol": "ETI", "symbol": "ETI",
"chain": "etica", "chain": "etica",
"price_usd": null, "price_usd": 0.050224,
"price_cad": null, "price_cad": 0.069362,
"source": null, "source": "coingecko",
"source_status": null, "source_status": "primary",
"updated_at": null, "updated_at": "2026-03-15T00:06:52.404Z",
"age_seconds": null, "age_seconds": 0,
"stale": true "stale": false
} }
} }
} }

Loading…
Cancel
Save