@@ -2,21 +2,13 @@ import { BigNumber, ethers } from "ethers";
2
2
import React , { useMemo } from "react" ;
3
3
import { DonationInput , PayoutToken } from "../../api/types" ;
4
4
import useSWR from "swr" ;
5
- import { Client } from "allo-indexer-client" ;
5
+ import { Client , MatchingEstimate } from "allo-indexer-client" ;
6
6
import { useParams } from "react-router-dom" ;
7
7
import { useAccount , useNetwork } from "wagmi" ;
8
+ import { getAddress } from "viem" ;
8
9
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 ;
20
12
21
13
export function useMatchingEstimates (
22
14
roundId : string ,
@@ -27,11 +19,32 @@ export function useMatchingEstimates(
27
19
amount : bigint ;
28
20
} [ ]
29
21
) {
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
+ ) ;
35
48
}
36
49
37
50
type SummaryBoxProps = {
@@ -50,25 +63,20 @@ export function Summary({
50
63
const { chainId, roundId } = useParams ( ) ;
51
64
const { address } = useAccount ( ) ;
52
65
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 (
54
75
roundId ! ,
55
76
chainId ! ,
56
- donations . map ( ( donation ) => ( {
57
- amount : BigInt ( donation . amount ) ,
58
- recipient : donation . projectAddress ,
59
- contributor : address ?? "" ,
60
- } ) )
77
+ donationsForEstimation
61
78
) ;
62
79
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
80
if ( ! roundId || ! chainId ) {
73
81
return null ;
74
82
}
@@ -81,6 +89,7 @@ export function Summary({
81
89
82
90
const totalMatchingInUSD =
83
91
data &&
92
+ data . length &&
84
93
data
85
94
?. filter ( ( estimate ) => estimate . difference > 0 )
86
95
. map ( ( estimate ) => estimate . difference )
@@ -94,7 +103,7 @@ export function Summary({
94
103
< p >
95
104
< span data-testid = { "totalDonation" } className = "mr-2" >
96
105
{ ethers . utils . formatUnits (
97
- BigNumber . from ( totalMatchingInUSD ) ,
106
+ BigNumber . from ( totalMatchingInUSD ?? 0 ) ,
98
107
selectedPayoutToken . decimal
99
108
) }
100
109
</ span >
0 commit comments