Skip to content

Commit ee07ec1

Browse files
committed
feat: matching estimates hook
1 parent fc5c549 commit ee07ec1

File tree

7 files changed

+1406
-267
lines changed

7 files changed

+1406
-267
lines changed

packages/grant-explorer/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"@testing-library/user-event": "^14.1.1",
6060
"@wagmi/core": "0.10.16",
6161
"@walletconnect/modal": "^2.5.9",
62-
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#0549b473941a916b38426b32f59250f87ec3274d",
62+
"allo-indexer": "file:/Users/vacekj/Programming/allo-indexer",
63+
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#6220c4a7f6c7770117380581b2d0afb9abf0a4eb",
6364
"babel-loader": "^8.3.0",
6465
"buffer": "^6.0.3",
6566
"common": "workspace:*",
@@ -95,9 +96,10 @@
9596
"stream-browserify": "^3.0.0",
9697
"stream-http": "^3.2.0",
9798
"swr": "^2.0.0",
98-
"typescript": "^5.1.3",
99+
"typescript": "^5.1.6",
99100
"url": "^0.11.0",
100101
"util": "^0.12.4",
102+
"viem": "^1.4.1",
101103
"wagmi": "0.12.18",
102104
"web-vitals": "^2.1.0",
103105
"webpack": ">=2"

packages/grant-explorer/src/features/common/MatchingEstimations.tsx

-102
This file was deleted.

packages/grant-explorer/src/features/round/ViewCartPage/SummaryBox.tsx

+68-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,91 @@
11
import { BigNumber, ethers } from "ethers";
2-
import React from "react";
3-
import { PayoutToken } from "../../api/types";
2+
import React, { useMemo } from "react";
3+
import { DonationInput, PayoutToken } from "../../api/types";
4+
import useSWR from "swr";
5+
import { Client } from "allo-indexer-client";
6+
import { useParams } from "react-router-dom";
7+
import { useAccount, useNetwork } from "wagmi";
48

5-
function useMatchingEstimation(): {
6-
totalMatchingEstimation: bigint;
7-
} {
8-
return {
9-
totalMatchingEstimation: 0n,
10-
};
9+
export function useAlloIndexerClient(): Client {
10+
const { chain } = useNetwork();
11+
12+
return useMemo(() => {
13+
return new Client(
14+
fetch.bind(window),
15+
process.env.REACT_APP_ALLO_API_URL ?? "",
16+
chain?.id ?? 1
17+
);
18+
}, [chain?.id]);
19+
}
20+
21+
export function useMatchingEstimates(
22+
roundId: string,
23+
chainId: string,
24+
potentialVotes: {
25+
contributor: string;
26+
recipient: string;
27+
amount: bigint;
28+
}[]
29+
) {
30+
const client = useAlloIndexerClient();
31+
console.log(client, roundId, chainId, potentialVotes);
32+
return useSWR([roundId, chainId, "/estimates"], ([roundId]) => {
33+
return client.getMatchingEstimations(roundId, chainId, potentialVotes);
34+
});
1135
}
1236

1337
type SummaryBoxProps = {
1438
payoutTokenPrice: number | undefined;
1539
totalDonation: BigNumber;
1640
selectedPayoutToken: PayoutToken;
41+
donations: DonationInput[];
1742
};
1843

1944
export function Summary({
2045
payoutTokenPrice,
2146
totalDonation,
2247
selectedPayoutToken,
48+
donations,
2349
}: SummaryBoxProps) {
50+
const { chainId, roundId } = useParams();
51+
const { address } = useAccount();
52+
53+
const { data, error, isLoading } = useMatchingEstimates(
54+
roundId!,
55+
chainId!,
56+
donations.map((donation) => ({
57+
amount: BigInt(donation.amount),
58+
recipient: donation.projectAddress,
59+
contributor: address ?? "",
60+
}))
61+
);
62+
63+
console.log("loading", isLoading);
64+
if (error) {
65+
console.log(
66+
"error",
67+
JSON.stringify(error, Object.getOwnPropertyNames(error))
68+
);
69+
}
70+
console.log("data", data);
71+
72+
if (!roundId || !chainId) {
73+
return null;
74+
}
75+
2476
const totalDonationInUSD =
2577
payoutTokenPrice &&
2678
Number(
2779
ethers.utils.formatUnits(totalDonation, selectedPayoutToken.decimal)
2880
) * Number(payoutTokenPrice.toFixed(2));
2981

82+
const totalMatchingInUSD =
83+
data &&
84+
data
85+
?.filter((estimate) => estimate.difference > 0)
86+
.map((estimate) => estimate.difference)
87+
.reduce((acc, curr) => acc + curr) * BigInt(payoutTokenPrice ?? 0);
88+
3089
return (
3190
<div className="shrink mb-5 block px-[16px] py-4 rounded-lg shadow-lg bg-white border border-violet-400 font-semibold">
3291
<h2 className="text-xl border-b-2 pb-2">Summary</h2>
@@ -35,7 +94,7 @@ export function Summary({
3594
<p>
3695
<span data-testid={"totalDonation"} className="mr-2">
3796
{ethers.utils.formatUnits(
38-
totalDonation,
97+
BigNumber.from(totalMatchingInUSD),
3998
selectedPayoutToken.decimal
4099
)}
41100
</span>

packages/grant-explorer/src/features/round/ViewCartPage/ViewCartPage.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export default function ViewCart() {
318318
payoutTokenPrice={payoutTokenPrice}
319319
selectedPayoutToken={selectedPayoutToken}
320320
totalDonation={totalDonation}
321+
donations={donations}
321322
/>
322323
<Button
323324
$variant="solid"

packages/grant-explorer/src/features/round/ViewProjectDetails.tsx

-9
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import PassportBanner from "../common/PassportBanner";
3232
import { ProjectBanner } from "../common/ProjectBanner";
3333
import RoundEndedBanner from "../common/RoundEndedBanner";
3434
import Breadcrumb, { BreadcrumbItem } from "../common/Breadcrumb";
35-
import { linearQF } from "pluralistic";
36-
import { MatchingEstimations } from "../common/MatchingEstimations";
3735

3836
const CalendarIcon = (props: React.SVGProps<SVGSVGElement>) => {
3937
return (
@@ -550,13 +548,6 @@ export function ProjectStats() {
550548
"rounded bg-gray-50 mb-4 p-4 gap-4 grid grid-cols-3 md:flex md:flex-col"
551549
}
552550
>
553-
{projectToRender && (
554-
<MatchingEstimations
555-
projectId={projectToRender.grantApplicationId}
556-
roundId={roundId as string}
557-
recipient={projectToRender.recipient}
558-
/>
559-
)}
560551
<div>
561552
<h3>${application?.amountUSD.toFixed(2) ?? "-"}</h3>
562553
<p>funding received in current round</p>

packages/round-manager/src/hooks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client } from "allo-indexer-client";
33
import { useWallet } from "./features/common/Auth";
44
import { useMemo } from "react";
55
import { useSearchParams } from "react-router-dom";
6+
import { ChainId } from "common";
67

78
export function useDebugMode(): boolean {
89
const [searchParams] = useSearchParams();

0 commit comments

Comments
 (0)