From 42fdc055aedcbb39e8d3016bede816b59525b368 Mon Sep 17 00:00:00 2001 From: Bernardo Vieira Date: Tue, 22 Feb 2022 18:53:49 +0000 Subject: [PATCH] community state from thegraph --- packages/core/src/services/ubi/community.ts | 92 +++++-------------- .../core/src/subgraph/queries/community.ts | 42 ++++++++- 2 files changed, 61 insertions(+), 73 deletions(-) diff --git a/packages/core/src/services/ubi/community.ts b/packages/core/src/services/ubi/community.ts index fb607a4e0..1231ed5cc 100644 --- a/packages/core/src/services/ubi/community.ts +++ b/packages/core/src/services/ubi/community.ts @@ -1,3 +1,4 @@ +import { BigNumber } from 'bignumber.js'; import { ethers } from 'ethers'; import { Op, @@ -32,7 +33,10 @@ import { UbiCommunityLabel } from '../../interfaces/ubi/ubiCommunityLabel'; import { UbiCommunityState } from '../../interfaces/ubi/ubiCommunityState'; import { UbiCommunitySuspect } from '../../interfaces/ubi/ubiCommunitySuspect'; import { UbiPromoter } from '../../interfaces/ubi/ubiPromoter'; -import { getCommunityProposal } from '../../subgraph/queries/community'; +import { + getCommunityProposal, + getCommunityState, +} from '../../subgraph/queries/community'; import { BaseError } from '../../utils/baseError'; import { fetchData } from '../../utils/dataFetching'; import { notifyManagerAdded } from '../../utils/util'; @@ -754,7 +758,7 @@ export default class CommunityService { ); return calldata[0][0]; }); - + return requestByAddress; } @@ -1064,81 +1068,25 @@ export default class CommunityService { public static async getState(communityId: number) { const community = await this.community.findOne({ - attributes: ['contractAddress', 'publicId'], + attributes: ['contractAddress'], where: { id: communityId, }, }); + if (!community || !community.contractAddress) { + return null; + } - const communityBackers = await models.inflow.count({ - distinct: true, - col: 'from', - where: { - contractAddress: community?.contractAddress, - }, - }); - - const communityClaimActivity = ( - await this.ubiClaim.findAll({ - attributes: [ - [fn('coalesce', fn('sum', col('amount')), '0'), 'claimed'], - [fn('coalesce', fn('count', col('amount')), '0'), 'claims'], - ], - where: { - communityId, - }, - raw: true, - }) - )[0]; - - const communityInflowActivity = ( - await models.inflow.findAll({ - attributes: [ - [fn('sum', fn('coalesce', col('amount'), 0)), 'amount'], - ], - where: { - contractAddress: community?.contractAddress, - }, - }) - )[0]; - - const communityBeneficiaryActivity = (await this.beneficiary.findAll({ - attributes: [[fn('COUNT', col('address')), 'count'], 'active'], - where: { - communityId, - }, - group: ['active'], - raw: true, - })) as any; - - const communityManagerActivity = await this.manager.count({ - where: { - communityId, - active: true, - }, - }); - - const beneficiaries: { count: string; active: boolean } = - communityBeneficiaryActivity.find((el: any) => el.active); - const removedBeneficiaries: { count: string; active: boolean } = - communityBeneficiaryActivity.find((el: any) => !el.active); + const state = await getCommunityState(community.contractAddress); + const toToken = (value: string) => + new BigNumber(value).multipliedBy(10 ** 18).toString(); return { - claims: communityClaimActivity - ? Number((communityClaimActivity as any).claims) - : 0, - claimed: communityClaimActivity - ? (communityClaimActivity as any).claimed - : '0', - raised: communityInflowActivity.amount - ? communityInflowActivity.amount - : '0', - beneficiaries: beneficiaries ? Number(beneficiaries.count) : 0, - removedBeneficiaries: removedBeneficiaries - ? Number(removedBeneficiaries.count) - : 0, - managers: communityManagerActivity, - backers: communityBackers, + ...state, + // TODO: should be transitional + claimed: toToken(state.claimed), + raised: toToken(state.contributed), + backers: state.contributors, communityId, }; } @@ -2189,7 +2137,9 @@ export default class CommunityService { if (fields.ambassador) { extendedInclude.push({ attributes: - fields.ambassador.length > 0 ? fields.ambassador : undefined, + fields.ambassador.length > 0 + ? fields.ambassador + : undefined, model: models.appUser, as: 'ambassador', }); diff --git a/packages/core/src/subgraph/queries/community.ts b/packages/core/src/subgraph/queries/community.ts index 30afceddf..fad3e3c00 100644 --- a/packages/core/src/subgraph/queries/community.ts +++ b/packages/core/src/subgraph/queries/community.ts @@ -1,8 +1,8 @@ import { gql } from 'apollo-boost'; - -import { client } from '../config'; import { ethers } from 'ethers'; + import config from '../../config'; +import { client } from '../config'; export const getCommunityProposal = async (): Promise => { try { @@ -39,3 +39,41 @@ export const getCommunityProposal = async (): Promise => { throw new Error(error); } }; + +export const getCommunityState = async ( + communityAddress: string +): Promise<{ + claims: number; + claimed: string; + beneficiaries: number; + removedBeneficiaries: number; + contributed: string; + contributors: number; + managers: number; +}> => { + try { + const query = gql` + { + communityEntity( + id: "${communityAddress}" + ) { + claims + claimed + beneficiaries + removedBeneficiaries + contributed + contributors + managers + } + } + `; + + const queryResult = await client.query({ + query, + }); + + return queryResult.data?.communityEntity; + } catch (error) { + throw new Error(error); + } +};