UNPKG

@hashgraphonline/standards-agent-kit

Version:

A modular SDK for building on-chain autonomous agents using Hashgraph Online Standards, including HCS-10 for agent discovery and communication.

93 lines (92 loc) 3.15 kB
import { StructuredTool } from "@langchain/core/tools"; import { z } from "zod"; import axios from "axios"; const HEDERA_MIRROR_NODE_API = "https://mainnet.mirrornode.hedera.com/api/v1"; const ExchangeRateResponseSchema = z.object({ current_rate: z.object({ cent_equivalent: z.number(), hbar_equivalent: z.number(), expiration_time: z.number() }), next_rate: z.object({ cent_equivalent: z.number(), hbar_equivalent: z.number(), expiration_time: z.number() }), timestamp: z.string() }); class GetHbarPriceTool extends StructuredTool { constructor() { super(...arguments); this.name = "getHbarPrice"; this.description = "Retrieves the current price of HBAR in USD from the Hedera Mirror Node."; this.schema = z.object({}); } // No input required for this tool /** * DISCLAIMER: THIS TOOL USES THE EXCHANGE RATE ENDPOINT FROM THE MIRROR NODE, AND IT IS NOT GUARANTEED TO BE ACCURATE. * USE AN ORACLE OR OTHER SOURCES FOR PRODUCTION USE WHERE PRICE IS IMPORTANT. * Retrieves the current price of HBAR in USD from the Hedera Mirror Node. * @returns A promise that resolves to a string containing the current HBAR price in USD. */ async _call() { try { const response = await axios.get(`${HEDERA_MIRROR_NODE_API}/network/exchangerate`); const data = response.data; const parsedData = ExchangeRateResponseSchema.safeParse(data); if (!parsedData.success) { console.error("Failed to parse exchange rate response:", parsedData.error); throw new Error("Invalid API response format"); } const { current_rate } = parsedData.data; const priceUsd = current_rate.cent_equivalent / current_rate.hbar_equivalent / 100; return `The current price of HBAR is $${priceUsd.toFixed(6)} USD.`; } catch (error) { console.error("Error fetching HBAR price:", error); let errorMessage = "An unknown error occurred"; if (axios.isAxiosError(error)) { errorMessage = error.message; if (error.response) { errorMessage += ` (Status: ${error.response.status})`; } } else if (error instanceof Error) { errorMessage = error.message; } return `Failed to retrieve HBAR price: ${errorMessage}`; } } } class HbarPricePlugin { constructor() { this.id = "hedera-hbar-price"; this.name = "Hedera HBAR Price Plugin"; this.description = "Provides tools to interact with Hedera network data, specifically HBAR price."; this.version = "1.0.0"; this.author = "Hedera Agent"; this.tools = [new GetHbarPriceTool()]; } /** * Initializes the plugin. Currently no specific initialization needed. */ async initialize() { return Promise.resolve(); } /** * Returns the tools provided by this plugin. * @returns An array containing the GetHbarPriceTool. */ getTools() { return this.tools; } /** * Cleans up resources. Currently no cleanup needed. */ async cleanup() { return Promise.resolve(); } } export { GetHbarPriceTool, HbarPricePlugin }; //# sourceMappingURL=standards-agent-kit.es21.js.map