UNPKG

@iqai/mcp-fraxlend

Version:
75 lines 2.98 kB
import { erc20Abi } from "viem"; import { FRAXLEND_ABI } from "../lib/fraxlend.abi.js"; export class AddCollateralService { walletService; constructor(walletService) { this.walletService = walletService; } async execute({ pairAddress, amount, }) { const publicClient = this.walletService.getPublicClient(); const walletClient = this.walletService.getWalletClient(); if (!walletClient) { throw new Error("Wallet client not initialized"); } const userAddress = walletClient.account?.address; if (!userAddress) { throw new Error("User address not found"); } const collateralAddress = await publicClient.readContract({ address: pairAddress, abi: FRAXLEND_ABI, functionName: "collateralContract", }); const balance = await publicClient.readContract({ address: collateralAddress, abi: erc20Abi, functionName: "balanceOf", args: [userAddress], }); if (balance < amount) { throw new Error(`Insufficient collateral balance. Available: ${balance}, Requested: ${amount}`); } await this.ensureTokenApproval(collateralAddress, pairAddress, amount); const { request } = await publicClient.simulateContract({ address: pairAddress, abi: FRAXLEND_ABI, functionName: "addCollateral", args: [amount, userAddress], account: walletClient.account, }); const hash = await walletClient.writeContract(request); const receipt = await publicClient.waitForTransactionReceipt({ hash }); return { txHash: receipt.transactionHash, amount, }; } async ensureTokenApproval(collateralAddress, spenderAddress, amount) { const publicClient = this.walletService.getPublicClient(); const walletClient = this.walletService.getWalletClient(); if (!walletClient) { throw new Error("Wallet client not initialized"); } const userAddress = walletClient.account?.address; if (!userAddress) { throw new Error("User address not found"); } const currentAllowance = await publicClient.readContract({ address: collateralAddress, abi: erc20Abi, functionName: "allowance", args: [userAddress, spenderAddress], }); if (currentAllowance < amount) { const { request: approveRequest } = await publicClient.simulateContract({ address: collateralAddress, abi: erc20Abi, functionName: "approve", args: [spenderAddress, amount], account: walletClient.account, }); await walletClient.writeContract(approveRequest); } } } //# sourceMappingURL=add-collateral.js.map