You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.9 KiB
87 lines
1.9 KiB
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', |
|
'ether-1', |
|
'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['ether-1']) { |
|
updateCachedPrice('ETHO_ETHO', { |
|
price_usd: data['ether-1'].usd ?? null, |
|
price_cad: data['ether-1'].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); |
|
});
|
|
|