Skip to content

Commit

Permalink
community state from thegraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira committed Feb 22, 2022
1 parent 2f91c1e commit 42fdc05
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 73 deletions.
92 changes: 21 additions & 71 deletions packages/core/src/services/ubi/community.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import {
Op,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -754,7 +758,7 @@ export default class CommunityService {
);
return calldata[0][0];
});

return requestByAddress;
}

Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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',
});
Expand Down
42 changes: 40 additions & 2 deletions packages/core/src/subgraph/queries/community.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> => {
try {
Expand Down Expand Up @@ -39,3 +39,41 @@ export const getCommunityProposal = async (): Promise<string[]> => {
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);
}
};

0 comments on commit 42fdc05

Please sign in to comment.