Skip to content

Commit

Permalink
add fetchRolesMod function
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Aug 14, 2023
1 parent 9266881 commit a4692d3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/app/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function AttachMod() {
onChange={(ev) => {
const { value } = ev.target;
if (value.indexOf(":") > 0) {
const [chainPrefix, address] = value.split(":");
const [chainPrefix] = value.split(":");
const nextChainId =
chains.find((chain) => chain.prefix === chainPrefix)
?.id || chainId;
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/fetchRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const QUERY = `
}
}
}
annotations {
url
schema
}
}
}
`
Expand Down
76 changes: 76 additions & 0 deletions packages/sdk/src/fetchRolesMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import SUBGRAPH from "./subgraph"
import { ChainId, Target } from "./types"

interface Props {
address: string
chainId: ChainId
}

const QUERY = `
query RolesMod($id: String) {
rolesModifier(id: $id) {
address
owner
avatar
target
roles {
members {
member {
address
}
}
targets {
address
}
}
}
}
`

export const fetchRolesMod = async ({
address,
chainId,
}: Props): Promise<RolesModifier> => {
const res = await fetch(SUBGRAPH[chainId], {
method: "POST",
body: JSON.stringify({
query: QUERY,
variables: { id: address.toLowerCase() },
operationName: "Roles",
}),
})
const { data, error } = await res.json()
if (error) {
throw new Error(error)
}
if (!data || !data.rolesModifier) {
throw new Error(`Roles modifier ${address} not found on chain #${chainId}`)
}

return mapGraphQl(data.rolesModifier)
}

interface RoleSummary {
key: string
members: `0x${string}`[]
targets: `0x${string}`[]
}

interface RolesModifier {
address: `0x${string}`
owner: `0x${string}`
avatar: `0x${string}`
target: `0x${string}`
roles: RoleSummary[]
}

const mapGraphQl = (rolesModifier: any): RolesModifier => ({
...rolesModifier,
roles: rolesModifier.roles.map(mapGraphQlRole),
})

const mapGraphQlRole = (role: any): RoleSummary => ({
key: role.key,
members: role.members.map((assignment: any) => assignment.member.address),
targets: role.targets.map((target: any): Target => target.address),
})
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./permissions"
export { encodeCalls } from "./calls"

export { fetchRole } from "./fetchRole"
export { fetchRolesMod } from "./fetchRolesMod"
export { diffTargets } from "./diffTargets"
export { replaceTargets } from "./replaceTargets"
export { extendTargets } from "./extendTargets"
Expand Down

0 comments on commit a4692d3

Please sign in to comment.