@byzantine/operator-sdk
Version:
SDK for integrating operators with Byzantine Finance ecosystem
152 lines (151 loc) • 6.15 kB
JavaScript
;
/**
* Native OperatorRegistry
*
* Client for interacting with the Native Operator Registry contract
* This service allows operators to register in the Byzantine native ecosystem
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NativeOperatorRegistry = void 0;
const ethers_1 = require("ethers");
const networks_1 = require("../../constants/networks");
const operatorRegistryABI_1 = require("../../constants/native/operatorRegistryABI");
const utils_1 = require("../../utils");
class NativeOperatorRegistry {
constructor(provider, signer, chainId) {
this.provider = provider;
this.signer = signer;
this.chainId = chainId;
const networkConfig = (0, networks_1.getNetworkConfig)(this.chainId);
// For now, we're using a placeholder address - this should be updated
// once the native registry is deployed to the respective networks
const contractAddress = networkConfig.byzOperatorRegistry;
if (!contractAddress || contractAddress === ethers_1.ethers.ZeroAddress) {
throw new Error(`Native Operator Registry not configured for chain ${this.chainId}`);
}
// Initialize contract with ABI and address
this.contract = new ethers_1.ethers.Contract(contractAddress, operatorRegistryABI_1.NativeOperatorRegistryABI, this.signer || this.provider);
}
/**
* Validate operator parameters to avoid common errors
*
* @param name - Name for the operator
* @param operatorFee - Fee percentage
*/
validateOperatorParams(name, operatorFee) {
if (!name || name.trim() === "") {
throw new Error("Operator name cannot be empty");
}
// Fee range validation, usually contracts limit this to a range
// Assuming max fee is 1000 (10%)
if (operatorFee < 0 || operatorFee > 1000) {
throw new Error("Operator fee must be between 0 and 1000 (0% and 10%)");
}
}
/**
* Register as an operator in the Native ecosystem
*
* @param name - Unique name for the operator
* @param admin - Address that will administer the operator
* @param operatorFee - Fee percentage (as uint16)
* @param managers - Array of manager addresses
* @returns Transaction response with index (bytes32) of the registered operator
*/
async registerOperator(name, admin, operatorFee, managers) {
// Validate parameters
this.validateOperatorParams(name, operatorFee);
return (0, utils_1.executeContractMethod)(this.contract, "registerOperator", name, admin, operatorFee, managers);
}
/**
* Unregister an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @returns Transaction response
*/
async unregisterOperator(operatorIndex) {
return (0, utils_1.executeContractMethod)(this.contract, "unregisterOperator", operatorIndex);
}
/**
* Check if an operator name is already registered
*
* @param name - The name to check
* @returns Boolean indicating if the operator is registered
*/
async isOperatorRegistered(name) {
return (0, utils_1.callContractMethod)(this.contract, "isOperatorRegistered", name);
}
/**
* Get the operator ID from a name
*
* @param name - The operator name
* @returns Bytes32 ID
*/
async getOperatorId(name) {
return (0, utils_1.callContractMethod)(this.contract, "getOperatorId", name);
}
/**
* Get the admin address of an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @returns Admin address
*/
async getOperatorAdmin(operatorIndex) {
return (0, utils_1.callContractMethod)(this.contract, "getOperatorAdmin", operatorIndex);
}
/**
* Get the fee percentage of an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @returns Fee percentage as uint16
*/
async getOperatorFee(operatorIndex) {
return (0, utils_1.callContractMethod)(this.contract, "getOperatorFee", operatorIndex);
}
/**
* Check if an address is a manager of an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @param address - The address to check
* @returns Boolean indicating if the address is a manager
*/
async isManagerOfOperator(operatorIndex, address) {
return (0, utils_1.callContractMethod)(this.contract, "isManagerOfOperator", operatorIndex, address);
}
/**
* Set manager status for an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @param managers - Array of manager addresses
* @param isManager - Boolean indicating if the addresses should be managers
* @returns Transaction response
*/
async setOperatorManager(operatorIndex, managers, isManager) {
return (0, utils_1.executeContractMethod)(this.contract, "setOperatorManager", operatorIndex, managers, isManager);
}
/**
* Transfer the admin role for an operator
*
* @param operatorIndex - The bytes32 index of the operator
* @param newAdmin - Address of the new admin
* @returns Transaction response
*/
async transferAdminRole(operatorIndex, newAdmin) {
if (!ethers_1.ethers.isAddress(newAdmin)) {
throw new Error(`Invalid new admin address: ${newAdmin}`);
}
return (0, utils_1.executeContractMethod)(this.contract, "transferAdminRole", operatorIndex, newAdmin);
}
/**
* Update an operator's fee
*
* @param operatorIndex - The bytes32 index of the operator
* @param operatorFee - New fee percentage (as uint16)
* @returns Transaction response
*/
async updateOperatorFee(operatorIndex, operatorFee) {
// Validate fee
this.validateOperatorParams("valid-name", operatorFee);
return (0, utils_1.executeContractMethod)(this.contract, "updateOperatorFee", operatorIndex, operatorFee);
}
}
exports.NativeOperatorRegistry = NativeOperatorRegistry;