UNPKG

1inch-agent-kit

Version:

AI Agent Kit for 1inch - Connect any LLM to 1inch DeFi protocols

221 lines 9.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNativeTokenDetails = getNativeTokenDetails; exports.getTokenDetails = getTokenDetails; exports.getNativeTokenPricesByRange = getNativeTokenPricesByRange; exports.getTokenPricesByRange = getTokenPricesByRange; exports.getNativeTokenPricesByInterval = getNativeTokenPricesByInterval; exports.getTokenPricesByInterval = getTokenPricesByInterval; exports.getNativeTokenPriceChange = getNativeTokenPriceChange; exports.getTokenPriceChange = getTokenPriceChange; exports.getTokenListPriceChange = getTokenListPriceChange; exports.tokenDetailsAPI = tokenDetailsAPI; const fetcher_1 = require("../../utils/fetcher"); // Individual endpoint functions async function getNativeTokenDetails(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 = `/token-details/v1.0/details/${params.chainId}`; if (params.provider) { url += `?provider=${params.provider}`; } return await fetcher.get(url); } async function getTokenDetails(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 = `/token-details/v1.0/details/${params.chainId}/${params.contractAddress}`; if (params.provider) { url += `?provider=${params.provider}`; } return await fetcher.get(url); } async function getNativeTokenPricesByRange(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 = `/token-details/v1.0/charts/range/${params.chainId}?from=${params.from}&to=${params.to}`; if (params.provider) { url += `&provider=${params.provider}`; } if (params.from_time) { url += `&from_time=${params.from_time}`; } return await fetcher.get(url); } async function getTokenPricesByRange(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 = `/token-details/v1.0/charts/range/${params.chainId}/${params.tokenAddress}?from=${params.from}&to=${params.to}`; if (params.provider) { url += `&provider=${params.provider}`; } if (params.from_time) { url += `&from_time=${params.from_time}`; } return await fetcher.get(url); } async function getNativeTokenPricesByInterval(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 = `/token-details/v1.0/charts/interval/${params.chainId}?interval=${params.interval}`; if (params.provider) { url += `&provider=${params.provider}`; } if (params.from_time) { url += `&from_time=${params.from_time}`; } return await fetcher.get(url); } async function getTokenPricesByInterval(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 = `/token-details/v1.0/charts/interval/${params.chainId}/${params.tokenAddress}?interval=${params.interval}`; if (params.provider) { url += `&provider=${params.provider}`; } if (params.from_time) { url += `&from_time=${params.from_time}`; } return await fetcher.get(url); } async function getNativeTokenPriceChange(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 = `/token-details/v1.0/prices/change/${params.chainId}?interval=${params.interval}`; return await fetcher.get(url); } async function getTokenPriceChange(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 = `/token-details/v1.0/prices/change/${params.chainId}/${params.tokenAddress}?interval=${params.interval}`; return await fetcher.get(url); } async function getTokenListPriceChange(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 = `/token-details/v1.0/prices/change/${params.chainId}`; const body = { tokenAddresses: params.tokenAddresses, interval: params.interval }; return await fetcher.post(url, body); } // Main function that handles all endpoints async function tokenDetailsAPI(params) { switch (params.endpoint) { case "native-details": return await getNativeTokenDetails({ chainId: params.chainId, provider: params.provider }); case "token-details": if (!params.contractAddress) { throw new Error("contractAddress is required for token-details endpoint"); } return await getTokenDetails({ chainId: params.chainId, contractAddress: params.contractAddress, provider: params.provider }); case "native-prices-range": if (!params.from || !params.to) { throw new Error("from and to timestamps are required for native-prices-range endpoint"); } return await getNativeTokenPricesByRange({ chainId: params.chainId, from: params.from, to: params.to, provider: params.provider, from_time: params.from_time }); case "token-prices-range": if (!params.tokenAddress || !params.from || !params.to) { throw new Error("tokenAddress, from, and to timestamps are required for token-prices-range endpoint"); } return await getTokenPricesByRange({ chainId: params.chainId, tokenAddress: params.tokenAddress, from: params.from, to: params.to, provider: params.provider, from_time: params.from_time }); case "native-prices-interval": if (!params.interval) { throw new Error("interval is required for native-prices-interval endpoint"); } return await getNativeTokenPricesByInterval({ chainId: params.chainId, interval: params.interval, provider: params.provider, from_time: params.from_time }); case "token-prices-interval": if (!params.tokenAddress || !params.interval) { throw new Error("tokenAddress and interval are required for token-prices-interval endpoint"); } return await getTokenPricesByInterval({ chainId: params.chainId, tokenAddress: params.tokenAddress, interval: params.interval, provider: params.provider, from_time: params.from_time }); case "native-price-change": if (!params.interval) { throw new Error("interval is required for native-price-change endpoint"); } return await getNativeTokenPriceChange({ chainId: params.chainId, interval: params.interval }); case "token-price-change": if (!params.tokenAddress || !params.interval) { throw new Error("tokenAddress and interval are required for token-price-change endpoint"); } return await getTokenPriceChange({ chainId: params.chainId, tokenAddress: params.tokenAddress, interval: params.interval }); case "token-list-price-change": if (!params.tokenAddresses || !params.interval) { throw new Error("tokenAddresses and interval are required for token-list-price-change endpoint"); } return await getTokenListPriceChange({ chainId: params.chainId, tokenAddresses: params.tokenAddresses, interval: params.interval }); default: throw new Error(`Unknown endpoint: ${params.endpoint}`); } } //# sourceMappingURL=index.js.map