1inch-agent-kit
Version:
AI Agent Kit for 1inch - Connect any LLM to 1inch DeFi protocols
109 lines • 4.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHistoryEvents = getHistoryEvents;
exports.postHistoryEvents = postHistoryEvents;
exports.searchHistoryEvents = searchHistoryEvents;
exports.getSwapEvents = getSwapEvents;
exports.historyAPI = historyAPI;
const fetcher_1 = require("../../utils/fetcher");
// Individual endpoint functions
async function getHistoryEvents(params) {
const apiKey = process.env.ONEINCH_API_KEY;
if (!apiKey) {
throw new Error("1inch API key is required. Set ONEINCH_API_KEY environment variable.");
}
const fetcher = new fetcher_1.OneInchFetcher(apiKey);
let url = `/history/v2.0/history/${params.address}/events`;
const queryParams = new URLSearchParams();
if (params.limit)
queryParams.append('limit', params.limit.toString());
if (params.tokenAddress)
queryParams.append('tokenAddress', params.tokenAddress);
if (params.chainId)
queryParams.append('chainId', params.chainId.toString());
if (params.toTimestampMs)
queryParams.append('toTimestampMs', params.toTimestampMs);
if (params.fromTimestampMs)
queryParams.append('fromTimestampMs', params.fromTimestampMs);
if (queryParams.toString()) {
url += `?${queryParams.toString()}`;
}
return await fetcher.get(url);
}
async function postHistoryEvents(params) {
const apiKey = process.env.ONEINCH_API_KEY;
if (!apiKey) {
throw new Error("1inch API key is required. Set ONEINCH_API_KEY environment variable.");
}
const fetcher = new fetcher_1.OneInchFetcher(apiKey);
const url = `/history/v2.0/history/${params.address}/events`;
const body = {
filter: params.filter
};
return await fetcher.post(url, body);
}
async function searchHistoryEvents(params) {
const apiKey = process.env.ONEINCH_API_KEY;
if (!apiKey) {
throw new Error("1inch API key is required. Set ONEINCH_API_KEY environment variable.");
}
const fetcher = new fetcher_1.OneInchFetcher(apiKey);
const url = `/history/v2.0/history/${params.address}/search/events`;
const body = {
filter: params.filter
};
return await fetcher.post(url, body);
}
async function getSwapEvents(params) {
const apiKey = process.env.ONEINCH_API_KEY;
if (!apiKey) {
throw new Error("1inch API key is required. Set ONEINCH_API_KEY environment variable.");
}
const fetcher = new fetcher_1.OneInchFetcher(apiKey);
const url = `/history/v2.0/history/${params.address}/events/swaps`;
const body = {
filter: params.filter
};
return await fetcher.post(url, body);
}
// Main function that handles all endpoints
async function historyAPI(params) {
switch (params.endpoint) {
case "get-events":
return await getHistoryEvents({
address: params.address,
limit: params.limit,
tokenAddress: params.tokenAddress,
chainId: params.chainId,
toTimestampMs: params.toTimestampMs,
fromTimestampMs: params.fromTimestampMs
});
case "post-events":
if (!params.filter) {
throw new Error("filter is required for post-events endpoint");
}
return await postHistoryEvents({
address: params.address,
filter: params.filter
});
case "search-events":
if (!params.searchFilter) {
throw new Error("searchFilter is required for search-events endpoint");
}
return await searchHistoryEvents({
address: params.address,
filter: params.searchFilter
});
case "swap-events":
if (!params.swapFilter) {
throw new Error("swapFilter is required for swap-events endpoint");
}
return await getSwapEvents({
address: params.address,
filter: params.swapFilter
});
default:
throw new Error(`Unknown endpoint: ${params.endpoint}`);
}
}
//# sourceMappingURL=index.js.map