From 589ae569ef6b39fe993fac7c7e6fdb0c28975356 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 18 Jun 2021 17:06:07 +0200 Subject: [PATCH] Add "My Orgs" section to orgs page --- src/base/orgs/Index.svelte | 62 ++++++++++++++++++++++++-------------- src/base/orgs/List.svelte | 26 ++++++++++++++++ src/base/orgs/Org.ts | 40 ++++++++++++++++++++++-- src/config.json | 3 ++ src/config.ts | 2 +- src/utils.ts | 4 +-- 6 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/base/orgs/List.svelte diff --git a/src/base/orgs/Index.svelte b/src/base/orgs/Index.svelte index d2d0369a7..0c0e5b4cd 100644 --- a/src/base/orgs/Index.svelte +++ b/src/base/orgs/Index.svelte @@ -1,17 +1,15 @@ + + + +{#each orgs as org} +
+ + + +
+{:else} + +{/each} diff --git a/src/base/orgs/Org.ts b/src/base/orgs/Org.ts index 744d9a5c0..c7a0c9bdb 100644 --- a/src/base/orgs/Org.ts +++ b/src/base/orgs/Org.ts @@ -18,7 +18,6 @@ const GetProjects = ` } `; -// TODO: Add timestamps to org creation. const GetOrgs = ` query GetOrgs { orgs { @@ -28,6 +27,26 @@ const GetOrgs = ` } `; +const GetSafes = ` + query GetSafes($owners: [String!]!) { + wallets(where: { owners_contains: $owners }) { + id + owners + } + } +`; + +const GetOrgsByOwner = ` + query GetOrgsByOwner($owners: [String!]!) { + orgs(where: { owner_in: $owners }) { + id + owner + creator + timestamp + } + } +`; + export class Org { address: string; owner: string; @@ -75,7 +94,9 @@ export class Org { } async getProjects(config: Config): Promise> { - const result = await utils.querySubgraph(GetProjects, { org: this.address }, config); + const result = await utils.querySubgraph( + config.orgs.subgraph, GetProjects, { org: this.address } + ); const projects: Project[] = []; for (const p of result.projects) { @@ -117,7 +138,7 @@ export class Org { } static async getAll(config: Config): Promise> { - const result = await utils.querySubgraph(GetOrgs, {}, config); + const result = await utils.querySubgraph(config.orgs.subgraph, GetOrgs, {}); const orgs: Org[] = []; for (const o of result.orgs) { @@ -162,6 +183,19 @@ export class Org { } } + static async getOrgsByMember(memberAddr: string, config: Config): Promise { + const safeResult = await utils.querySubgraph( + config.safe.subgraph, GetSafes, { owners: [memberAddr] } + ); + const wallets: { id: string }[] = safeResult.wallets; + const owners = wallets.map(wallet => wallet.id).concat([memberAddr]); + const orgsResult = await utils.querySubgraph(config.orgs.subgraph, GetOrgsByOwner, { owners }); + + return orgsResult.orgs.map((o: { id: string; owner: string }) => { + return new Org(o.id, o.owner); + }); + } + static async createMultiSig( owners: [string], threshold: number, diff --git a/src/config.json b/src/config.json index 284063b05..e6857c2b8 100644 --- a/src/config.json +++ b/src/config.json @@ -16,6 +16,7 @@ }, "safe": { "api": "https://safe-transaction.gnosis.io/api/v1", + "subgraph": null, "viewer": "https://gnosis-safe.io/app/#/safes" }, "tokens": [] @@ -37,6 +38,7 @@ }, "safe": { "api": null, + "subgraph": null, "viewer": null }, "tokens": [] @@ -58,6 +60,7 @@ }, "safe": { "api": "https://safe-transaction.rinkeby.gnosis.io/api/v1", + "subgraph": "https://api.thegraph.com/subgraphs/name/radicle-dev/gnosis-safe-rinkeby", "viewer": "https://rinkeby.gnosis-safe.io/app/#/safes" }, "tokens": [] diff --git a/src/config.ts b/src/config.ts index 0dcf4b4a4..d2a1ebf18 100644 --- a/src/config.ts +++ b/src/config.ts @@ -19,7 +19,7 @@ export class Config { gasLimits: { createOrg: number }; provider: ethers.providers.JsonRpcProvider; signer: ethers.Signer & TypedDataSigner | null; - safe: { api: string | null; viewer: string | null }; + safe: { api: string | null; subgraph: string; viewer: string | null }; abi: { [contract: string]: string[] }; seed: { api?: string }; tokens: string[]; diff --git a/src/utils.ts b/src/utils.ts index 5bb7a66d0..a77ff8b38 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -95,11 +95,11 @@ export function safeLink(addr: string, config: Config): string { // Query a subgraph. export async function querySubgraph( + url: string, query: string, variables: Record, - config: Config ): Promise { - const response = await fetch(config.orgs.subgraph, { + const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json',