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.

130 lines (107 loc) 4.61 kB
import { HCS10Plugin } from '../../../src/plugins/HCS10Plugin'; import { StructuredTool } from '@langchain/core/tools'; import { z } from 'zod'; import axios from 'axios'; import { HCS10Client } from '../../../src/hcs10/HCS10Client'; /** * Tool for getting token price information using CoinGecko */ class GetTokenPriceTool extends StructuredTool { name = 'get_token_price'; description = 'Get the current price of a token on Hedera'; schema = z.object({ tokenId: z.string().describe('The Hedera token ID (e.g., 0.0.12345)'), }); constructor(private client: HCS10Client) { super(); } async _call(input: z.infer<typeof this.schema>): Promise<string> { try { // In a real implementation, this would map Hedera token IDs to CoinGecko IDs // This is a simplified mock implementation const mockCoinGeckoIds: Record<string, string> = { '0.0.1234': 'hbar', '0.0.5678': 'ethereum', '0.0.9012': 'bitcoin', }; const coinGeckoId = mockCoinGeckoIds[input.tokenId] || 'hbar'; // Use CoinGecko's public API (no API key required) const response = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoId}&vs_currencies=usd`); const price = response.data[coinGeckoId]?.usd || 0; return `Current price of token ${input.tokenId}: $${price.toFixed(4)} USD`; } catch (error) { return `Error fetching token price: ${error instanceof Error ? error.message : String(error)}`; } } } /** * Tool for swapping tokens */ class SwapTokensTool extends StructuredTool { name = 'swap_tokens'; description = 'Swap one token for another on Hedera'; schema = z.object({ fromTokenId: z.string().describe('The ID of the token to swap from (e.g., 0.0.12345)'), toTokenId: z.string().describe('The ID of the token to swap to (e.g., 0.0.67890)'), amount: z.number().positive().describe('The amount of the source token to swap'), }); constructor(private client: HCS10Client) { super(); } async _call(input: z.infer<typeof this.schema>): Promise<string> { try { // In a real implementation, this would interact with a DEX contract // This is a simplified mock implementation const { accountId } = this.client.getAccountAndSigner(); // Mock exchange rate const exchangeRate = Math.random() * 2; const receivedAmount = input.amount * exchangeRate; return `Simulated swap of ${input.amount} tokens (${input.fromTokenId}) for ${receivedAmount.toFixed(4)} tokens (${input.toTokenId}).\n\nNote: This is a mock implementation. In a real implementation, this would execute the swap through a DEX on Hedera.`; } catch (error) { return `Error performing token swap: ${error instanceof Error ? error.message : String(error)}`; } } } /** * Tool for checking token balance */ class CheckTokenBalanceTool extends StructuredTool { name = 'check_token_balance'; description = 'Check the balance of a token for an account on Hedera'; schema = z.object({ tokenId: z.string().describe('The Hedera token ID (e.g., 0.0.12345)'), accountId: z.string().optional().describe('The account ID to check (defaults to the operator account)'), }); constructor(private client: HCS10Client) { super(); } async _call(input: z.infer<typeof this.schema>): Promise<string> { try { const { accountId: operatorId } = this.client.getAccountAndSigner(); const accountToCheck = input.accountId || operatorId; // In a real implementation, this would query the account's token balance // This is a simplified mock implementation const mockBalance = Math.floor(Math.random() * 10000); return `Token balance for account ${accountToCheck}:\n${mockBalance} tokens of ${input.tokenId}\n\nNote: This is a mock implementation. In a real implementation, this would query the actual token balance from the Hedera network.`; } catch (error) { return `Error checking token balance: ${error instanceof Error ? error.message : String(error)}`; } } } /** * DeFi Integration Plugin for the Standards Agent Kit */ export default class DeFiPlugin extends HCS10Plugin { id = 'defi-integration'; name = 'DeFi Integration Plugin'; description = 'Provides tools to interact with DeFi protocols on Hedera'; version = '1.0.0'; author = 'Hashgraph Online'; getTools(): StructuredTool[] { return [ new GetTokenPriceTool(this.context.client), new SwapTokensTool(this.context.client), new CheckTokenBalanceTool(this.context.client) ]; } }