UNPKG

@coinbase/agentkit

Version:

Coinbase AgentKit core primitives

160 lines (159 loc) 7.13 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.zerionActionProvider = exports.ZerionActionProvider = void 0; const viem_1 = require("viem"); const zod_1 = require("zod"); const actionDecorator_1 = require("../actionDecorator"); const actionProvider_1 = require("../actionProvider"); const constants_1 = require("./constants"); const schemas_1 = require("./schemas"); const utils_1 = require("./utils"); /** * ZerionActionProvider provides actions for zerion operations. * * @description * This provider is designed to provide EVM-based operations. * It supports all EVM-based networks. */ class ZerionActionProvider extends actionProvider_1.ActionProvider { /** * Constructor for the ZerionActionProvider. * * @param config - The configuration options for the ZerionActionProvider. */ constructor(config = {}) { super("zerion", []); const apiKey = config.apiKey || process.env.ZERION_API_KEY; if (!apiKey) { throw new Error("ZERION_API_KEY is not configured."); } const encodedKey = Buffer.from(`${apiKey}:`).toString("base64"); this.apiKey = encodedKey; } /** * Fetches and summarizes a crypto wallet's portfolio in USD. * * @param args - Arguments defined by GetWalletPortfolioSchema * @returns A promise that resolves to a string describing the action result */ async getPortfolioOverview(args) { try { const address = args.walletAddress || ""; if (!(0, viem_1.isAddress)(address)) { return `Invalid wallet address: ${address}`; } const options = { method: "GET", headers: { accept: "application/json", authorization: `Basic ${this.apiKey}`, }, }; const url = `${constants_1.ZERION_V1_BASE_URL}/wallets/${args.walletAddress}/portfolio?filter[positions]=no_filter&currency=usd`; const response = await fetch(url, options); const { data } = await response.json(); return (0, utils_1.formatPortfolioData)(data); } catch (error) { return `Error fetching portfolio overview for wallet ${args.walletAddress}: ${error}`; } } /** * Retrieves and summarizes a wallet's fungible token holdings. * * @param args - Arguments defined by GetWalletPortfolioSchema * @returns A promise that resolves to a string describing the action result */ async getFungiblePositions(args) { try { const address = args.walletAddress || ""; if (!(0, viem_1.isAddress)(address)) { return `Invalid wallet address: ${address}`; } const options = { method: "GET", headers: { accept: "application/json", authorization: `Basic ${this.apiKey}`, }, }; const url = `${constants_1.ZERION_V1_BASE_URL}/wallets/${args.walletAddress}/positions?filter[positions]=no_filter&currency=usd&filter[trash]=only_non_trash&sort=value`; const response = await fetch(url, options); const { data } = await response.json(); return (0, utils_1.formatPositionsData)(data); } catch (error) { return `Error fetching fungible positions for wallet ${args.walletAddress}: ${error}`; } } /** * Checks if this provider supports the given network. * * @param network - The network to check support for * @returns True if the network is supported */ supportsNetwork(network) { // all protocol networks return network.protocolFamily === "evm"; } } exports.ZerionActionProvider = ZerionActionProvider; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_portfolio_overview", description: ` Fetches and summarizes a crypto wallet's portfolio in USD. The tool returns a human-readable overview of the wallet's total value, value distribution across blockchains, position types (e.g., staked, deposited), and 24-hour performance change. Useful for providing quick insights into a wallet's DeFi and cross-chain holdings. Input: - walletAddress: The wallet address to fetch portfolio overview for. Output a structured text summary with: - Total portfolio value in USD - 24h percentage change in value - Breakdown of value by position types (e.g., wallet, deposited, staked, locked, borrowed) - Top 5 chains by value distribution `, schema: schemas_1.GetWalletPortfolioSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [void 0]), __metadata("design:returntype", Promise) ], ZerionActionProvider.prototype, "getPortfolioOverview", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_fungible_positions", description: ` Retrieves and summarizes a wallet's fungible token holdings. For each token, it includes metadata such as symbol, name, holding value, associated protocol (if applicable), and the type of position (e.g., deposit, wallet, reward). The summary also reports the total USD value of all qualifying token positions. Input: - walletAddress: The wallet address to fetch fungible positions for. Output a readable text summary including: - All token positions - For each: token name, symbol, USD value, chain, position type - If applicable: protocol used and type of action (e.g. staked, deposited via protocol) - A final total value in USD across all included positions `, schema: schemas_1.GetWalletPortfolioSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [void 0]), __metadata("design:returntype", Promise) ], ZerionActionProvider.prototype, "getFungiblePositions", null); /** * Factory function to create a new ZerionActionProvider instance. * * @param config - The configuration options for the ZerionActionProvider. * @returns A new ZerionActionProvider instance */ const zerionActionProvider = (config) => new ZerionActionProvider(config); exports.zerionActionProvider = zerionActionProvider;