@byzantine/vault-sdk
Version:
Byzantine Vault SDK for creating and managing vaults on Ethereum for restaking strategies
100 lines (99 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EigenLayerClient = void 0;
const ethers_1 = require("ethers");
const abis_1 = require("../../constants/abis");
const VaultType_1 = require("./VaultType");
const utils_1 = require("../../utils");
class EigenLayerClient {
constructor(provider, signer) {
this.provider = provider;
this.signer = signer;
this.vaultTypeClient = new VaultType_1.VaultTypeClient(provider);
this.vaultCache = new Map();
}
/**
* Get the SuperVault contract instance
* @param vaultAddress The address of the SuperVault
* @returns The SuperVault contract instance
*/
getSuperVaultContract(vaultAddress) {
return new ethers_1.ethers.Contract(vaultAddress, abis_1.SUPER_ERC20_ABI, this.signer || this.provider);
}
/**
* Get the vault contract instance
* @param vaultAddress The address of the vault or SuperVault
* @returns The vault contract instance
*/
async getVaultContract(vaultAddress) {
// Check if we have cached information for this vault
let cacheEntry = this.vaultCache.get(vaultAddress);
if (!cacheEntry) {
cacheEntry = {};
this.vaultCache.set(vaultAddress, cacheEntry);
}
// If we don't know if it's a SuperVault yet, check it
if (cacheEntry.isSupervault === undefined) {
cacheEntry.isSupervault = await this.vaultTypeClient.isSupervault(vaultAddress);
}
let eigenVaultAdd;
// If we've already determined the eigenVault address, use it
if (cacheEntry.eigenVaultAddress) {
eigenVaultAdd = cacheEntry.eigenVaultAddress;
}
else {
// Otherwise, retrieve it based on the vault type
if (cacheEntry.isSupervault) {
// For SuperVaults, we need to get the eigenVault from the SuperVault contract
const superVaultContract = this.getSuperVaultContract(vaultAddress);
const [symVaultAddress, eigenVaultAddress] = await (0, utils_1.callContractMethod)(superVaultContract, "getUnderlyingVaults");
eigenVaultAdd = eigenVaultAddress;
}
else {
// For regular EigenLayer vaults, the address is the same
eigenVaultAdd = vaultAddress;
}
// Cache the result
cacheEntry.eigenVaultAddress = eigenVaultAdd;
}
return new ethers_1.ethers.Contract(eigenVaultAdd, abis_1.ERC20_VAULT_ABI, this.signer || this.provider);
}
/**
* Clear the cache for a specific vault or all vaults
* @param vaultAddress Optional address of the vault to clear from cache
*/
clearCache(vaultAddress) {
if (vaultAddress) {
this.vaultCache.delete(vaultAddress);
}
else {
this.vaultCache.clear();
}
}
/**
* Get the Eigen Operator of a vault
* @param vaultAddress The address of the vault
* @returns The Eigen Operator
*/
async getEigenOperator(vaultAddress) {
const vaultContract = await this.getVaultContract(vaultAddress);
return await (0, utils_1.callContractMethod)(vaultContract, "delegatedTo");
}
/**
* Set the Eigen Operator of a vault
* @param vaultAddress The address of the vault
* @param operator The new Eigen Operator
* @param approverSignatureAndExpiry The signature and expiry for the approver
* @param approverSalt The salt for the approver
* @param options Optional transaction parameters like gas limit, gas price, etc.
* @returns Transaction response
*/
async setEigenOperator(vaultAddress, operator, approverSignatureAndExpiry, approverSalt, options) {
if (!this.signer) {
throw new Error("Signer is required for this operation");
}
const vaultContract = await this.getVaultContract(vaultAddress);
return await (0, utils_1.executeContractMethod)(vaultContract, "delegateTo", operator, approverSignatureAndExpiry, approverSalt, options);
}
}
exports.EigenLayerClient = EigenLayerClient;