@bitte-ai/agent-sdk
Version:
Agent SDK for Bitte Protocol
41 lines (40 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNearPriceUSD = getNearPriceUSD;
exports.binancePrice = binancePrice;
exports.coinGeckoPrice = coinGeckoPrice;
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";
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- ")}`);
}
async function binancePrice() {
return fetchPrice(BINANCE_API, (x) => x.price);
}
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);
}