import type { Address, Hex } from "viem"; import type { LucidlyClient } from "./client.js"; import type { TxResult, RoleName } from "./types.js"; import { Roles } from "./types.js"; import { RolesAuthority, ManagerWithMerkleVerification } from "./artifacts.js"; export class RolesModule { constructor(private readonly c: LucidlyClient) {} /** Grant an arbitrary role id (or named role) to a user via RolesAuthority. */ async grant(user: Address, role: RoleName | number, rolesAuthority?: Address): Promise { const ra = this.c.requireRolesAuthority("roles.grant", rolesAuthority); const id = typeof role === "number" ? role : Roles[role]; const hash = await this.c.send({ address: ra, abi: RolesAuthority.abi, functionName: "setUserRole", args: [user, id, true], }); return this.c.wrapTx(hash); } /** Revoke a role from a user. */ async revoke(user: Address, role: RoleName | number, rolesAuthority?: Address): Promise { const ra = this.c.requireRolesAuthority("roles.revoke", rolesAuthority); const id = typeof role === "number" ? role : Roles[role]; const hash = await this.c.send({ address: ra, abi: RolesAuthority.abi, functionName: "setUserRole", args: [user, id, false], }); return this.c.wrapTx(hash); } /** Grant STRATEGIST so an address can call manageVaultWithMerkleVerification. */ async grantStrategist(strategist: Address, rolesAuthority?: Address): Promise { return this.grant(strategist, "STRATEGIST", rolesAuthority); } /** Check whether a user holds a role. */ async hasRole(user: Address, role: RoleName | number, rolesAuthority?: Address): Promise { const ra = this.c.requireRolesAuthority("roles.hasRole", rolesAuthority); const id = typeof role === "number" ? role : Roles[role]; return this.c.read({ address: ra, abi: RolesAuthority.abi, functionName: "doesUserHaveRole", args: [user, id], }); } /** Set the Merkle manage root for a strategist (ManagerWithMerkleVerification.setManageRoot). */ async setManageRoot(strategist: Address, root: Hex, manager?: Address): Promise { const m = this.c.requireManager("roles.setManageRoot", manager); const hash = await this.c.send({ address: m, abi: ManagerWithMerkleVerification.abi, functionName: "setManageRoot", args: [strategist, root], }); return this.c.wrapTx(hash); } /** Read the current manage root for a strategist. */ async getManageRoot(strategist: Address, manager?: Address): Promise { const m = this.c.requireManager("roles.getManageRoot", manager); return this.c.read({ address: m, abi: ManagerWithMerkleVerification.abi, functionName: "manageRoot", args: [strategist], }); } }