tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
83 lines (82 loc) • 3.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeWithdrawLoanToken = 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 previously supplied loanToken from a Morpho Blue market.
*
* This function:
* 1. Validates that the user-specified amount (`assets`) is a positive number.
* 2. Fetches the Morpho Blue market configuration using the provided `marketId`.
* 3. Determines the token decimals and converts the asset amount to atomic units.
* 4. Encodes the `withdraw` function call for the Morpho Blue contract.
* 5. Sends the transaction to withdraw the specified amount and waits for confirmation.
*
* Used when a user wants to retrieve supplied loanToken funds from a lending position.
*
* @param walletProvider - Instance of {@link EvmWalletProvider} connected to the user's wallet.
* @param args - Object validated by {@link WithdrawSchema}, including:
* @property {string} assets - Amount of loanToken to withdraw (human-readable format).
* @property {string} marketId - The bytes32 market identifier to withdraw from.
*
* @returns A Promise resolving to an object containing:
* @property {string} loanToken - ERC20 address of the token being withdrawn.
* @property {string} txHash - Transaction hash of the withdraw operation.
* @property {object} receipt - The receipt of the completed transaction.
*
* @throws Will throw if:
* - The input `assets` amount is zero or invalid.
* - The market configuration is missing or malformed.
* - The transaction fails to send or confirm.
*/
const writeWithdrawLoanToken = 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 loanToken = marketResponse.loanToken;
const decimals = await walletProvider.readContract({
address: loanToken,
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: "withdraw",
args: [
marketResponse,
atomicAssets,
BigInt(0),
walletProvider.getAddress(),
walletProvider.getAddress(),
],
});
const txHash = await walletProvider.sendTransaction({
to: marketResponse.morphoBlueContractAddress,
data,
});
const receipt = await walletProvider.waitForTransactionReceipt(txHash);
return { loanToken, txHash, receipt };
}
catch (error) {
throw (0, errors_1.handleError)("Error withdrawing from Morpho Vault", error);
}
};
exports.writeWithdrawLoanToken = writeWithdrawLoanToken;