UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

125 lines 6.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAaveReserveData = exports.getAaveUserData = void 0; const client_js_1 = require("../client.js"); const zod_1 = require("zod"); const constants_js_1 = require("./constants.js"); const viem_1 = require("viem"); const formatRate = (rate) => `${(Number((0, viem_1.formatUnits)(rate, 27)) * 100).toFixed(2)}%`; const isZeroAddress = (address) => address === "0x0000000000000000000000000000000000000000"; exports.getAaveUserData = (0, client_js_1.createTool)({ name: "getAaveUserData", description: "Fetches Aave user data including total collateral, total debt, available borrowing power, current liquidation threshold, LTV, and health factor.", parameters: zod_1.z.object({ userAddress: zod_1.z.string().describe("The user's wallet address."), chainId: zod_1.z.number().describe("The chain ID where Aave is deployed."), }), supportedChains: constants_js_1.supportedChains, async execute(client, args) { const publicClient = client.getPublicClient(args.chainId); const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId); const result = (await publicClient.readContract({ address: poolAddress, abi: constants_js_1.aavePoolAbi, functionName: "getUserAccountData", args: [args.userAddress], })); const [totalCollateralBase, totalDebtBase, availableBorrowsBase, currentLiquidationThreshold, ltv, healthFactor,] = result; return { summary: { totalCollateralUSD: `$${Number((0, viem_1.formatUnits)(totalCollateralBase, 8)).toFixed(2)}`, totalDebtUSD: `$${Number((0, viem_1.formatUnits)(totalDebtBase, 8)).toFixed(2)}`, availableToBorrowUSD: `$${Number((0, viem_1.formatUnits)(availableBorrowsBase, 8)).toFixed(2)}`, loanToValue: `${Number((0, viem_1.formatUnits)(ltv, 2)).toFixed(2)}%`, liquidationThreshold: `${Number((0, viem_1.formatUnits)(currentLiquidationThreshold, 2)).toFixed(2)}%`, healthFactor: healthFactor >= BigInt(1e9) ? "∞" : Number((0, viem_1.formatUnits)(healthFactor, 18)).toFixed(2), }, riskAssessment: { status: healthFactor > BigInt(1e18) ? "HEALTHY" : healthFactor > BigInt(1e18) ? "SAFE" : "AT RISK", canBorrow: availableBorrowsBase > BigInt(0) ? "YES" : "NO", liquidationRisk: healthFactor < BigInt(1.1e18) ? "HIGH" : healthFactor < BigInt(2e18) ? "MEDIUM" : "LOW", }, rawData: { totalCollateralBase: totalCollateralBase.toString(), totalDebtBase: totalDebtBase.toString(), availableBorrowsBase: availableBorrowsBase.toString(), currentLiquidationThreshold: currentLiquidationThreshold.toString(), ltv: ltv.toString(), healthFactor: healthFactor.toString(), }, }; }, }); exports.getAaveReserveData = (0, client_js_1.createTool)({ name: "getAaveReserveData", description: "Fetches reserve data for a given asset from Aave including available liquidity, total stable and variable debt, and interest rates.", parameters: zod_1.z.object({ asset: zod_1.z.string().describe("The token contract address of the asset."), chainId: zod_1.z.number().describe("Chain ID where Aave is deployed."), }), supportedChains: constants_js_1.supportedChains, async execute(client, args) { const publicClient = client.getPublicClient(args.chainId); const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId); const result = (await publicClient.readContract({ address: poolAddress, abi: constants_js_1.aavePoolAbi, functionName: "getReserveData", args: [args.asset], })); return { summary: { assetStatus: Number(result.configuration.data) > 0 ? "ACTIVE" : "INACTIVE", supplyAPY: formatRate(result.currentLiquidityRate), variableBorrowAPY: formatRate(result.currentVariableBorrowRate), stableBorrowAPY: formatRate(result.currentStableBorrowRate), lastUpdate: new Date(result.lastUpdateTimestamp * 1000).toISOString(), }, tokens: { aToken: !isZeroAddress(result.aTokenAddress) ? result.aTokenAddress : "Not Available", stableDebtToken: !isZeroAddress(result.stableDebtTokenAddress) ? result.stableDebtTokenAddress : "Not Available", variableDebtToken: !isZeroAddress(result.variableDebtTokenAddress) ? result.variableDebtTokenAddress : "Not Available", }, metrics: { liquidityIndex: Number((0, viem_1.formatUnits)(result.liquidityIndex, 27)).toFixed(8), accruedToTreasury: Number((0, viem_1.formatUnits)(result.accruedToTreasury, 27)).toFixed(8), unbacked: Number((0, viem_1.formatUnits)(result.unbacked, 27)).toFixed(8), isolationModeTotalDebt: Number((0, viem_1.formatUnits)(result.isolationModeTotalDebt, 27)).toFixed(8), }, rawData: { configuration: result.configuration.data.toString(), liquidityIndex: result.liquidityIndex.toString(), currentLiquidityRate: result.currentLiquidityRate.toString(), variableBorrowIndex: result.variableBorrowIndex.toString(), currentVariableBorrowRate: result.currentVariableBorrowRate.toString(), currentStableBorrowRate: result.currentStableBorrowRate.toString(), lastUpdateTimestamp: result.lastUpdateTimestamp, id: result.id, aTokenAddress: result.aTokenAddress, stableDebtTokenAddress: result.stableDebtTokenAddress, variableDebtTokenAddress: result.variableDebtTokenAddress, interestRateStrategyAddress: result.interestRateStrategyAddress, accruedToTreasury: result.accruedToTreasury.toString(), unbacked: result.unbacked.toString(), isolationModeTotalDebt: result.isolationModeTotalDebt.toString(), }, }; }, }); //# sourceMappingURL=tools.js.map