Skip to content

Commit

Permalink
feat(data): create function to check if a member exists
Browse files Browse the repository at this point in the history
re #321
  • Loading branch information
cedoor committed May 15, 2023
1 parent 5915ea0 commit 9d7076a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ const group = await semaphoreSubgraph.getGroup("42")
const { members, verifiedProofs } = semaphoreSubgraph.getGroup("42", { members: true, verifiedProofs: true })
```

\# **isGroupMember**(groupId: _string_, member: _string_): _Promise\<boolean>_

```typescript
await semaphoreSubgraph.isGroupMember("42", "16948514235341957898454876473214737047419402240398321289450170535251226167324")
```

\# **new Ethers**(networkOrEthereumURL: Network | string = "goerli", options: EthersOptions = {}): _SemaphoreEthers_

```typescript
Expand Down
27 changes: 27 additions & 0 deletions packages/data/src/subgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,31 @@ describe("SemaphoreSubgraph", () => {
})
})
})

describe("# isGroupMember", () => {
it("Should throw an error if the member parameter type is wrong", async () => {
const fun = () => semaphore.isGroupMember("1", 1 as any)

await expect(fun).rejects.toThrow("Parameter 'member' is not a string")
})

it("Should return true if a group member exist", async () => {
requestMocked.mockImplementationOnce(() =>
Promise.resolve({
groups: [
{
id: "1"
}
]
})
)

const expectedValue = await semaphore.isGroupMember(
"1",
"19759682999141591121829027463339362582441675980174830329241909768001406603165"
)

expect(expectedValue).toBeTruthy()
})
})
})
26 changes: 26 additions & 0 deletions packages/data/src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,30 @@ export default class SemaphoreSubgraph {

return groups[0]
}

/**
* Returns true if a member is part of group, and false otherwise.
* @param groupId Group id
* @param member Group member.
* @returns True if the member is part of the group, false otherwise.
*/
async isGroupMember(groupId: string, member: string): Promise<boolean> {
checkParameter(groupId, "groupId", "string")
checkParameter(member, "member", "string")

const config: AxiosRequestConfig = {
method: "post",
data: JSON.stringify({
query: `{
groups(where: { id: "${groupId}", members_: { identityCommitment: "${member}" } }) {
id
}
}`
})
}

const { groups } = await request(this._url, config)

return groups.length !== 0
}
}

0 comments on commit 9d7076a

Please sign in to comment.