### TL;DR
Exposed permission detection logic for the Extensions module.
### What changed?
- Added `is*Supported` functions for various permission-related operations.
- Exported these new functions to make them accessible to users.
- Implemented `isGetAllRoleMembersSupported` function to check if both `getRoleMember` and `getRoleMemberCount` are supported.
- Renamed `grant.ts` to `grantRole.ts` for consistency.
### How to test?
1. Import the new `is*Supported` functions from the permissions module.
2. Pass an array of contract function selectors to these functions.
3. Verify that they correctly identify whether the corresponding permission operations are supported.
Example:
```typescript
import { isGetAllRoleMembersSupported } from "thirdweb/extensions/permissions";
const supported = isGetAllRoleMembersSupported(["0x..."]);
console.log("getAllRoleMembers supported:", supported);
```
### Why make this change?
This change allows developers to programmatically detect which permission-related operations are supported by a given contract. This is particularly useful when working with contracts that may implement only a subset of the permissions interface, enabling more robust and flexible integration with various smart contracts.
---
<!-- start pr-codex -->
---
## PR-Codex overview
This PR enhances the `thirdweb` package by exposing permission detection logic for various role-related operations.
### Detailed summary
- Exposes permission detection logic for `hasRole`, `revokeRole`, `getRoleAdmin`, `renounceRole`, and more
- Renamed `grant.js` to `grantRole.js`
- Added new exports for supported permission operations
- Added a new function `isGetAllRoleMembersSupported` to check support for `getAllRoleMembers` operation
> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`
<!-- end pr-codex -->
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import type { BaseTransactionOptions } from "../../../transaction/types.js";
|
|
import { getRoleMemberCount as generatedGetRoleMemberCount } from "../__generated__/IPermissionsEnumerable/read/getRoleMemberCount.js";
|
|
import { type RoleInput, getRoleHash } from "../utils.js";
|
|
|
|
export { isGetRoleMemberCountSupported } from "../__generated__/IPermissionsEnumerable/read/getRoleMemberCount.js";
|
|
|
|
/**
|
|
* @extension PERMISSIONS
|
|
*/
|
|
export type GetRoleMemberCountParams = {
|
|
role: RoleInput;
|
|
};
|
|
|
|
/**
|
|
* Retrieves the number of members of a specific role.
|
|
*
|
|
* @param options - The options for retrieving role member count.
|
|
* @returns A promise that resolves to the number of members of the role.
|
|
* @extension PERMISSIONS
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* import { getRoleMemberCount } from "thirdweb/extensions/permissions";
|
|
*
|
|
* const result = await getRoleMemberCount({
|
|
* contract,
|
|
* role: "admin",
|
|
* });
|
|
* ```
|
|
*/
|
|
export function getRoleMemberCount(
|
|
options: BaseTransactionOptions<GetRoleMemberCountParams>,
|
|
): Promise<bigint> {
|
|
return generatedGetRoleMemberCount({
|
|
contract: options.contract,
|
|
role: getRoleHash(options.role),
|
|
});
|
|
}
|