UNPKG

@byzantine/vault-sdk

Version:

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

137 lines (136 loc) 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WithdrawClient = void 0; const ethers_1 = require("ethers"); const abis_1 = require("../../constants/abis"); const utils_1 = require("../../utils"); class WithdrawClient { 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 queued withdrawal requests for a specific staker * @param vaultAddress The address of the vault * @param stakerAddress Address of the staker * @returns Array of withdrawal request IDs */ async getQueuedWithdrawalRequests(vaultAddress, stakerAddress) { // The contract doesn't have a direct method to get queued withdrawal requests // This would typically require accessing blockchain events or an indexer throw new Error("Function not implemented - requires event querying or indexer"); } /** * Check if a withdrawal request is claimable * @param vaultAddress The address of the vault * @param requestId The ID of the withdrawal request * @returns True if the request is claimable */ async isClaimable(vaultAddress, requestId) { const vaultContract = this.getVaultContract(vaultAddress); try { // Use the correct way to call static method in ethers.js v6 // In v6, we use directly the method with staticCall option await vaultContract.completeWithdrawal.staticCall(requestId); // If we get here, it means the simulation was successful return true; } catch (error) { // The simulation failed, so the transaction would revert // console.log("Revert reason:", error); return false; } } /** * Withdraw assets from a vault * @param vaultAddress The address of the vault * @param amount The amount of assets to withdraw * @returns Transaction response */ async withdrawFromVault(vaultAddress, amount) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); const signerAddress = await this.signer.getAddress(); // Call withdraw function with the same address for receiver and owner return await (0, utils_1.executeContractMethod)(vaultContract, "withdraw", amount, signerAddress, signerAddress); } /** * Redeem shares from a vault * @param vaultAddress The address of the vault * @param shares The amount of shares to redeem * @returns Transaction response */ async redeemSharesFromVault(vaultAddress, shares) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); const signerAddress = await this.signer.getAddress(); // Call redeem function with the same address for receiver and owner return await (0, utils_1.executeContractMethod)(vaultContract, "redeem", shares, signerAddress, signerAddress); } /** * Request withdrawal of native ETH from the vault * @param vaultAddress The address of the vault * @param assets The amount of assets to withdraw (use 0 for all available) * @returns Transaction response */ async requestNativeWithdrawal(vaultAddress, assets) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); const signerAddress = await this.signer.getAddress(); // For native ETH withdrawal, call withdraw but with the assets value return await (0, utils_1.executeContractMethod)(vaultContract, "withdraw", assets, signerAddress, signerAddress); } /** * Redeem shares for native ETH from the vault * @param vaultAddress The address of the vault * @param shares The amount of shares to redeem (use 0 for all available) * @returns Transaction response */ async redeemNativeShares(vaultAddress, shares) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); const signerAddress = await this.signer.getAddress(); // For native ETH redemption, call redeem with the shares value return await (0, utils_1.executeContractMethod)(vaultContract, "redeem", shares, signerAddress, signerAddress); } /** * Complete a withdrawal request * @param vaultAddress The address of the vault * @param requestId The ID of the withdrawal request * @returns Transaction response */ async completeWithdrawal(vaultAddress, requestId) { if (!this.signer) { throw new Error("Signer is required for this operation"); } const vaultContract = this.getVaultContract(vaultAddress); return await (0, utils_1.executeContractMethod)(vaultContract, "completeWithdrawal", requestId); } /** * Get a withdrawal request * @param vaultAddress The address of the vault * @param requestId The ID of the withdrawal request * @returns The withdrawal request */ async getWithdrawalRequest(vaultAddress, requestId) { const vaultContract = this.getVaultContract(vaultAddress); return await (0, utils_1.callContractMethod)(vaultContract, "getWithdrawalRequest", requestId); } } exports.WithdrawClient = WithdrawClient;