tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
132 lines (127 loc) • 6.15 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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SushiSwapTokenActions = void 0;
const zod_1 = __importDefault(require("zod"));
const actionProvider_1 = require("../../actionProvider");
const actionDecorator_1 = require("../../actionDecorator");
const walletProviders_1 = require("../../../walletProviders");
const errors_1 = require("../../../common/errors");
const schemas_1 = require("../schemas");
const utils_1 = require("../../../common/utils");
const logic_1 = require("../logic");
/**
* SushiSwapTokenActions provides utilities for retrieving token metadata and listings from SushiSwap.
*
* This action group focuses on token-related operations, including:
* - Fetching metadata (name, symbol, decimals) for a specific token via SushiSwap’s pricing API
* - Querying a paginated list of all tokens available on SushiSwap using The Graph’s subgraph
*
* Responsibilities:
* - Use on-chain or indexed APIs to fetch metadata for ERC-20 tokens on supported EVM chains
* - Support pagination and API key–secured access to the SushiSwap subgraph
*
* Registered under the action provider key: `sushiSwap.token`
*
* Note:
* - Requires a valid `subGraphApiKey` for subgraph queries.
* - Only EVM-compatible chains are supported.
*/
class SushiSwapTokenActions extends actionProvider_1.ActionProvider {
constructor(subGraphApiKey) {
super("sushi_swap.token", []);
this.supportsNetwork = (network) => network.protocolFamily === "evm";
this.subGraphApiKey = subGraphApiKey;
}
/**
* Fetches metadata for a token (e.g., decimals, symbol, name) from SushiSwap's public API.
*
* @param walletProvider - Instance of EVM-compatible WalletProvider
* @param args - Includes `tokenAddress` of the token to fetch metadata for
* @returns A JSON string containing token metadata
* @throws If the API call fails or the token is unsupported
*/
async getTokenDetails(walletProvider, args) {
try {
const { tokenAddress } = args;
const chainId = walletProvider.getNetwork().chainId;
if (!chainId) {
throw (0, errors_1.createError)("Invalid or missing chain ID", errors_1.ErrorCode.INVALID_NETWORK);
}
const response = await (0, logic_1.fetchTokenMetadata)(chainId, tokenAddress);
return (0, utils_1.wrapAndStringify)("sushi_swap.token.get_token_details", response);
}
catch (error) {
throw (0, errors_1.handleError)("Failed to retrieve token details", error);
}
}
/**
* Fetches a paginated list of all tokens available on SushiSwap from the subgraph for the connected EVM chain.
*
* @param walletProvider - Instance of EVM-compatible WalletProvider.
* @param args - Object containing `skip` and `first` for pagination control.
* @returns A JSON string representing the array of tokens including id, name, symbol, poolCount, and volume.
* @throws If the connected network's chainId is missing or the subgraph query fails.
*
* Notes:
* - Uses The Graph's SushiSwap subgraph endpoint.
* - Requires a valid `subGraphApiKey` to authenticate the query request.
* - Supports pagination using `skip` (default: 0) and `first` (default: 50).
*/
async getAllSushiTokens(walletProvider, args) {
try {
const response = await (0, logic_1.fetchAllTokensFromSubgraph)(walletProvider, args);
return (0, utils_1.wrapAndStringify)("sushi_swap.token.get_all_sushi_tokens", response);
}
catch (error) {
throw (0, errors_1.handleError)("Failed to fetch all tokens", error);
}
}
}
exports.SushiSwapTokenActions = SushiSwapTokenActions;
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "get_token_details",
description: `
Retrieves metadata for a specific token on a chain.
Inputs:
- chainId: Chain ID of the network.
- tokenAddress: Contract address of the token.
Notes:
- Ensure the token address is valid and supported by SushiSwap.
`,
schema: schemas_1.GetTokenDetailsSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], SushiSwapTokenActions.prototype, "getTokenDetails", null);
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "get_all_sushi_tokens",
description: `
Fetches a paginated list of all tokens available on SushiSwap for the connected chain using the subgraph.
Inputs:
- skip: Number of tokens to skip (useful for pagination)
- first: Number of tokens to fetch (e.g., 50)
Note:
- This uses The Graph's SushiSwap subgraph endpoint.
- Make sure a valid subGraphApiKey is configured for authenticated access.
`,
schema: schemas_1.QueryGetSushiAllTokens,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], SushiSwapTokenActions.prototype, "getAllSushiTokens", null);