UNPKG

tensaikit

Version:

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

340 lines (325 loc) 16.7 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.morphoWriteActionProvider = exports.MorphoWriteActionProvider = void 0; const zod_1 = require("zod"); const actionDecorator_1 = require("../actionDecorator"); const actionProvider_1 = require("../actionProvider"); const walletProviders_1 = require("../../walletProviders"); const schemas_1 = require("./schemas"); const errors_1 = require("../../common/errors"); const objectToString_1 = require("../../common/utils/objectToString"); const utils_1 = require("./utils"); const utils_2 = require("../../common/utils"); const logic_1 = require("./logic"); /** * MorphoWriteActionProvider is an action provider to write into Morpho Vault. */ class MorphoWriteActionProvider extends actionProvider_1.ActionProvider { /** * Creates a new instance of Morpho Protocol Action Provider * */ constructor() { super("morpho.write_action", []); this.supportsNetwork = (network) => { return (network.protocolFamily === "evm" && utils_1.MORPHO_SUPPORTED_PROTOCOL.includes(network.networkId)); }; } /** * Supplies loanToken assets to a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of loanToken to be supplied, provided as a decimal string (e.g., "1.0", "0.5"). * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The approve or transaction functions fail. */ async supplyLoanAsset(walletProvider, args) { try { const response = await (0, logic_1.writeSupplyLoanToken)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.supply_loan_asset", `Deposited ${args.assets} tokens (loanToken: ${response.loanToken}) to Morpho Market Id ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Error supplying loan token to Morpho Vault", error); } } /** * Withdraws loanToken assets from a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of loanToken to be withdrawn, provided as a decimal string. * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The transaction fails. */ async withdrawLoanAsset(walletProvider, args // Change to WithdrawSchema if separate ) { try { const response = await (0, logic_1.writeWithdrawLoanToken)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.withdraw_loan_asset", `Withdrew ${args.assets} tokens (loanToken: ${response.loanToken}) from Morpho Market ID ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Error withdrawing from Morpho Vault", error); } } /** * Supplies collateralToken assets to a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of collateralToken to be supplied, provided as a decimal string. * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The approve or transaction functions fail. */ async supplyCollateralLoanAsset(walletProvider, args) { try { const response = await (0, logic_1.writeSupplyCollateralToken)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.supply_collateral_loan_asset", `Supplied collateral ${args.assets} tokens (loanToken: ${response.collateralToken}) to Morpho Market Id ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Error supplying collateral to Morpho Vault", error); } } /** * Withdraws collateralToken assets from a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of collateralToken to be withdrawn, provided as a decimal string. * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The transaction fails. */ async withdrawCollateralLoanAsset(walletProvider, args // Change to WithdrawSchema if separate ) { try { const response = await (0, logic_1.writeWithdrawCollateralToken)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.withdraw_collateral_loan_asset", `Withdrew collateral ${args.assets} tokens (collateralToken: ${response.collateralToken}) from Morpho Market ID ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Failed to withdraw collateral token from Morpho Vault", error); } } /** * Borrows loanToken assets from a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of loanToken to be borrowed, provided as a decimal string. * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The transaction fails. */ async borrowLoanAsset(walletProvider, args) { try { const response = await (0, logic_1.writeBorrowLoan)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.borrow_loan_asset", `Borrowed ${args.assets} tokens (loanToken: ${response.loanToken}) from Morpho Market ID ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Error borrowing from Morpho Vault", error); } } /** * Repays loanToken debt in a Morpho Blue market using a specific market ID. * * @param walletProvider - Instance of EvmWalletProvider used to interact with the chain. * @param args - Contains: * - marketId: The unique bytes32 identifier for the Morpho market. * - assets: The amount of loanToken to be repaid, provided as a decimal string. * * @returns A string summarizing the transaction, including the hash and receipt. * * @throws Will throw an error if: * - The asset amount is zero or negative. * - Network information is invalid or missing. * - Market ID is invalid or returns no information. * - The approve or transaction functions fail. */ async repay(walletProvider, args) { try { const response = await (0, logic_1.writeRepayLoan)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.write_action.repay_loan_asset", `Repaid ${args.assets} of tokens (loanToken: ${response.loanToken}) from Morpho Market ID ${args.marketId} with transaction hash: ${response.txHash}\nTransaction receipt: ${(0, objectToString_1.objectToString)(response.receipt)}`); } catch (error) { throw (0, errors_1.handleError)("Error repaying to Morpho Vault", error); } } } exports.MorphoWriteActionProvider = MorphoWriteActionProvider; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "supply_loan_asset", description: ` This action allows supplying loanToken assets to a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of assets to deposit in whole units Examples for WETH: - 1 WETH - 0.1 WETH - 0.01 WETH Make sure the market is active and the asset will be approved by the function. Important notes: - Make sure to use the exact amount provided. Do not convert units for assets for this action. - Please use a market id (example 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5) for the marketId field.`, schema: schemas_1.SupplySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "supplyLoanAsset", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "withdraw_loan_asset", description: ` This action allows withdrawing loanToken assets from a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of assets to withdraw in whole units Examples for USDC (6 decimals): - 1 USDC - 0.5 USDC - 0.01 USDC Make sure the market is active, and the withdrawable balance is available. Important notes: - Do not convert units manually; input the asset amount as a decimal string. - Please use a valid market ID (example: 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5). `, schema: schemas_1.WithdrawSchema, // You may want to rename this to `WithdrawSchema` if it's a different zod schema }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "withdrawLoanAsset", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "supply_collateral_loan_asset", description: ` This action allows supplying collateralToken assets to a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of assets to deposit in whole units Examples for WETH: - 1 WETH - 0.1 WETH - 0.01 WETH Make sure the market is active and the asset will be approved by the function. Important notes: - Make sure to use the exact amount provided. Do not convert units for assets for this action. - Please use a market id (example 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5) for the marketId field.`, schema: schemas_1.SupplyCollateralSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "supplyCollateralLoanAsset", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "withdraw_collateral_loan_asset", description: ` This action allows withdrawing collateralToken assets from a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of assets to withdraw in whole units Examples for USDC (6 decimals): - 1 USDC - 0.5 USDC - 0.01 USDC Make sure the market is active, and the withdrawable balance is available. Important notes: - Do not convert units manually; input the asset amount as a decimal string. - Please use a valid market ID (example: 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5). `, schema: schemas_1.WithdrawCollateralSchema, // You may want to rename this to `WithdrawSchema` if it's a different zod schema }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "withdrawCollateralLoanAsset", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "borrow_loan_asset", description: ` This action allows borrowing loanToken assets from a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of loanToken to borrow in whole units (e.g., 1 USDC, 0.5 DAI) Examples for USDC (6 decimals): - 1 USDC - 0.5 USDC - 0.01 USDC Make sure: - The market exists and is active. - You have deposited sufficient collateral in the market. - You pass a valid marketId (e.g. 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5). `, schema: schemas_1.BorrowSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "borrowLoanAsset", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "repay_loan_asset", description: ` This action allows repaying loanToken debt in a Morpho Blue market using a market ID. Inputs: - marketId: Unique ID of the market (bytes32 hash) - assets: The amount of assets to repay (in whole units) Notes: - Shares is set to 0, meaning assets will be used directly. - onBehalf is your own address. - Example marketId: 0x65086b4f89ea71ea533af56a6b4075e0f16a52879e17b091f6658a18d96177e5 `, schema: schemas_1.RepaySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoWriteActionProvider.prototype, "repay", null); const morphoWriteActionProvider = () => new MorphoWriteActionProvider(); exports.morphoWriteActionProvider = morphoWriteActionProvider;