@@ -15,28 +15,54 @@ const supabase = createClient(supabaseUrl, supabaseKey);
1515
1616app . use ( express . json ( ) ) ;
1717
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 ) => {
2020 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+
2235 const { data, error } = await supabase
2336 . from ( 'accrue_rewards_events' )
2437 . select ( '*' )
2538 . eq ( 'user' , user . toLowerCase ( ) )
39+ . eq ( 'chain' , chain . toLowerCase ( ) ) // Add chain filter
2640 . order ( 'timestamp' , { ascending : false } ) ;
2741
2842 if ( error ) throw error ;
2943
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 ) ) ;
3558
3659 res . json ( {
60+ chain : chain . toLowerCase ( ) ,
3761 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,
4066 } ) ;
4167 } catch ( error ) {
4268 console . error ( 'Error fetching user rewards:' , error ) ;
0 commit comments