Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use production subgraphs #319

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ Address: `0x91B1bd7BCC5E623d5CE76b0152253499a9C819d1`

#### Subgraphs

- [Mainnet](https://api.studio.thegraph.com/query/23167/zodiac-roles-mainnet/v2.3.4)
- [Optimism](https://api.studio.thegraph.com/query/23167/zodiac-roles-optimism/v2.3.4)
- [Gnosis](https://api.studio.thegraph.com/query/23167/zodiac-roles-gnosis/v2.3.4)
- [Polygon](https://api.studio.thegraph.com/query/23167/zodiac-roles-polygon/v2.3.4)
- [Polygon zkEVM](https://api.studio.thegraph.com/query/23167/zodiac-roles-zkevm/v2.3.4)
- [Arbitrum One](https://api.studio.thegraph.com/query/23167/zodiac-roles-arbitrum-one/v2.3.4)
- [Avalanche C-Chain](https://api.studio.thegraph.com/query/23167/zodiac-roles-avalanche/v2.3.4)
- [BSC](https://api.studio.thegraph.com/query/23167/zodiac-roles-bsc/v2.3.4)
- [Base](https://api.studio.thegraph.com/query/23167/zodiac-roles-base/v2.3.4)
- [Base Sepolia](https://api.studio.thegraph.com/query/23167/zodiac-roles-base-sepolia/v2.3.4)
- [Sepolia](https://api.studio.thegraph.com/query/23167/zodiac-roles-sepolia/v2.3.4)
- [Mainnet](https://api.studio.thegraph.com/query/93263/zodiac-roles-mainnet/version/latest)
- [Optimism](https://api.studio.thegraph.com/query/93263/zodiac-roles-optimism/version/latest)
- [Gnosis](https://api.studio.thegraph.com/query/93263/zodiac-roles-gnosis/version/latest)
- [Polygon](https://api.studio.thegraph.com/query/93263/zodiac-roles-polygon/version/latest)
- [Polygon zkEVM](https://api.studio.thegraph.com/query/93263/zodiac-roles-zkevm/version/latest)
- [Arbitrum One](https://api.studio.thegraph.com/query/93263/zodiac-roles-arbitrum-one/version/latest)
- [Avalanche C-Chain](https://api.studio.thegraph.com/query/93263/zodiac-roles-avalanche/version/latest)
- [BSC](https://api.studio.thegraph.com/query/93263/zodiac-roles-bsc/version/latest)
- [Base](https://api.studio.thegraph.com/query/93263/zodiac-roles-base/version/latest)
- [Base Sepolia](https://api.studio.thegraph.com/query/93263/zodiac-roles-base-sepolia/version/latest)
- [Sepolia](https://api.studio.thegraph.com/query/93263/zodiac-roles-sepolia/version/latest)

### Development environment setup

Expand Down
33 changes: 11 additions & 22 deletions packages/deployments/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,56 @@ export const chains = {
[1]: {
name: "mainnet",
prefix: "eth",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-mainnet/v2.3.4",
subgraphDeploymentId: "QmSXFnZr641zZforeXRAGHfynCEi1Vq35AKQ8qB1W1CdU6",
},
[10]: {
name: "optimism",
prefix: "oeth",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-optimism/v2.3.4",
subgraphDeploymentId: "QmeJXG1sWpdbqs4nZE9JFs2j9nGuFLH4VCmbCVXP7oQh9d",
},
[100]: {
name: "gnosis",
prefix: "gno",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-gnosis/v2.3.4",
subgraphDeploymentId: "QmR1TkpcQzhBfUwmRJfeV4kQbUbDUZUvyWxtLTCYkiivvz",
},
[137]: {
name: "polygon",
prefix: "matic",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-polygon/v2.3.4",
subgraphDeploymentId: "QmVrtFGzKJcuCbdbeaCAmhzJeVpkqrySNwvaCiVSpip4cm",
},
[1101]: {
name: "zkevm",
prefix: "zkevm",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-zkevm/v2.3.4",
subgraphDeploymentId: "QmctRf4gwTT63t2SNT4pFekHfjjHyV97jX8CaMQdrUBS4y",
},
[42161]: {
name: "arbitrumOne",
prefix: "arb1",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-arbitrum-one/v2.3.4",
subgraphDeploymentId: "QmPBUT8onznz5kocjwzzQPoRatu8gSPAaaSra7jixaF1mD",
},
[43114]: {
name: "avalanche",
prefix: "avax",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-avalanche/v2.3.4",
subgraphDeploymentId: "QmSegJhx8SEQ5WFfi6ZLQLs8XRSfLoka7yULsUKgeGBLnT",
},
[56]: {
name: "bsc",
prefix: "bnb",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-bsc/v2.3.4",
subgraphDeploymentId: "QmUM9dfkiN5jHMxUzPYHW8tKguPr65qnWpzb3dRMKT1kDc",
},
[8453]: {
name: "base",
prefix: "base",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-base/v2.3.4",
subgraphDeploymentId: "QmWGQfg86sAjZUQ2HUeHN4mVQTejFb1XPPtLLoz2Ks7Wfp",
},
[84532]: {
name: "baseSepolia",
prefix: "basesep",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-base-sepolia/v2.3.4",
subgraphDeploymentId: "QmSZuUNsMtLjN9Gd4sNu7rCGChZ56NQc2XY7kN3YLz9pQD",
},
[11155111]: {
name: "sepolia",
prefix: "sep",
subgraph:
"https://api.studio.thegraph.com/query/93263/zodiac-roles-sepolia/v2.3.4",
subgraphDeploymentId: "QmdeXJ7g889wK4wDNpRrvKGN91SZiwvdmCU9dFQsgqaQ3K",
},
} as const
87 changes: 25 additions & 62 deletions packages/deployments/src/fetchRole.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chains } from "./chains"
import { assertNoPagination, fetchFromSubgraph, FetchOptions } from "./subgraph"
import {
ChainId,
Role,
Expand All @@ -15,18 +16,18 @@ type Props = {
blockNumber?: number
} & (
| {
/** pass a chainId to use query against a dev subgraph */
/** pass a chainId to use query against the official subgraph deployment */
chainId: ChainId
/** pass your own API key from The Graph for production use */
theGraphApiKey?: string
}
| {
/** pass your own subgraph endpoint for production use */
/** pass your own subgraph endpoint */
subgraph: string
}
)

const ROLE_QUERY = `
query Role($id: String) {
role(id: $id) {
const ROLE_FIELDS = `
key
members(first: 1000) {
member {
Expand All @@ -52,82 +53,50 @@ query Role($id: String) {
schema
}
lastUpdate
`.trim()

const ROLE_QUERY = `
query Role($id: String) {
role(id: $id) {
${ROLE_FIELDS}
}
}
`.trim()

const ROLE_AT_BLOCK_QUERY = `
query Role($id: String, $block: Int) {
role(id: $id, block: { number: $block }) {
key
members(first: 1000) {
member {
address
}
}
targets(first: 1000) {
address
clearance
executionOptions
functions(first: 1000) {
selector
executionOptions
wildcarded
condition {
id
json
}
}
}
annotations(first: 1000) {
uri
schema
}
lastUpdate
${ROLE_FIELDS}
}
}
`.trim()

const getRoleId = (address: `0x${string}`, roleKey: `0x${string}`) =>
`${address.toLowerCase()}-ROLE-${roleKey}`

type FetchOptions = Omit<RequestInit, "method" | "body">

export const fetchRole = async (
{ address, roleKey, blockNumber, ...rest }: Props,
{ address, roleKey, blockNumber, ...subgraphProps }: Props,
options?: FetchOptions
): Promise<Role | null> => {
const endpoint =
"subgraph" in rest ? rest.subgraph : chains[rest.chainId].subgraph

const res = await fetch(endpoint, {
...options,
method: "POST",
headers: {
...options?.headers,
"Content-Type": "application/json",
},
body: JSON.stringify({
const { role } = await fetchFromSubgraph(
subgraphProps,
{
query: blockNumber ? ROLE_AT_BLOCK_QUERY : ROLE_QUERY,
variables: { id: getRoleId(address, roleKey), block: blockNumber },
operationName: "Role",
}),
})
const { data, error, errors } = await res.json()

if (error || (errors && errors[0])) {
throw new Error(error || errors[0])
}
},
options
)

if (!data || !data.role) {
if (!role) {
return null
}

assertNoPagination(data.role.members)
assertNoPagination(data.role.targets)
assertNoPagination(data.role.annotations)
assertNoPagination(role.members)
assertNoPagination(role.targets)
assertNoPagination(role.annotations)

return mapGraphQl(data.role)
return mapGraphQl(role)
}

const mapGraphQl = (role: any): Role => ({
Expand Down Expand Up @@ -155,9 +124,3 @@ const mapGraphQl = (role: any): Role => ({
})
),
})

const assertNoPagination = (data: any[]) => {
if (data.length === 1000) {
throw new Error("Pagination not supported")
}
}
Loading