tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
114 lines (111 loc) • 4.75 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.alchemyTokenPricesActionProvider = exports.AlchemyTokenPricesActionProvider = void 0;
const zod_1 = require("zod");
const actionProvider_1 = require("../actionProvider");
const actionDecorator_1 = require("../actionDecorator");
const schemas_1 = require("./schemas");
const utils_1 = require("../../common/utils");
const errors_1 = require("../../common/errors");
/**
* AlchemyTokenPricesActionProvider enables fetching real-time token price data
* using the Alchemy Prices API, supporting lookups by token symbol or address.
*/
class AlchemyTokenPricesActionProvider extends actionProvider_1.ActionProvider {
/**
* Creates a new AlchemyTokenPricesActionProvider instance.
*
* @param config - Configuration including the Alchemy API key. Falls back to environment variable.
* @throws If no valid API key is provided via config or environment.
*/
constructor(config = {}) {
super("alchemy_token_prices", []);
this.supportsNetwork = () => {
return true;
};
config.apiKey || (config.apiKey = process.env.ALCHEMY_API_KEY || "");
if (!config.apiKey) {
throw new Error("ALCHEMY_API_KEY is not configured.");
}
this.apiKey = config.apiKey;
this.baseUrl = "https://api.g.alchemy.com/prices/v1";
}
/**
* Fetches current prices for one or more tokens based on their symbols.
*
* @remarks
* This uses Alchemy’s `tokens/by-symbol` GET endpoint and allows multiple symbols per request.
*
* @param args - Object containing an array of token symbols (e.g., ["ETH", "USDC"]).
* @returns A formatted JSON string with price information or error details.
*/
async tokenPricesBySymbol(args) {
try {
const params = new URLSearchParams();
for (const symbol of args.symbols) {
params.append("symbols", symbol);
}
const url = `${this.baseUrl}/${this.apiKey}/tokens/by-symbol?${params.toString()}`;
const response = await fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return (0, utils_1.wrapAndStringify)("alchemy_token_prices.token_prices_by_symbol", data);
}
catch (error) {
throw (0, errors_1.handleError)("Error fetching token prices by symbol", error);
}
}
}
exports.AlchemyTokenPricesActionProvider = AlchemyTokenPricesActionProvider;
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "token_prices_by_symbol",
description: `
This tool will fetch current prices for one or more tokens using their symbols via the Alchemy Prices API.
A successful response will return a JSON payload similar to:
{
"data": [
{
"symbol": "ETH",
"prices": [
{
"currency": "usd",
"value": "2873.490923459",
"lastUpdatedAt": "2025-02-03T23:46:40Z"
}
]
}
]
}
A failure response will return an error message with details.
`,
schema: schemas_1.AlchemyTokenPricesBySymbolSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [void 0]),
__metadata("design:returntype", Promise)
], AlchemyTokenPricesActionProvider.prototype, "tokenPricesBySymbol", null);
/**
* Factory function to instantiate AlchemyTokenPricesActionProvider.
*
* @param config - Optional configuration object containing the Alchemy API key.
* @returns A new initialized provider instance.
*/
const alchemyTokenPricesActionProvider = (config) => new AlchemyTokenPricesActionProvider(config);
exports.alchemyTokenPricesActionProvider = alchemyTokenPricesActionProvider;