From 0b3b254cc05cae3f9cd14d4ae5722e9123197481 Mon Sep 17 00:00:00 2001 From: Matthew Cantelon Date: Fri, 21 May 2021 17:11:25 -0700 Subject: [PATCH] feat: add polygon and xdai chains when pulling DAO membership This uses two new subgraphs to pull DAO memberships for Ethereum, xDai, and Polygon mainnets. The pulls are in parallel. The underlying data is structured the same way but seems to be missing some information, see https://github.com/HausDAO/daohaus-supergraph/issues/44 --- packages/backend/src/config.ts | 10 ++++++ .../resolvers/daohaus/resolver.ts | 33 ++++++++++++++++--- .../src/handlers/remote-schemas/typeDefs.ts | 1 + packages/backend/src/lib/daoHausClient.ts | 15 ++++++--- schema.graphql | 1 + 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index be88530da..0781ae668 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -2,6 +2,8 @@ interface IConfig { port: number; graphqlURL: string; daoHausGraphqlURL: string; + daoHausPolygonGraphqlURL: string; + daoHausXdaiGraphqlURL: string; seedGraphqlURL: string; githubApiToken: string; adminKey: string; @@ -44,6 +46,14 @@ export const CONFIG: IConfig = { process.env.DAOHAUS_GRAPHQL_URL, 'https://api.thegraph.com/subgraphs/name/odyssy-automaton/daohaus', ), + daoHausPolygonGraphqlURL: parseEnv( + process.env.DAOUHAUS_POLYGON_GRAPHQL_URL, + 'https://api.thegraph.com/subgraphs/name/odyssy-automaton/daohaus-matic', + ), + daoHausXdaiGraphqlURL: parseEnv( + process.env.DAOUHAUS_XDAI_GRAPHQL_URL, + 'https://api.thegraph.com/subgraphs/name/odyssy-automaton/daohaus-xdai', + ), adminKey: parseEnv( process.env.HASURA_GRAPHQL_ADMIN_SECRET, 'metagame_secret', diff --git a/packages/backend/src/handlers/remote-schemas/resolvers/daohaus/resolver.ts b/packages/backend/src/handlers/remote-schemas/resolvers/daohaus/resolver.ts index 9c06fcc8c..df554b633 100644 --- a/packages/backend/src/handlers/remote-schemas/resolvers/daohaus/resolver.ts +++ b/packages/backend/src/handlers/remote-schemas/resolvers/daohaus/resolver.ts @@ -1,12 +1,37 @@ -import { daoHausClient } from '../../../../lib/daoHausClient'; -import { QueryResolvers } from '../../autogen/types'; +import { clientFactory } from '../../../../lib/daoHausClient'; +import { Member, QueryResolvers } from '../../autogen/types'; + +const addChain = (memberAddress: string) => async (chain: string) => { + const client = clientFactory(chain); + const members = ( + (await client.GetDaoHausMemberships({ memberAddress })).members + ); + + return members.map((member: Member) => { + const updatedMember: Member = { ...member }; + updatedMember.moloch.chain = chain; + return updatedMember; + }); +}; export const getDaoHausMemberships: QueryResolvers['getDaoHausMemberships'] = async ( _, { memberAddress }, ) => { if (!memberAddress) return []; - const res = await daoHausClient.GetDaoHausMemberships({ memberAddress }); - return res.members; + const membershipsOn = addChain(memberAddress); + + const res = await Promise.all([ + membershipsOn('ethereum'), + membershipsOn('polygon'), + membershipsOn('xdai'), + ]); + + const members: Member[] = res.reduce( + (allMembers, networkMembers) => [...allMembers, ...networkMembers], + [], + ); + + return members; }; diff --git a/packages/backend/src/handlers/remote-schemas/typeDefs.ts b/packages/backend/src/handlers/remote-schemas/typeDefs.ts index 6aa3d38b8..a860f02ed 100644 --- a/packages/backend/src/handlers/remote-schemas/typeDefs.ts +++ b/packages/backend/src/handlers/remote-schemas/typeDefs.ts @@ -43,6 +43,7 @@ export const typeDefs = gql` version: String totalShares: String! totalLoot: String! + chain: String! } type Member { diff --git a/packages/backend/src/lib/daoHausClient.ts b/packages/backend/src/lib/daoHausClient.ts index f40f116d8..e6b2deebd 100644 --- a/packages/backend/src/lib/daoHausClient.ts +++ b/packages/backend/src/lib/daoHausClient.ts @@ -1,8 +1,15 @@ import { GraphQLClient } from 'graphql-request'; import { CONFIG } from '../config'; -import { getSdk } from './autogen/daohaus-sdk'; +import { getSdk, Sdk } from './autogen/daohaus-sdk'; -export const daoHausClient = getSdk( - new GraphQLClient(CONFIG.daoHausGraphqlURL), -); +export function clientFactory(chain: string): Sdk { + switch (chain) { + case 'polygon': + return getSdk(new GraphQLClient(CONFIG.daoHausPolygonGraphqlURL)); + case 'xdai': + return getSdk(new GraphQLClient(CONFIG.daoHausXdaiGraphqlURL)); + default: + return getSdk(new GraphQLClient(CONFIG.daoHausGraphqlURL)); + } +} diff --git a/schema.graphql b/schema.graphql index 1d1a1d3f5..a20e80466 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2096,6 +2096,7 @@ type Member { } type Moloch { + chain: String! id: ID! summoner: String! title: String