Skip to content

Commit 0e42097

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

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

packages/grant-explorer/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@wagmi/core": "0.10.16",
6161
"@walletconnect/modal": "^2.5.9",
6262
"allo-indexer": "file:/Users/vacekj/Programming/allo-indexer",
63-
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#6220c4a7f6c7770117380581b2d0afb9abf0a4eb",
63+
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#035babb80621c67197e39bc98beeb1b457f9f676",
6464
"babel-loader": "^8.3.0",
6565
"buffer": "^6.0.3",
6666
"common": "workspace:*",

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

+42-33
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@ import { BigNumber, ethers } from "ethers";
22
import React, { useMemo } from "react";
33
import { DonationInput, PayoutToken } from "../../api/types";
44
import useSWR from "swr";
5-
import { Client } from "allo-indexer-client";
5+
import { Client, MatchingEstimate } from "allo-indexer-client";
66
import { useParams } from "react-router-dom";
77
import { useAccount, useNetwork } from "wagmi";
8+
import { getAddress } from "viem";
89

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-
}
10+
const replacer = (key: string, value: any) =>
11+
typeof value === "bigint" ? value.toString() : value;
2012

2113
export function useMatchingEstimates(
2214
roundId: string,
@@ -27,11 +19,32 @@ export function useMatchingEstimates(
2719
amount: bigint;
2820
}[]
2921
) {
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-
});
22+
return useSWR<MatchingEstimate[]>(
23+
[
24+
roundId,
25+
chainId,
26+
JSON.stringify({ potentialVotes }, replacer),
27+
"/estimates",
28+
],
29+
([roundId, chainId, potentialVotes]) => {
30+
return fetch(
31+
`${
32+
process.env.REACT_APP_ALLO_API_URL
33+
}api/v1/chains/${chainId}/rounds/${getAddress(roundId)}/estimate`,
34+
{
35+
method: "POST",
36+
headers: {
37+
"Content-Type": "application/json",
38+
},
39+
body: potentialVotes,
40+
}
41+
).then((r) => {
42+
if (r.ok) {
43+
return r.json();
44+
}
45+
});
46+
}
47+
);
3548
}
3649

3750
type SummaryBoxProps = {
@@ -50,25 +63,20 @@ export function Summary({
5063
const { chainId, roundId } = useParams();
5164
const { address } = useAccount();
5265

53-
const { data, error, isLoading } = useMatchingEstimates(
66+
const donationsForEstimation = donations.map((donation) => ({
67+
amount: BigInt(donation.amount),
68+
recipient: donation.projectAddress,
69+
contributor: address ?? "",
70+
}));
71+
72+
console.log(donationsForEstimation);
73+
74+
const { data } = useMatchingEstimates(
5475
roundId!,
5576
chainId!,
56-
donations.map((donation) => ({
57-
amount: BigInt(donation.amount),
58-
recipient: donation.projectAddress,
59-
contributor: address ?? "",
60-
}))
77+
donationsForEstimation
6178
);
6279

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-
7280
if (!roundId || !chainId) {
7381
return null;
7482
}
@@ -81,6 +89,7 @@ export function Summary({
8189

8290
const totalMatchingInUSD =
8391
data &&
92+
data.length &&
8493
data
8594
?.filter((estimate) => estimate.difference > 0)
8695
.map((estimate) => estimate.difference)
@@ -94,7 +103,7 @@ export function Summary({
94103
<p>
95104
<span data-testid={"totalDonation"} className="mr-2">
96105
{ethers.utils.formatUnits(
97-
BigNumber.from(totalMatchingInUSD),
106+
BigNumber.from(totalMatchingInUSD ?? 0),
98107
selectedPayoutToken.decimal
99108
)}
100109
</span>

pnpm-lock.yaml

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)