@wasserstoff/tribes-sdk
Version:
SDK for integrating with Tribes by Astrix platform on any EVM compatible chain
163 lines (162 loc) • 5.43 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RolesModule = void 0;
// import { ethers } from 'ethers'; // Removed unused import
const BaseModule_1 = require("../core/BaseModule");
const core_1 = require("../types/core");
// Import RoleManager ABI
const RoleManager_json_1 = __importDefault(require("../../abis/RoleManager.json"));
/**
* Module for managing roles
*/
class RolesModule extends BaseModule_1.BaseModule {
/**
* Get the RoleManager contract
* @param useSigner Whether to use the signer
*/
getRoleManagerContract(useSigner = false) {
// Use ethers.Contract for the specific contract type if ABI matches
// Use unknown cast as intermediate step if direct cast fails
return this.getContract(this.config.contracts.roleManager, RoleManager_json_1.default, useSigner);
}
/**
* Get the DEFAULT_ADMIN_ROLE bytes
*/
async getDefaultAdminRole() {
try {
const roleManager = this.getRoleManagerContract();
return await roleManager.DEFAULT_ADMIN_ROLE();
}
catch (error) {
return this.handleError(error, 'Failed to get DEFAULT_ADMIN_ROLE', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the ORGANIZER_ROLE bytes
*/
async getOrganizerRole() {
try {
const roleManager = this.getRoleManagerContract();
return await roleManager.ORGANIZER_ROLE();
}
catch (error) {
return this.handleError(error, 'Failed to get ORGANIZER_ROLE', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Check if an account has a specific role
* @param role Role bytes string
* @param account Account address
*/
async hasRole(role, account) {
try {
const roleManager = this.getRoleManagerContract();
return await roleManager.hasRole(role, account);
}
catch (error) {
return this.handleError(error, 'Failed to check role', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Check if an account is an admin
* @param account Account address
*/
async isAdmin(account) {
try {
const adminRole = await this.getDefaultAdminRole();
return this.hasRole(adminRole, account);
}
catch (error) {
return this.handleError(error, 'Failed to check admin status', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Check if an account is an organizer
* @param account Account address
*/
async isOrganizer(account) {
try {
const organizerRole = await this.getOrganizerRole();
return this.hasRole(organizerRole, account);
}
catch (error) {
return this.handleError(error, 'Failed to check organizer status', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Assign a role to an account
* @param account Account address
* @param role Role bytes string
* @returns Transaction hash
*/
async assignRole(account, role) {
try {
const roleManager = this.getRoleManagerContract(true);
const tx = await roleManager.assignRole(account, role);
const receipt = await tx.wait();
this.log(`Assigned role`, {
account,
role,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to assign role', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Remove a role from an account
* @param account Account address
* @param role Role bytes string
* @returns Transaction hash
*/
async removeRole(account, role) {
try {
const roleManager = this.getRoleManagerContract(true);
const tx = await roleManager.removeRole(account, role);
const receipt = await tx.wait();
this.log(`Removed role`, {
account,
role,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to remove role', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Make an account an admin
* @param account Account address
* @returns Transaction hash
*/
async makeAdmin(account) {
try {
const adminRole = await this.getDefaultAdminRole();
return this.assignRole(account, adminRole);
}
catch (error) {
return this.handleError(error, 'Failed to make admin', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Make an account an organizer
* @param account Account address
* @returns Transaction hash
*/
async makeOrganizer(account) {
try {
const organizerRole = await this.getOrganizerRole();
return this.assignRole(account, organizerRole);
}
catch (error) {
return this.handleError(error, 'Failed to make organizer', core_1.ErrorType.CONTRACT_ERROR);
}
}
}
exports.RolesModule = RolesModule;