@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
45 lines (44 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchVaultHistoricalData = fetchVaultHistoricalData;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
/**
* Fetch historical data for a vault
*
* @param args - The arguments for the action
* @param apiKey - The API key to use for the request
* @returns The historical data for the vault
*/
async function fetchVaultHistoricalData(args, apiKey) {
const params = (0, utils_1.createSearchParams)({
interval: args.apyRange ?? "7day",
});
const timestamp = new Date(args.date).getTime() / 1000;
const [tvlResponse, apyResponse] = await Promise.all([
fetch(`${constants_1.VAULTS_API_URL}/vaults/${args.network}/${args.vaultAddress}/historical-tvl/${timestamp}?${params}`, {
method: "GET",
headers: {
"x-api-key": apiKey,
},
}),
fetch(`${constants_1.VAULTS_API_URL}/vaults/${args.network}/${args.vaultAddress}/historical-apy/${timestamp}?${params}`, {
method: "GET",
headers: {
"x-api-key": apiKey,
},
}),
]);
const [apy, tvl] = await Promise.all([
apyResponse.json(),
tvlResponse.json(),
]);
if ("error" in apy)
return apy;
if ("error" in tvl)
return tvl;
return {
apy,
tvl,
};
}