UNPKG

tensaikit

Version:

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

84 lines (83 loc) 3.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeBorrowLoan = 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"); /** * Borrows a specified amount of loanToken from a Morpho Blue market using the connected wallet. * * This function: * 1. Validates the input amount is greater than zero. * 2. Fetches the market configuration for the provided `marketId`. * 3. Reads the `decimals` of the loan token to convert user input to atomic units. * 4. Prepares and encodes the `borrow` transaction for the Morpho Blue contract. * 5. Sends the transaction and waits for confirmation. * * The borrow action transfers the specified loanToken amount from the Morpho market to the caller's wallet. * * @param walletProvider - Instance of {@link EvmWalletProvider} connected to the user's wallet. * @param args - Object validated by {@link BorrowSchema}, containing: * @property {string} assets - Amount of loanToken to borrow (as a human-readable string). * @property {string} marketId - The bytes32 hex string identifier for the target market. * * @returns A Promise resolving to an object containing: * @property {string} loanToken - The token being borrowed. * @property {string} txHash - The transaction hash of the borrow operation. * @property {object} receipt - The transaction receipt returned by the network. * * @throws Will throw if: * - The input amount is invalid or zero. * - Market configuration is missing or invalid. * - Decimals lookup or transaction preparation fails. * - Transaction fails to send or confirm. */ const writeBorrowLoan = 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: "borrow", 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 borrowing from Morpho Vault", error); } }; exports.writeBorrowLoan = writeBorrowLoan;