UNPKG

sei-agent-kit

Version:

A package for building AI agents on the SEI blockchain

94 lines 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mintTakara = mintTakara; const viem_1 = require("viem"); const chains_1 = require("viem/chains"); const getTokenDecimals_1 = require("../../utils/getTokenDecimals"); const t_Tokenabi_1 = require("./abi/mint/t_Tokenabi"); const erc20abi_1 = require("./abi/mint/erc20abi"); const tokenMap_1 = require("./tokenMap"); /** * Mints tTokens by depositing underlying tokens into Takara Protocol * @param agent SeiAgentKit instance * @param params Parameters for minting * @returns Transaction hash */ async function mintTakara(agent, params) { const tTokenAddress = (0, tokenMap_1.getTakaraTTokenAddress)(params.ticker); if (!tTokenAddress) { throw new Error(`Invalid ticker: ${params.ticker}`); } // 1. Get the underlying token address from the tToken contract const underlyingTokenAddress = await agent.publicClient.readContract({ address: tTokenAddress, abi: t_Tokenabi_1.tTokenAbi, functionName: 'underlying', }); // 2. Get the decimals of the underlying token const tokenDecimals = await (0, getTokenDecimals_1.getTokenDecimals)(agent, underlyingTokenAddress); // 3. Convert the mint amount to the proper decimal representation const mintAmountRaw = (0, viem_1.parseUnits)(params.mintAmount, tokenDecimals); // 4. Check user's current token balance const tokenBalance = await agent.publicClient.readContract({ address: underlyingTokenAddress, abi: erc20abi_1.erc20Abi, functionName: 'balanceOf', args: [agent.wallet_address], }); // Ensure user has enough tokens if (tokenBalance < mintAmountRaw) { throw new Error(`Insufficient token balance. Required: ${params.mintAmount}, Available: ${(0, viem_1.formatUnits)(tokenBalance, tokenDecimals)}`); } const account = agent.walletClient.account; if (!account) { throw new Error("Wallet account is not initialized"); } // 5. Approve the tToken contract to spend tokens const approvalHash = await agent.walletClient.writeContract({ chain: chains_1.sei, account, address: underlyingTokenAddress, abi: erc20abi_1.erc20Abi, functionName: 'approve', args: [tTokenAddress, mintAmountRaw], }); // Wait for approval to be mined await agent.publicClient.waitForTransactionReceipt({ hash: approvalHash }); // 6. Get the current exchange rate const { result: exchangeRate } = await agent.publicClient.simulateContract({ address: tTokenAddress, abi: t_Tokenabi_1.tTokenAbi, functionName: 'exchangeRateCurrent', }); // 7. Calculate expected tToken amount (mintAmount * 1e18 / exchangeRate) const expectedTTokenAmountRaw = (mintAmountRaw * BigInt(10 ** 18)) / exchangeRate; const expectedTTokenAmount = (0, viem_1.formatUnits)(expectedTTokenAmountRaw, 18); // 8. Call the mint function on the tToken contract const mintTxHash = await agent.walletClient.writeContract({ chain: chains_1.sei, account, address: tTokenAddress, abi: t_Tokenabi_1.tTokenAbi, functionName: 'mint', args: [mintAmountRaw], }); // Wait for mint transaction to be mined const receipt = await agent.publicClient.waitForTransactionReceipt({ hash: mintTxHash }); // Check if transaction was successful if (receipt.status !== 'success') { throw new Error(`Mint transaction failed. Hash: ${mintTxHash}`); } // 9. Verify the tToken balance after minting const tTokenBalance = await agent.publicClient.readContract({ address: tTokenAddress, abi: t_Tokenabi_1.tTokenAbi, functionName: 'balanceOf', args: [agent.wallet_address], }); return mintTxHash; } //# sourceMappingURL=mint.js.map