@bitte-ai/agent-sdk
Version:
Agent SDK for Bitte Protocol
36 lines (35 loc) • 1.18 kB
JavaScript
const BINANCE_API = "https://api.binance.com/api/v3/ticker/price?symbol=NEARUSDT";
const COIN_GECKO_API = "https://api.coingecko.com/api/v3/simple/price?ids=near&vs_currencies=usd";
export async function getNearPriceUSD() {
const errs = [];
try {
const price = await coinGeckoPrice();
return price;
}
catch (err) {
errs.push(`coingecko: ${err}`);
}
try {
const price = await binancePrice();
return price;
}
catch (err) {
errs.push(`binance: ${err}`);
}
throw new Error(`All price providers failed:\n- ${errs.join("\n- ")}`);
}
export async function binancePrice() {
return fetchPrice(BINANCE_API, (x) => x.price);
}
export async function coinGeckoPrice() {
return fetchPrice(COIN_GECKO_API, (x) => x.near.usd);
}
// TODO: find out the token ID here and implement this.
// const LLAMA_FI_API = 'https://coins.llama.fi/prices/current/near-protocol';
async function fetchPrice(url, tokenPrice) {
const res = await fetch(url);
if (!res.ok)
throw new Error(`Failed to fetch NEAR price: ${res.statusText}`);
const data = (await res.json());
return tokenPrice(data);
}