UNPKG

@coinbase/agentkit

Version:

Coinbase AgentKit core primitives

114 lines (112 loc) 5.11 kB
"use strict"; 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.sushiDataActionProvider = exports.SushiDataActionProvider = void 0; const zod_1 = require("zod"); const wallet_providers_1 = require("../../wallet-providers"); const actionDecorator_1 = require("../actionDecorator"); const actionProvider_1 = require("../actionProvider"); const sushiDataSchemas_1 = require("./sushiDataSchemas"); const evm_1 = require("sushi/evm"); const viem_1 = require("viem"); /** * SushiDataActionProvider is an action provider for Sushi. * * This provider is used for any action that uses the Sushi Data API. */ class SushiDataActionProvider extends actionProvider_1.ActionProvider { /** * Constructor for the SushiDataActionProvider class. */ constructor() { super("sushi-data", []); } /** * Swaps a specified amount of a from token to a to token for the wallet. * * @param walletProvider - The wallet provider to swap the tokens from. * @param args - The input arguments for the action. * @returns A message containing the swap details. */ async findToken(walletProvider, args) { try { const chainId = Number((await walletProvider.getNetwork()).chainId); if (!(0, evm_1.isEvmChainId)(chainId)) { return `Unsupported chainId: ${chainId}`; } const request = await fetch(`${evm_1.SUSHI_DATA_API_HOST}/graphql`, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: `{"query":"query { tokenList(chainId: ${chainId}, first: 10, search: \\"${args.search}\\") { address symbol name decimals } }"}`, }); const response = await request.json(); const schema = zod_1.z.object({ data: zod_1.z.object({ tokenList: zod_1.z.array(zod_1.z.object({ address: zod_1.z.string().refine(val => (0, viem_1.isAddress)(val, { strict: false })), symbol: zod_1.z.string(), name: zod_1.z.string(), decimals: zod_1.z.number(), })), }), }); const result = schema.safeParse(response); if (!result.success) { return `Error parsing response: ${result.error.message}`; } const tokens = result.data.data.tokenList; const chain = (0, evm_1.getEvmChainById)(chainId); let message = `Found ${tokens.length} tokens on ${chain.shortName}:`; tokens.forEach(token => { message += `\n- ${token.symbol} (${token.name}) - ${token.address}`; }); return message; } catch (error) { return `Error finding tokens: ${error}`; } } /** * Custom action providers are supported on all networks * * @param network - The network to checkpointSaver * @returns True if the network is supported, false otherwise */ supportsNetwork(network) { if (network.protocolFamily !== "evm" || !network.chainId) { return false; } return (0, evm_1.isEvmChainId)(Number(network.chainId)); } } exports.SushiDataActionProvider = SushiDataActionProvider; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "find-token", description: `This tool finds tokens (erc20) using the Sushi Data API It takes the following inputs: - Search: Either the token symbol (either full or partial) or token address to search for Important notes: - Only returns the first 10 tokens found - Always use the full token symbol for better results - Always use this tool to verify the token address if you are not sure about the address `, schema: sushiDataSchemas_1.FindTokenSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], SushiDataActionProvider.prototype, "findToken", null); const sushiDataActionProvider = () => new SushiDataActionProvider(); exports.sushiDataActionProvider = sushiDataActionProvider;