@agentek/tools
Version:
Blockchain tools for AI agents
70 lines • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDefiLlamaPools = fetchDefiLlamaPools;
exports.fetchPoolHistoricalData = fetchPoolHistoricalData;
exports.fetchProtocolData = fetchProtocolData;
const constants_js_1 = require("../constants.js");
const helpers_js_1 = require("./helpers.js");
// Fetch pool data from DefiLlama
async function fetchDefiLlamaPools() {
const response = await fetch(constants_js_1.PROTOCOL_API_ENDPOINTS.DefiLlama);
if (!response.ok) {
throw new Error(`Failed to fetch from DefiLlama: ${response.statusText}`);
}
return await response.json();
}
// Fetch historical chart data for a specific pool
async function fetchPoolHistoricalData(poolId) {
const apiUrl = `${constants_js_1.PROTOCOL_API_ENDPOINTS.DefiLlamaChart}/${poolId}`;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Failed to fetch from DefiLlama: ${response.statusText}`);
}
const data = await response.json();
if (!data.data || data.data.length === 0) {
throw new Error(`No historical data found for pool ID: ${poolId}`);
}
return data;
}
// Normalized fetching logic for different protocols
async function fetchProtocolData(protocol, chainId) {
try {
// Fetch data from DefiLlama yields API
const data = await fetchDefiLlamaPools();
// Filter by project if specific protocol is requested (except DefiLlama)
let filteredData = data.data;
if (protocol !== 'DefiLlama') {
const projectFilter = (0, helpers_js_1.getProjectFilter)(protocol);
if (projectFilter) {
filteredData = filteredData.filter(pool => pool.project.toLowerCase().includes(projectFilter.toLowerCase()));
}
}
// Filter by chain ID if specified
if (chainId) {
filteredData = filteredData.filter(pool => {
const poolChainId = helpers_js_1.chainIdMap[pool.chain];
return poolChainId === chainId;
});
}
// Map to YieldData format
return filteredData.map(pool => {
// Get apy value, using base APY if total APY is null
const apyValue = pool.apy !== null ? pool.apy :
(pool.apyBase !== null ? pool.apyBase : 0);
return {
protocol: protocol,
asset: pool.project,
symbol: pool.symbol,
apy: apyValue,
tvl: pool.tvlUsd,
chain: helpers_js_1.chainIdMap[pool.chain] || 1, // Default to Ethereum if chain not found
risk: (0, helpers_js_1.assessRisk)(apyValue),
};
});
}
catch (error) {
console.error(`Error fetching yield data for ${protocol}:`, error);
return [];
}
}
//# sourceMappingURL=api.js.map