Skip to content

Commit

Permalink
feat: matching estimates hook
Browse files Browse the repository at this point in the history
  • Loading branch information
vacekj committed Jul 26, 2023
1 parent ee07ec1 commit 0e42097
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/grant-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@wagmi/core": "0.10.16",
"@walletconnect/modal": "^2.5.9",
"allo-indexer": "file:/Users/vacekj/Programming/allo-indexer",
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#6220c4a7f6c7770117380581b2d0afb9abf0a4eb",
"allo-indexer-client": "github:gitcoinco/allo-indexer-client#035babb80621c67197e39bc98beeb1b457f9f676",
"babel-loader": "^8.3.0",
"buffer": "^6.0.3",
"common": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ import { BigNumber, ethers } from "ethers";
import React, { useMemo } from "react";
import { DonationInput, PayoutToken } from "../../api/types";
import useSWR from "swr";
import { Client } from "allo-indexer-client";
import { Client, MatchingEstimate } from "allo-indexer-client";
import { useParams } from "react-router-dom";
import { useAccount, useNetwork } from "wagmi";
import { getAddress } from "viem";

export function useAlloIndexerClient(): Client {
const { chain } = useNetwork();

return useMemo(() => {
return new Client(
fetch.bind(window),
process.env.REACT_APP_ALLO_API_URL ?? "",
chain?.id ?? 1
);
}, [chain?.id]);
}
const replacer = (key: string, value: any) =>
typeof value === "bigint" ? value.toString() : value;

export function useMatchingEstimates(
roundId: string,
Expand All @@ -27,11 +19,32 @@ export function useMatchingEstimates(
amount: bigint;
}[]
) {
const client = useAlloIndexerClient();
console.log(client, roundId, chainId, potentialVotes);
return useSWR([roundId, chainId, "/estimates"], ([roundId]) => {
return client.getMatchingEstimations(roundId, chainId, potentialVotes);
});
return useSWR<MatchingEstimate[]>(
[
roundId,
chainId,
JSON.stringify({ potentialVotes }, replacer),
"/estimates",
],
([roundId, chainId, potentialVotes]) => {
return fetch(
`${
process.env.REACT_APP_ALLO_API_URL
}api/v1/chains/${chainId}/rounds/${getAddress(roundId)}/estimate`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: potentialVotes,
}
).then((r) => {
if (r.ok) {
return r.json();
}
});
}
);
}

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

const { data, error, isLoading } = useMatchingEstimates(
const donationsForEstimation = donations.map((donation) => ({
amount: BigInt(donation.amount),
recipient: donation.projectAddress,
contributor: address ?? "",
}));

console.log(donationsForEstimation);

const { data } = useMatchingEstimates(
roundId!,
chainId!,
donations.map((donation) => ({
amount: BigInt(donation.amount),
recipient: donation.projectAddress,
contributor: address ?? "",
}))
donationsForEstimation
);

console.log("loading", isLoading);
if (error) {
console.log(
"error",
JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
console.log("data", data);

if (!roundId || !chainId) {
return null;
}
Expand All @@ -81,6 +89,7 @@ export function Summary({

const totalMatchingInUSD =
data &&
data.length &&
data
?.filter((estimate) => estimate.difference > 0)
.map((estimate) => estimate.difference)
Expand All @@ -94,7 +103,7 @@ export function Summary({
<p>
<span data-testid={"totalDonation"} className="mr-2">
{ethers.utils.formatUnits(
BigNumber.from(totalMatchingInUSD),
BigNumber.from(totalMatchingInUSD ?? 0),
selectedPayoutToken.decimal
)}
</span>
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e42097

Please sign in to comment.