UNPKG

tensaikit

Version:

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

94 lines (93 loc) 4.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeSupplyCollateralToken = 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 morphoBlueABI_1 = require("../abi/morphoBlueABI"); const viem_1 = require("viem"); const utils_1 = require("../../../utils"); /** * Supplies a specified amount of collateralToken into a Morpho Blue market using the connected wallet. * * This function: * 1. Validates that the input asset amount is greater than zero. * 2. Fetches the market configuration using the provided marketId. * 3. Reads the collateralToken's decimals and parses the input amount to atomic units. * 4. Checks and ensures the token allowance; calls approve if necessary. * 5. Prepares and encodes the `supplyCollateral` function call to Morpho Blue. * 6. Sends the transaction and waits for it to be mined. * * This is typically used to deposit collateral before borrowing from a Morpho Blue market. * * @param walletProvider - Instance of {@link EvmWalletProvider} connected to the user's wallet. * @param args - Object validated by {@link SupplyCollateralSchema}, including: * @property {string} assets - The amount of collateralToken to supply (human-readable string). * @property {string} marketId - The bytes32 hex identifier for the target Morpho Blue market. * * @returns A Promise resolving to an object with: * @property {string} collateralToken - The ERC20 address of the collateral token. * @property {string} txHash - The hash of the submitted supplyCollateral transaction. * @property {object} receipt - The transaction receipt after confirmation. * * @throws Will throw if: * - The assets value is invalid or non-positive. * - The marketId is invalid or configuration cannot be fetched. * - The token approval process fails. * - The transaction fails to send or confirm on-chain. */ const writeSupplyCollateralToken = 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 currentAllowance = await (0, utils_1.allowance)(walletProvider, collateralToken, marketResponse.morphoBlueContractAddress); if (currentAllowance < atomicAssets) { const approvalResult = await (0, utils_1.approve)(walletProvider, collateralToken, marketResponse.morphoBlueContractAddress, atomicAssets); if (approvalResult.startsWith("Error")) { throw (0, errors_1.createError)(`Error approving Morpho Vault as spender: ${approvalResult}`, errors_1.ErrorCode.CONTRACT_ERROR); } else { console.log(approvalResult); } } const data = (0, viem_1.encodeFunctionData)({ abi: morphoBlueABI_1.MORPHO_BLUE_ABI, functionName: "supplyCollateral", args: [marketResponse, atomicAssets, walletProvider.getAddress(), "0x"], }); 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)("Error supplying collateral to Morpho Vault", error); } }; exports.writeSupplyCollateralToken = writeSupplyCollateralToken;