Skip to content

Show direct donations on builder #3571

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

Merged
merged 2 commits into from
Aug 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function StatCard({
p={3}
className={`${
bg ? `bg-${bg}` : ""
} border-grey-100 mx-2 mt-2 sm:table-row md:table-cell`}
} border-grey-100 mx-2 sm:table-row md:table-cell`}
borderWidth={border ? "1px" : "0px"}
borderRadius="md"
minWidth="193px"
Expand Down
142 changes: 102 additions & 40 deletions packages/builder/src/components/grants/stats/Stats.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Spinner } from "@chakra-ui/react";
import { BaseDonorValues, useDataLayer } from "data-layer";
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { useParams } from "react-router-dom";
import { RootState } from "../../../reducers";
import { ProjectStats } from "../../../reducers/projects";
import RoundDetailsCard from "./RoundDetailsCard";
import StatCard from "./StatCard";
import { allChains } from "../../../utils/wagmi";

export const slugify = (input: string): string =>
input
Expand All @@ -19,9 +21,12 @@ export default function RoundStats() {
const NAText = "N/A";

const params = useParams();
const dataLayer = useDataLayer();
const [details, setDetails] = useState<any>([]);
const [allTimeStats, setAllTimeStats] = useState<any>({
allTimeReceived: 0,
totalCrowdFunding: 0,
totalDirectDonations: 0,
allTimeContributions: 0,
roundsLength: 0,
});
Expand All @@ -41,57 +46,101 @@ export default function RoundStats() {
});

useEffect(() => {
const detailsTmp: any[] = [];
let allTime = {
allTimeReceived: 0,
allTimeContributions: 0,
roundsLength: 0,
};
const fetch = async () => {
const detailsTmp: any[] = [];
let allTime = {
allTimeReceived: 0,
totalCrowdFunding: 0,
totalDirectDonations: 0,
allTimeContributions: 0,
roundsLength: 0,
};

let totalDirectDonations = 0;
let totalDirectDonationCount = 0;

if (props.stats?.length > 0) {
props.stats.forEach((stat) => {
allTime = {
allTimeReceived:
allTime.allTimeReceived + (stat.success ? stat.fundingReceived : 0),
allTimeContributions:
allTime.allTimeContributions +
(stat.success ? stat.totalContributions : 0),
roundsLength: props.stats.length,
};

const newStat = { ...stat };
if (newStat.uniqueContributors > 0 || newStat.totalContributions > 0) {
if (newStat.fundingReceived === 0) newStat.fundingReceived = NA;
if (newStat.avgContribution === 0) newStat.avgContribution = NA;
}

if (props.rounds[stat.roundId]?.round?.programName) {
detailsTmp.push({
round: props.rounds[stat.roundId].round,
stats: { ...newStat },
try {
const directDonations: BaseDonorValues[] =
await dataLayer.getDirectDonationsByProjectId({
projectId: props.projectID,
chainIds: allChains.map((chain) => chain.id),
});
}
});
}

setAllTimeStats(allTime);
setDetails(detailsTmp);
}, [props.stats, props.rounds]);
directDonations.forEach((donation) => {
totalDirectDonations += donation.totalAmountDonatedInUsd;
totalDirectDonationCount += donation.totalDonationsCount;
});
} catch (e) {
console.error("Error fetching direct donations", e);
}

if (props.stats?.length > 0) {
props.stats.forEach((stat) => {
allTime = {
...allTime,
totalCrowdFunding:
allTime.totalCrowdFunding +
(stat.success ? stat.fundingReceived : 0),
allTimeContributions:
allTime.allTimeContributions +
(stat.success ? stat.totalContributions : 0),
roundsLength: props.stats.length,
};

const newStat = { ...stat };
if (
newStat.uniqueContributors > 0 ||
newStat.totalContributions > 0
) {
if (newStat.fundingReceived === 0) newStat.fundingReceived = NA;
if (newStat.avgContribution === 0) newStat.avgContribution = NA;
}

if (props.rounds[stat.roundId]?.round?.programName) {
detailsTmp.push({
round: props.rounds[stat.roundId].round,
stats: { ...newStat },
});
}
});
}

allTime.totalDirectDonations = totalDirectDonations;
allTime.allTimeReceived =
allTime.totalCrowdFunding + totalDirectDonations;
allTime.allTimeContributions += totalDirectDonationCount;

setAllTimeStats(allTime);
setDetails(detailsTmp);
};
if (props.projectID) fetch();
}, [props.stats, props.rounds, props.projectID, dataLayer]);

const section = (
description: any,
container: any,
pt: boolean,
key: string
key: string,
shortRow?: boolean
) => (
<div
key={key}
className={`grid md:grid-cols-7 sm:grid-cols-1 border-b border-gitcoin-grey-100 pb-10 ${
pt && "pt-10"
}`}
className={`grid ${
shortRow ? "md:grid-cols-8" : "md:grid-cols-7"
} sm:grid-cols-1 border-b border-gitcoin-grey-100 pb-10 ${pt && "pt-10"}`}
>
<div className="md:col-span-2">{description}</div>
<div className="md:col-span-4 sm:col-span-1 md:flex space-between">
<div
className={`${
shortRow ? "md:col-span-2" : "md:col-span-1"
} flex items-center justify-start`}
>
{description}
</div>
<div
className={`${
shortRow ? "md:col-span-5" : "md:col-span-6"
} sm:col-span-1 md:flex space-between`}
>
{container}
</div>
<div className="md:col-span-1 sm:col-span-1" />
Expand Down Expand Up @@ -131,6 +180,18 @@ export default function RoundStats() {
bg="gitcoin-violet-100"
tooltip="The estimated funding received by this project. This number is not final and may change based on updated data."
/>
<StatCard
heading="Est. Crowdfunding Received"
value={`$${allTimeStats.totalCrowdFunding.toFixed(2)}`}
bg="gitcoin-violet-100"
tooltip="The number of rounds this project has participated in."
/>
<StatCard
heading="Total Direct Donations"
value={`$${allTimeStats.totalDirectDonations.toFixed(2)}`}
bg="gitcoin-violet-100"
tooltip="The amount of contributions this project has received as direct donations."
/>
<StatCard
heading="No. of Contributions"
value={allTimeStats.allTimeContributions}
Expand Down Expand Up @@ -193,7 +254,8 @@ export default function RoundStats() {
/>
</>,
true,
`details-${index}`
`details-${index}`,
true
)
)}
</>
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/utils/wagmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { providers } from "ethers";
import { type Account, type Chain, type Client, type Transport } from "viem";
import { Connector } from "wagmi";

const allChains: RChain[] =
export const allChains: RChain[] =
process.env.REACT_APP_ENV === "development" ? allNetworks : mainnetNetworks;

/* TODO: remove hardcoded value once we have environment variables validation */
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/allo/backends/allo-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ export function getDirectAllocationPoolId(chainId: number) {
return 36;
case 43114:
return 15;
case 534352:
case 534353:
return 22;
case 250:
return 4;
case 1:
return 11;
case 1329:
case 808:
return 8;
case 42:
Expand Down Expand Up @@ -130,12 +132,16 @@ export function getDirectAllocationStrategyAddress(chainId: number) {
return "0x86b4329E7CB8674b015477C81356420D79c71A53";
case 534353:
return "0x56662F9c0174cD6ae14b214fC52Bd6Eb6B6eA602";
case 534352:
return "0x9da0a7978b7bd826e06800427cbf1ec1200393e3";
case 250:
return "0x1E18cdce56B3754c4Dca34CB3a7439C24E8363de";
case 1:
return "0x56662F9c0174cD6ae14b214fC52Bd6Eb6B6eA602";
case 808:
return "0x1cfa7A687cd18b99D255bFc25930d3a0b05EB00F";
case 1329:
return "0x7836f59bd6dc1d87a45df8b9a74eefcdf25bc8a9";
case 42:
return "0xeB6325d9daCD1E46A20C02F46E41d4CAE45C0980";
case 1088:
Expand Down
17 changes: 17 additions & 0 deletions packages/data-layer/src/data-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ExpandedApplicationRef,
RoundApplicationPayout,
ProjectApplicationWithRoundAndProgram,
BaseDonorValues,
} from "./data.types";
import {
ApplicationSummary,
Expand Down Expand Up @@ -56,6 +57,7 @@ import {
getPaginatedProjects,
getProjectsBySearchTerm,
getRoundsForManagerByAddress,
getDirectDonationsByProjectId,
} from "./queries";
import { mergeCanonicalAndLinkedProjects } from "./utils";

Expand Down Expand Up @@ -959,4 +961,19 @@ export class DataLayer {
): Promise<SearchBasedProjectCategory | null> {
return await categories.getSearchBasedCategoryById(id);
}

async getDirectDonationsByProjectId({
projectId,
chainIds,
}: {
projectId: string;
chainIds: number[];
}): Promise<BaseDonorValues[]> {
const response: { rounds: BaseDonorValues[] } = await request(
this.gsIndexerEndpoint,
getDirectDonationsByProjectId,
{ projectId, chainIds },
);
return response.rounds;
}
}
15 changes: 9 additions & 6 deletions packages/data-layer/src/data.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ export type v2Project = {
* The linked chains to the canonical project
*/
linkedChains?: number[];
qfRounds? : string[];
dgRounds? : string[];
qfRounds?: string[];
dgRounds?: string[];
};

/**
Expand Down Expand Up @@ -257,6 +257,12 @@ export type RoundForExplorer = Omit<RoundGetRound, "applications"> & {
uniqueDonorsCount?: number;
};

export type BaseDonorValues = {
totalAmountDonatedInUsd: number;
totalDonationsCount: number;
uniqueDonorsCount: number;
};

/**
* The project application type for v2
*
Expand All @@ -269,11 +275,8 @@ export type ProjectApplication = {
status: ApplicationStatus;
metadataCid: string;
metadata: ProjectApplicationMetadata;
totalDonationsCount: number;
totalAmountDonatedInUsd: number;
uniqueDonorsCount: number;
distributionTransaction: string | null;
};
} & BaseDonorValues;

export type ProjectApplicationForManager = ProjectApplication & {
anchorAddress: Address;
Expand Down
18 changes: 18 additions & 0 deletions packages/data-layer/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,21 @@ export const getPayoutsByChainIdRoundIdProjectId = gql`
}
}
`;

// 0x4cd0051913234cdd7d165b208851240d334786d6e5afbb4d0eec203515a9c6f3 == DirectDonationsStrategy Id
export const getDirectDonationsByProjectId = gql`
query getDirectDonationsByProjectId($projectId: String!, $chainIds: [Int!]!) {
rounds(
filter: {
strategyId: { equalTo: "0x4cd0051913234cdd7d165b208851240d334786d6e5afbb4d0eec203515a9c6f3" }
chainId: { in: $chainIds }
donations: { every: { projectId: { equalTo: $projectId } } }
}
) {
id
uniqueDonorsCount
totalAmountDonatedInUsd
totalDonationsCount
}
}
`;
Loading