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.

86 lines (85 loc) 3.33 kB
import { TransferTransaction, Hbar } from "@hashgraph/sdk"; import { HederaMirrorNode } from "@hashgraphonline/standards-sdk"; const MIN_REQUIRED_USD = 2; const MIN_REQUIRED_HBAR_USD = 10; async function ensureAgentHasEnoughHbar(logger, baseClient, accountId, agentName) { try { const account = await baseClient.requestAccount(accountId); const balance = account.balance.balance; const hbarBalance = balance / 1e8; logger.info(`${agentName} account ${accountId} has ${hbarBalance} HBAR`); try { const mirrorNode = new HederaMirrorNode("testnet", logger); const hbarPrice = await mirrorNode.getHBARPrice(/* @__PURE__ */ new Date()); if (hbarPrice) { const balanceInUsd = hbarBalance * hbarPrice; logger.info(`${agentName} balance in USD: $${balanceInUsd.toFixed(2)}`); if (balanceInUsd < MIN_REQUIRED_USD) { logger.warn( `${agentName} account ${accountId} has less than $${MIN_REQUIRED_USD} (${balanceInUsd.toFixed( 2 )}). Attempting to fund.` ); try { const funder = baseClient.getAccountAndSigner(); const targetHbar = MIN_REQUIRED_HBAR_USD / hbarPrice; const amountToTransferHbar = Math.max(0, targetHbar - hbarBalance); if (amountToTransferHbar > 0) { const transferTx = new TransferTransaction().addHbarTransfer( funder.accountId, Hbar.fromTinybars( Math.round(amountToTransferHbar * -1e8) ) ).addHbarTransfer( accountId, Hbar.fromTinybars( Math.round(amountToTransferHbar * 1e8) ) ); logger.info( `Funding ${agentName} account ${accountId} with ${amountToTransferHbar.toFixed( 2 )} HBAR from ${funder.accountId}` ); const fundTxResponse = await transferTx.execute( baseClient.getClient() ); await fundTxResponse.getReceipt(baseClient.getClient()); logger.info( `Successfully funded ${agentName} account ${accountId}.` ); } else { logger.info( `${agentName} account ${accountId} does not require additional funding.` ); } } catch (fundingError) { logger.error( `Failed to automatically fund ${agentName} account ${accountId}:`, fundingError ); logger.warn( `Please fund the account ${accountId} manually with at least ${(MIN_REQUIRED_HBAR_USD / hbarPrice).toFixed(2)} HBAR.` ); } } } else { logger.warn( "Failed to get HBAR price from Mirror Node. Please ensure the account has enough HBAR." ); } } catch (error) { logger.warn( "Failed to check USD balance. Please ensure the account has enough HBAR." ); } } catch (error) { logger.error(`Failed to check ${agentName} account balance:`, error); } } export { MIN_REQUIRED_HBAR_USD, MIN_REQUIRED_USD, ensureAgentHasEnoughHbar }; //# sourceMappingURL=standards-agent-kit.es26.js.map