UNPKG

@byzantine/vault-sdk

Version:

Byzantine Vault SDK for creating and managing vaults on Ethereum for restaking strategies

132 lines (131 loc) 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DepositClient = void 0; const ethers_1 = require("ethers"); const abis_1 = require("../../constants/abis"); const constants_1 = require("../../constants"); const viem_1 = require("viem"); const utils_1 = require("../../utils"); class DepositClient { constructor(provider, signer) { this.provider = provider; this.signer = signer; } /** * Get the vault contract instance * @param vaultAddress The address of the vault * @returns The vault contract instance */ getVaultContract(vaultAddress) { return new ethers_1.ethers.Contract(vaultAddress, abis_1.ERC20_VAULT_ABI, this.signer || this.provider); } /** * Get the asset address of a vault * @param vaultAddress The address of the vault * @returns The asset address */ async getVaultAsset(vaultAddress) { const vaultContract = this.getVaultContract(vaultAddress); return await (0, utils_1.callContractMethod)(vaultContract, "asset"); } /** * Get the balance of assets in a user's wallet * @param assetAddress The address of the asset * @param userAddress The address of the user * @returns The user's wallet balance */ async getUserWalletBalance(assetAddress, userAddress) { // Check if asset is native ETH if (assetAddress === constants_1.ETH_TOKEN_ADDRESS) { return await this.provider.getBalance(userAddress); } else { // For ERC20 tokens const tokenContract = new ethers_1.ethers.Contract(assetAddress, viem_1.erc20Abi, this.provider); return await (0, utils_1.callContractMethod)(tokenContract, "balanceOf", userAddress); } } /** * Get the balance of a user in a vault * @param vaultAddress The address of the vault * @param userAddress The address of the user * @returns The user's vault balance */ async getUserVaultBalance(vaultAddress, userAddress) { const vaultContract = this.getVaultContract(vaultAddress); return await (0, utils_1.callContractMethod)(vaultContract, "balanceOf", userAddress); } /** * Get the total value locked in a vault * @param vaultAddress The address of the vault * @returns The total value locked */ async getVaultTVL(vaultAddress) { const vaultContract = this.getVaultContract(vaultAddress); return await (0, utils_1.callContractMethod)(vaultContract, "totalAssets"); } /** * Get the allowance of a user for a vault * @param assetAddress The address of the asset * @param userAddress The address of the user * @param vaultAddress The address of the vault * @returns The user's allowance */ async getUserAllowance(assetAddress, userAddress, vaultAddress) { // If asset is native ETH, there's no allowance concept if (assetAddress === constants_1.ETH_TOKEN_ADDRESS) { return BigInt(0); } const tokenContract = new ethers_1.ethers.Contract(assetAddress, viem_1.erc20Abi, this.provider); return await (0, utils_1.callContractMethod)(tokenContract, "allowance", userAddress, vaultAddress); } /** * Approve a vault to spend user's tokens * @param assetAddress The address of the asset * @param vaultAddress The address of the vault * @param amount The amount to approve * @returns Transaction response */ async approveVault(assetAddress, vaultAddress, amount) { if (!this.signer) { throw new Error("Signer is required for this operation"); } if (assetAddress === constants_1.ETH_TOKEN_ADDRESS) { throw new Error("Cannot approve native ETH"); } const tokenContract = new ethers_1.ethers.Contract(assetAddress, viem_1.erc20Abi, this.signer); return await (0, utils_1.executeContractMethod)(tokenContract, "approve", vaultAddress, amount); } /** * Deposit assets into a vault * @param vaultAddress The address of the vault * @param amount The amount to deposit * @param autoApprove Whether to automatically approve if needed, true by default * @returns Transaction response */ async depositToVault(vaultAddress, amount, autoApprove = true) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); const assetAddress = await (0, utils_1.callContractMethod)(vaultContract, "asset"); const signerAddress = await this.signer.getAddress(); // If asset is native ETH if (assetAddress === constants_1.ETH_TOKEN_ADDRESS) { return await (0, utils_1.executeContractMethod)(vaultContract, "deposit", amount, signerAddress, { value: amount }); } else { // For ERC20 tokens if (autoApprove) { const tokenContract = new ethers_1.ethers.Contract(assetAddress, viem_1.erc20Abi, this.signer); const allowance = await (0, utils_1.callContractMethod)(tokenContract, "allowance", signerAddress, vaultAddress); if (allowance < amount) { const approveTx = await (0, utils_1.executeContractMethod)(tokenContract, "approve", vaultAddress, amount); await approveTx.wait(); } } return await (0, utils_1.executeContractMethod)(vaultContract, "deposit", amount, signerAddress); } } } exports.DepositClient = DepositClient;