@@ -15,28 +15,54 @@ const supabase = createClient(supabaseUrl, supabaseKey);
15
15
16
16
app . use ( express . json ( ) ) ;
17
17
18
- // Example endpoint to fetch rewards for a specific user with total rewards
19
- app . get ( '/api/rewards/:user' , async ( req , res ) => {
18
+ // Updated endpoint to include chain parameter
19
+ app . get ( '/api/rewards/:chain/: user' , async ( req : any , res : any ) => {
20
20
try {
21
- const { user } = req . params ;
21
+ const { chain, user } = req . params ;
22
+ const validChains = [ 'mode' , 'base' ] ; // Add supported chains here
23
+
24
+ // Hardcoded ionTokenAddress for each chain
25
+ const ionTokenAddresses : { [ key : string ] : string } = {
26
+ mode : '0x18470019bF0E94611f15852F7e93cf5D65BC34CA' , // Replace with actual address
27
+ base : '0x3eE5e23eEE121094f1cFc0Ccc79d6C809Ebd22e5' , // Replace with actual address
28
+ } ;
29
+
30
+ // Validate chain parameter
31
+ if ( ! validChains . includes ( chain . toLowerCase ( ) ) ) {
32
+ return res . status ( 400 ) . json ( { error : 'Invalid chain. Supported chains are: ' + validChains . join ( ', ' ) } ) ;
33
+ }
34
+
22
35
const { data, error } = await supabase
23
36
. from ( 'accrue_rewards_events' )
24
37
. select ( '*' )
25
38
. eq ( 'user' , user . toLowerCase ( ) )
39
+ . eq ( 'chain' , chain . toLowerCase ( ) ) // Add chain filter
26
40
. order ( 'timestamp' , { ascending : false } ) ;
27
41
28
42
if ( error ) throw error ;
29
43
30
- // Sum up the amount in each reward event
31
- const totalAmount = data . reduce ( ( acc : bigint , event : any ) => {
32
- const amount = BigInt ( event . allEventArgs . amount ) ; // Convert amount to BigInt
33
- return acc + amount ; // Add the amount to the accumulator
34
- } , BigInt ( 0 ) ) ; // Start with 0 as the initial value for the accumulator
44
+ // Create a Map to track unique events using transactionHash as the key
45
+ const uniqueEvents = new Map ( ) ;
46
+ data . forEach ( ( event : any ) => {
47
+ const eventKey = event . transactionHash ; // Or use: `${event.blockNumber}-${event.logIndex}`
48
+ if ( ! uniqueEvents . has ( eventKey ) ) {
49
+ uniqueEvents . set ( eventKey , event ) ;
50
+ }
51
+ } ) ;
52
+
53
+ // Sum up the amount only for unique events
54
+ const totalAmount = Array . from ( uniqueEvents . values ( ) ) . reduce ( ( acc : bigint , event : any ) => {
55
+ const amount = BigInt ( event . allEventArgs . amount ) ;
56
+ return acc + amount ;
57
+ } , BigInt ( 0 ) ) ;
35
58
36
59
res . json ( {
60
+ chain : chain . toLowerCase ( ) ,
37
61
user : user . toLowerCase ( ) ,
38
- totalRewards : totalAmount . toString ( ) , // Convert BigInt to string to avoid precision issues in JSON
39
- events : data , // Return the original events data as well
62
+ totalRewards : totalAmount . toString ( ) ,
63
+ ionTokenAddress : ionTokenAddresses [ chain . toLowerCase ( ) ] , // Include ionTokenAddress in response
64
+ // uniqueEventsCount: uniqueEvents.size,
65
+ // totalEventsCount: data.length,
40
66
} ) ;
41
67
} catch ( error ) {
42
68
console . error ( 'Error fetching user rewards:' , error ) ;
0 commit comments