UNPKG

tensaikit

Version:

An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.

82 lines (81 loc) 3.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeWithdrawCollateralToken = void 0; const constants_1 = require("../../erc20/constants"); const errors_1 = require("../../../common/errors"); const decimal_js_1 = __importDefault(require("decimal.js")); const fetchMarketConfigFromContract_1 = require("./fetchMarketConfigFromContract"); const viem_1 = require("viem"); const morphoBlueABI_1 = require("../abi/morphoBlueABI"); /** * Withdraws a specified amount of collateralToken from a Morpho Blue market for the connected wallet. * * This function: * 1. Validates that the requested `assets` amount is greater than zero. * 2. Fetches market configuration using the provided `marketId`. * 3. Determines the decimals of the collateral token and converts the user-readable amount to atomic units. * 4. Encodes the `withdrawCollateral` function call to the Morpho Blue contract. * 5. Sends the transaction to withdraw collateral and waits for its confirmation. * * This is typically used to reclaim previously deposited collateral, if conditions allow (e.g. health factor is safe). * * @param walletProvider - Instance of {@link EvmWalletProvider} connected to the user's wallet. * @param args - Object matching {@link WithdrawCollateralSchema}, including: * @property {string} assets - The amount of collateralToken to withdraw (human-readable string). * @property {string} marketId - The bytes32 hex identifier of the market to withdraw from. * * @returns A Promise resolving to an object containing: * @property {string} collateralToken - The ERC20 token address of the withdrawn collateral. * @property {string} txHash - The transaction hash of the withdrawal. * @property {object} receipt - The transaction receipt after confirmation. * * @throws Will throw if: * - The input amount is invalid or zero. * - The market configuration cannot be fetched. * - The transaction fails to send or confirm. */ const writeWithdrawCollateralToken = async (walletProvider, args) => { try { const assets = new decimal_js_1.default(args.assets); if (assets.lessThanOrEqualTo(0)) { throw (0, errors_1.createError)("Error: Assets amount must be greater than 0", errors_1.ErrorCode.INVALID_INPUT); } const marketResponse = await (0, fetchMarketConfigFromContract_1.fetchMarketConfigFromContract)(walletProvider, { marketId: args.marketId, }); if (!marketResponse) { throw (0, errors_1.createError)("Invalid market id or missing market information", errors_1.ErrorCode.INVALID_INPUT); } const collateralToken = marketResponse.collateralToken; const decimals = await walletProvider.readContract({ address: collateralToken, abi: constants_1.abi, functionName: "decimals", args: [], }); const atomicAssets = (0, viem_1.parseUnits)(args.assets, decimals); const data = (0, viem_1.encodeFunctionData)({ abi: morphoBlueABI_1.MORPHO_BLUE_ABI, functionName: "withdrawCollateral", args: [ marketResponse, atomicAssets, walletProvider.getAddress(), walletProvider.getAddress(), ], }); const txHash = await walletProvider.sendTransaction({ to: marketResponse.morphoBlueContractAddress, data, }); const receipt = await walletProvider.waitForTransactionReceipt(txHash); return { collateralToken, txHash, receipt }; } catch (error) { throw (0, errors_1.handleError)("Failed to withdraw collateral token from Morpho Vault", error); } }; exports.writeWithdrawCollateralToken = writeWithdrawCollateralToken;