forked from gitcoinco/grants-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SummaryContainer.tsx
441 lines (402 loc) · 14.9 KB
/
SummaryContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { ChainId, getTokenPrice, PassportState } from "common";
import { useCartStorage } from "../../../store";
import React, { useEffect, useMemo, useState } from "react";
import { Summary } from "./Summary";
import ErrorModal from "../../common/ErrorModal";
import ChainConfirmationModal from "../../common/ConfirmationModal";
import { ChainConfirmationModalBody } from "./ChainConfirmationModalBody";
import { ProgressStatus } from "../../api/types";
import { modalDelayMs } from "../../../constants";
import { useNavigate } from "react-router-dom";
import { useAccount, usePublicClient, useWalletClient } from "wagmi";
import { Button } from "common/src/styles";
import { InformationCircleIcon } from "@heroicons/react/24/solid";
import { BoltIcon } from "@heroicons/react/24/outline";
import { getClassForPassportColor, usePassport } from "../../api/passport";
import useSWR from "swr";
import { groupBy, uniqBy } from "lodash-es";
import { getRoundById } from "../../api/round";
import MRCProgressModal from "../../common/MRCProgressModal";
import { MRCProgressModalBody } from "./MRCProgressModalBody";
import { useCheckoutStore } from "../../../checkoutStore";
import { formatUnits, getAddress, parseUnits, zeroAddress } from "viem";
import { useConnectModal } from "@rainbow-me/rainbowkit";
import {
matchingEstimatesToText,
useMatchingEstimates,
} from "../../../hooks/matchingEstimate";
import { Skeleton } from "@chakra-ui/react";
import { MatchingEstimateTooltip } from "../../common/MatchingEstimateTooltip";
import { parseChainId } from "common/src/chains";
export function SummaryContainer() {
const { projects, getVotingTokenForChain, chainToVotingToken } =
useCartStorage();
const { checkout, voteStatus, chainsToCheckout } = useCheckoutStore();
const { openConnectModal } = useConnectModal();
const publicClient = usePublicClient();
const projectsByChain = useMemo(
() => groupBy(projects, "chainId"),
[projects]
);
const { data: rounds } = useSWR(projects, (projects) => {
const uniqueProjects = uniqBy(projects, "roundId");
return Promise.all(
uniqueProjects.map((proj) => getRoundById(proj.roundId, proj.chainId))
);
});
const [clickedSubmit, setClickedSubmit] = useState(false);
useEffect(() => {
clickedSubmit && checkEmptyDonations();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [projects, clickedSubmit]);
/** The ids of the chains that will be checked out */
const [chainIdsBeingCheckedOut, setChainIdsBeingCheckedOut] = useState<
ChainId[]
>(Object.keys(projectsByChain).map(Number));
/** Keep the chains to be checked out in sync with the projects in the cart */
useEffect(() => {
const chainIdsFromProjects = Object.keys(projectsByChain).map(Number);
if (chainIdsFromProjects.length < chainIdsBeingCheckedOut.length) {
setChainIdsBeingCheckedOut(chainIdsFromProjects);
}
}, [chainIdsBeingCheckedOut, projectsByChain]);
/** The ID of the current chain (from wallet) */
const { data: walletClient } = useWalletClient();
/** We find the round that ends last, and take its end date as the permit deadline */
const currentPermitDeadline =
rounds && rounds.length > 0
? [...rounds]
.sort(
(a, b) => a.roundEndTime.getTime() - b.roundEndTime.getTime()
)[0]
.roundEndTime.getTime()
: 0;
const totalDonationsPerChain = useMemo(() => {
return Object.fromEntries(
Object.entries(projectsByChain).map(([key, value]) => [
parseChainId(key),
value
.map((project) => project.amount)
.reduce(
(acc, amount) =>
acc +
parseUnits(
amount ? amount : "0",
getVotingTokenForChain(parseChainId(key)).decimal
),
0n
),
])
);
/* NB: we want to update the totalDonationsPerChain value based on chainToVotingToken */
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getVotingTokenForChain, chainToVotingToken, projectsByChain]);
const navigate = useNavigate();
const { address, isConnected } = useAccount();
const [emptyInput, setEmptyInput] = useState(false);
const [openChainConfirmationModal, setOpenChainConfirmationModal] =
useState(false);
const [openMRCProgressModal, setOpenMRCProgressModal] = useState(false);
/* Donate without matching warning modal */
const [donateWarningModalOpen, setDonateWarningModalOpen] = useState(false);
useEffect(() => {
/* Check if all chains that were meant to be checked out were succesful */
const success = chainsToCheckout
.map((chain) => voteStatus[chain])
.every((status) => status === ProgressStatus.IS_SUCCESS);
/* Redirect to thank you page */
if (success && chainsToCheckout.length > 0) {
navigate("/thankyou");
}
}, [chainsToCheckout, navigate, voteStatus]);
function checkEmptyDonations() {
const emptyDonations = projects.filter(
(project) => !project.amount || Number(project.amount) === 0
);
if (emptyDonations.length > 0) {
setEmptyInput(true);
return true;
} else {
setEmptyInput(false);
return false;
}
}
function handleConfirmation() {
const emptyDonations = checkEmptyDonations();
setClickedSubmit(true);
if (emptyDonations) {
return;
}
// TODO: check if signer has enough token balance for the current round
// const accountBalance = rounds.find();
//
// if (!accountBalance || totalDonation.gt(accountBalance)) {
// setInsufficientBalance(true);
// return;
// }
setOpenChainConfirmationModal(true);
}
function PayoutModals() {
return (
<>
<ChainConfirmationModal
title={"Checkout"}
confirmButtonText={"Checkout"}
confirmButtonAction={handleSubmitDonation}
body={
<ChainConfirmationModalBody
projectsByChain={projectsByChain}
totalDonationsPerChain={totalDonationsPerChain}
chainIdsBeingCheckedOut={chainIdsBeingCheckedOut}
setChainIdsBeingCheckedOut={setChainIdsBeingCheckedOut}
/>
}
isOpen={openChainConfirmationModal}
setIsOpen={setOpenChainConfirmationModal}
disabled={chainIdsBeingCheckedOut.length === 0}
/>
<MRCProgressModal
isOpen={openMRCProgressModal}
subheading={"Please hold while we submit your donation."}
body={
<MRCProgressModalBody
chainIdsBeingCheckedOut={chainIdsBeingCheckedOut}
tryAgainFn={handleSubmitDonation}
setIsOpen={setOpenMRCProgressModal}
/>
}
/>
{/*Passport not connected warning modal*/}
<ErrorModal
isOpen={donateWarningModalOpen}
setIsOpen={setDonateWarningModalOpen}
onDone={() => {
setDonateWarningModalOpen(false);
handleConfirmation();
}}
tryAgainText={"Go to Passport"}
doneText={"Donate without matching"}
onTryAgain={() => {
window.location.href = "https://passport.gitcoin.co";
}}
heading={`Don’t miss out on getting your donations matched!`}
subheading={
<>
<p className={"text-sm text-grey-400 mb-2"}>
Verify your identity with Gitcoin Passport to amplify your
donations.
</p>
<p className={"text-sm text-grey-400"}>
Note that donations made without Gitcoin Passport verification
will not be matched.
</p>
</>
}
closeOnBackgroundClick={true}
/>
</>
);
}
async function handleSubmitDonation() {
try {
if (!walletClient) {
return;
}
setTimeout(() => {
setOpenMRCProgressModal(true);
setOpenChainConfirmationModal(false);
}, modalDelayMs);
await checkout(
chainIdsBeingCheckedOut.map((chainId) => ({
chainId,
permitDeadline: currentPermitDeadline,
})),
walletClient,
publicClient
);
} catch (error) {
console.error(error);
}
}
const { passportColor, passportScore, passportState } = usePassport({
address: address ?? "",
});
const passportTextClass = getClassForPassportColor(passportColor ?? "gray");
const [totalDonationAcrossChainsInUSD, setTotalDonationAcrossChainsInUSD] =
useState<number | undefined>();
const { data: totalDonationAcrossChainsInUSDData } = useSWR(
totalDonationsPerChain,
(totalDonationsPerChain) => {
return Promise.all(
Object.keys(totalDonationsPerChain).map((chainId) =>
getTokenPrice(
getVotingTokenForChain(parseChainId(chainId)).redstoneTokenId
).then((price) => {
return (
Number(
formatUnits(
totalDonationsPerChain[chainId],
getVotingTokenForChain(parseChainId(chainId)).decimal
)
) * Number(price)
);
})
)
);
}
);
/*TODO: this can be a variable */
useEffect(() => {
if (totalDonationAcrossChainsInUSDData) {
setTotalDonationAcrossChainsInUSD(
totalDonationAcrossChainsInUSDData.reduce((acc, curr) => acc + curr, 0)
);
}
}, [totalDonationAcrossChainsInUSDData]);
/* Matching estimates are calculated per-round */
const matchingEstimateParamsPerRound =
rounds?.map((round) => {
const projectFromRound = projects.find(
(project) => project.roundId === round.id
);
return {
roundId: getAddress(round.id ?? zeroAddress),
chainId: projectFromRound?.chainId ?? ChainId.MAINNET,
potentialVotes: projects
.filter((proj) => proj.roundId === round.id)
.map((proj) => ({
amount: parseUnits(
proj.amount ?? "0",
getVotingTokenForChain(parseChainId(proj.chainId)).decimal ?? 18
),
grantAddress: proj.recipient,
voter: address ?? zeroAddress,
token: getVotingTokenForChain(
parseChainId(proj.chainId)
).address.toLowerCase(),
projectId: proj.projectRegistryId,
applicationId: proj.grantApplicationId,
roundId: getAddress(round.id ?? zeroAddress),
})),
};
}) ?? [];
const {
data,
error: matchingEstimateError,
isLoading: matchingEstimateLoading,
} = useMatchingEstimates(matchingEstimateParamsPerRound);
const matchingEstimates = data?.length && data.length > 0 ? data : undefined;
const estimateText = matchingEstimatesToText(matchingEstimates);
if (projects.length === 0) {
return null;
}
return (
<div className="mb-5 block px-[16px] py-4 rounded-lg shadow-lg bg-white border border-violet-400 font-semibold sticky top-20">
<h2 className="text-xl border-b-2 pb-2">Summary</h2>
<div
className={`flex flex-row items-center justify-between mt-4 font-semibold italic ${passportTextClass}`}
>
{matchingEstimateError === undefined &&
matchingEstimates !== undefined && (
<>
<div className="flex flex-row mt-4 items-center">
<p>Estimated match</p>
<MatchingEstimateTooltip
isEligible={
passportScore !== undefined && passportScore >= 15
}
/>
</div>
<div className="flex justify-end mt-4">
<Skeleton isLoaded={!matchingEstimateLoading}>
<p>
<BoltIcon className={"w-4 h-4 inline"} />
~$
{estimateText}
</p>
</Skeleton>
</div>
</>
)}
</div>
<div>
{Object.keys(projectsByChain).map((chainId) => (
<Summary
key={chainId}
chainId={parseChainId(chainId)}
selectedPayoutToken={getVotingTokenForChain(parseChainId(chainId))}
totalDonation={totalDonationsPerChain[chainId]}
/>
))}
{totalDonationAcrossChainsInUSD &&
totalDonationAcrossChainsInUSD > 0 ? (
<div className="flex flex-row justify-between mt-4 border-t-2">
<div className="flex flex-col mt-4">
<p className="mb-2">Your total contribution</p>
</div>
<div className="flex justify-end mt-4">
<p>$ {totalDonationAcrossChainsInUSD?.toFixed(2)}</p>
</div>
</div>
) : null}
<Button
$variant="solid"
data-testid="handle-confirmation"
type="button"
onClick={() => {
/* If wallet is not connected, display Rainbowkit modal */
if (!isConnected) {
openConnectModal?.();
return;
}
/* Check if user hasn't connected passport yet, display the warning modal */
if (
passportState === PassportState.ERROR ||
passportState === PassportState.NOT_CONNECTED ||
passportState === PassportState.INVALID_PASSPORT
) {
setDonateWarningModalOpen(true);
return;
}
/* If passport is fine, proceed straight to confirmation */
handleConfirmation();
}}
className="items-center shadow-sm text-sm rounded w-full mt-4"
>
{isConnected ? "Submit your donation!" : "Connect wallet to continue"}
</Button>
{/*{round.round?.roundMetadata?.quadraticFundingConfig*/}
{/* ?.minDonationThresholdAmount && (*/}
{/* <p className="flex justify-center my-4 text-sm italic">*/}
{/* Your donation to each project must be valued at{" "}*/}
{/* {*/}
{/* round.round?.roundMetadata?.quadraticFundingConfig*/}
{/* ?.minDonationThresholdAmount*/}
{/* }{" "}*/}
{/* USD or more to be eligible for matching.*/}
{/* </p>*/}
{/*)}*/}
{emptyInput && (
<p
data-testid="emptyInput"
className="rounded-md bg-red-50 py-2 text-pink-500 flex justify-center my-4 text-sm"
>
<InformationCircleIcon className="w-4 h-4 mr-1 mt-0.5" />
<span>You must enter donations for all the projects</span>
</p>
)}
{/*TODO: insufficient balance check*/}
{false && (
<p
data-testid="insufficientBalance"
className="rounded-md bg-red-50 py-2 text-pink-500 flex justify-center my-4 text-sm"
>
<InformationCircleIcon className="w-4 h-4 mr-1 mt-0.5" />
<span>You do not have enough funds for these donations</span>
</p>
)}
</div>
<PayoutModals />
</div>
);
}