1
1
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" ;
4
8
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
+ } ) ;
11
35
}
12
36
13
37
type SummaryBoxProps = {
14
38
payoutTokenPrice : number | undefined ;
15
39
totalDonation : BigNumber ;
16
40
selectedPayoutToken : PayoutToken ;
41
+ donations : DonationInput [ ] ;
17
42
} ;
18
43
19
44
export function Summary ( {
20
45
payoutTokenPrice,
21
46
totalDonation,
22
47
selectedPayoutToken,
48
+ donations,
23
49
} : 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
+
24
76
const totalDonationInUSD =
25
77
payoutTokenPrice &&
26
78
Number (
27
79
ethers . utils . formatUnits ( totalDonation , selectedPayoutToken . decimal )
28
80
) * Number ( payoutTokenPrice . toFixed ( 2 ) ) ;
29
81
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
+
30
89
return (
31
90
< div className = "shrink mb-5 block px-[16px] py-4 rounded-lg shadow-lg bg-white border border-violet-400 font-semibold" >
32
91
< h2 className = "text-xl border-b-2 pb-2" > Summary</ h2 >
@@ -35,7 +94,7 @@ export function Summary({
35
94
< p >
36
95
< span data-testid = { "totalDonation" } className = "mr-2" >
37
96
{ ethers . utils . formatUnits (
38
- totalDonation ,
97
+ BigNumber . from ( totalMatchingInUSD ) ,
39
98
selectedPayoutToken . decimal
40
99
) }
41
100
</ span >
0 commit comments