UNPKG

@iqai/mcp-fraxlend

Version:
120 lines • 4.62 kB
import dedent from "dedent"; import { graphql } from "gql.tada"; import { formatWeiToNumber } from "../lib/format-number.js"; import { client } from "../lib/graphql.js"; const AGENT_POSITIONS_QUERY = graphql(` query fraxlendUsers($user: User_filter) { users(first: 1000, where: $user) { id positions { borrowedAssetShare lentAssetShare depositedCollateralAmount pair { symbol asset { symbol address decimals } collateral { symbol address decimals } } dailyHistory(first: 1, orderBy: timestamp, orderDirection: desc) { lentAssetValue lendProfitTaken borrowedAssetValue depositedCollateralValue timestamp } } } } `); export class AgentPositionsService { walletService; constructor(walletService) { this.walletService = walletService; } async getPositions(address) { const walletClient = this.walletService.getWalletClient(); if (!walletClient) { throw new Error("Wallet client not initialized"); } const userAddress = address || walletClient.account?.address; if (!userAddress) { throw new Error("User address not found"); } try { // biome-ignore lint/suspicious/noExplicitAny: <explanation> const data = await client.request(AGENT_POSITIONS_QUERY, { user: { id: userAddress.toLowerCase(), }, }); if (!data.users || data.users.length === 0 || !data.users[0].positions || data.users[0].positions.length === 0) { return []; } const positions = data.users[0].positions; return ( // biome-ignore lint/suspicious/noExplicitAny: <explanation> positions.map((position) => ({ symbol: position.pair.symbol, assetSymbol: position.pair.asset.symbol, collateralSymbol: position.pair.collateral.symbol, assetAddress: position.pair.asset.address, collateralAddress: position.pair.collateral.address, lentAmount: position.lentAssetShare, borrowedAmount: position.borrowedAssetShare, collateralAmount: position.depositedCollateralAmount, value: position.dailyHistory[0]?.lentAssetValue || "0", borrowValue: position.dailyHistory[0]?.borrowedAssetValue || "0", collateralValue: position.dailyHistory[0]?.depositedCollateralValue || "0", profit: position.dailyHistory[0]?.lendProfitTaken || "0", })) || []); } catch (error) { throw new Error(`Failed to fetch agent positions: ${error.message}`); } } formatPositions(positions) { if (positions.length === 0) { return "šŸ“Š No Active Positions Found"; } const formattedPositions = positions // biome-ignore lint/suspicious/noExplicitAny: <explanation> .map((pos) => { const assetSymbol = pos.assetSymbol; const collateralSymbol = pos.collateralSymbol; const assetAddress = pos.assetAddress; const collateralAddress = pos.collateralAddress; const lentAmount = formatWeiToNumber(pos.lentAmount); const lentValue = formatWeiToNumber(pos.value); const borrowedAmount = formatWeiToNumber(pos.borrowedAmount); const borrowValue = formatWeiToNumber(pos.borrowValue); const collateralAmount = formatWeiToNumber(pos.collateralAmount); const collateralValue = formatWeiToNumber(pos.collateralValue); const profit = formatWeiToNumber(pos.profit); return dedent ` šŸ”¹ ${pos.symbol} - Lent: ${lentAmount} ${assetSymbol} (Value: $${lentValue}) - Borrowed: ${borrowedAmount} ${assetSymbol} (Value: $${borrowValue}) - Collateral: ${collateralAmount} ${collateralSymbol} (Value: $${collateralValue}) - Profit: $${profit} - Asset symbol: ${assetSymbol} - Collateral symbol: ${collateralSymbol} - Asset address: ${assetAddress} - Collateral address: ${collateralAddress} `; }) .join("\n\n"); return `šŸ“Š *Your Active Positions*\n\n${formattedPositions}`; } } //# sourceMappingURL=agent-positions.js.map