UNPKG

@devasher/kuru-sdk

Version:

Ethers v6 SDK to interact with Kuru (forked from @kuru-labs/kuru-sdk)

93 lines 4.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MarginDeposit = void 0; // ============ External Imports ============ const ethers_1 = require("ethers"); // ============ Internal Imports ============ const utils_1 = require("../utils"); // ============ Config Imports ============ const IERC20_json_1 = __importDefault(require("../../abi/IERC20.json")); const MarginAccount_json_1 = __importDefault(require("../../abi/MarginAccount.json")); const txConfig_1 = __importDefault(require("../utils/txConfig")); class MarginDeposit { static async deposit(providerOrSigner, marginAccountAddress, userAddress, tokenAddress, amount, decimals, approveTokens, txOptions) { console.log('typeof providerOrSigner', typeof providerOrSigner); console.log('providerOrSigner', providerOrSigner instanceof ethers_1.ethers.AbstractSigner); try { const tokenContract = new ethers_1.ethers.Contract(tokenAddress, IERC20_json_1.default.abi, providerOrSigner); if (approveTokens && tokenAddress !== ethers_1.ZeroAddress) { await (0, utils_1.approveToken)(tokenContract, marginAccountAddress, (0, ethers_1.parseUnits)(amount, decimals), providerOrSigner); } const formattedAmount = (0, ethers_1.parseUnits)(amount, decimals); let signer; try { signer = (await providerOrSigner.getAddress()) ? providerOrSigner : await providerOrSigner.getSigner(); } catch (_a) { signer = await providerOrSigner.getSigner(); } const tx = await MarginDeposit.constructDepositTransaction(signer, marginAccountAddress, userAddress, tokenAddress, formattedAmount, txOptions); const transaction = await signer.sendTransaction(tx); const receipt = await transaction.wait(); if (!receipt) { throw new Error('Transaction failed'); } return receipt; } catch (e) { if (!e.error) { throw e; } throw (0, utils_1.extractErrorMessage)(e); } } static async constructDepositTransaction(signer, marginAccountAddress, userAddress, tokenAddress, amount, txOptions) { const address = await signer.getAddress(); const marginAccountInterface = new ethers_1.ethers.Interface(MarginAccount_json_1.default.abi); const data = marginAccountInterface.encodeFunctionData('deposit', [userAddress, tokenAddress, amount]); const value = tokenAddress === ethers_1.ZeroAddress ? amount : BigInt(0); return (0, txConfig_1.default)({ to: marginAccountAddress, from: address, data, value, txOptions, signer, }); } static async estimateGas(providerOrSigner, marginAccountAddress, userAddress, tokenAddress, amount, decimals, approveTokens) { try { const tokenContract = new ethers_1.ethers.Contract(tokenAddress, IERC20_json_1.default.abi, providerOrSigner); const marginAccount = new ethers_1.ethers.Contract(marginAccountAddress, MarginAccount_json_1.default.abi, providerOrSigner); const formattedAmount = (0, ethers_1.parseUnits)(amount, decimals); let gasEstimate; if (tokenAddress === ethers_1.ZeroAddress) { gasEstimate = await marginAccount.deposit.estimateGas(userAddress, tokenAddress, formattedAmount, { value: formattedAmount, }); } else { if (approveTokens) { gasEstimate = await (0, utils_1.estimateApproveGas)(tokenContract, marginAccountAddress, (0, ethers_1.parseUnits)(amount, decimals)); } else { gasEstimate = await marginAccount.deposit.estimateGas(userAddress, tokenAddress, formattedAmount); } } return gasEstimate; } catch (e) { if (!e.error) { throw e; } throw (0, utils_1.extractErrorMessage)(e); } } } exports.MarginDeposit = MarginDeposit; //# sourceMappingURL=deposit.js.map