@@ -27,10 +27,14 @@ import (
27
27
)
28
28
29
29
type fakeELReader struct {
30
- roots []rewardscoordinator.IRewardsCoordinatorDistributionRoot
30
+ roots []rewardscoordinator.IRewardsCoordinatorDistributionRoot
31
+ earnerTokenClaimedMap map [common.Address ]map [common.Address ]* big.Int
31
32
}
32
33
33
- func newFakeELReader (now time.Time ) * fakeELReader {
34
+ func newFakeELReader (
35
+ now time.Time ,
36
+ earnerTokenClaimedMap map [common.Address ]map [common.Address ]* big.Int ,
37
+ ) * fakeELReader {
34
38
roots := make ([]rewardscoordinator.IRewardsCoordinatorDistributionRoot , 0 )
35
39
rootOne := rewardscoordinator.IRewardsCoordinatorDistributionRoot {
36
40
Root : [32 ]byte {0x01 },
@@ -60,7 +64,8 @@ func newFakeELReader(now time.Time) *fakeELReader {
60
64
return roots [i ].ActivatedAt < roots [j ].ActivatedAt
61
65
})
62
66
return & fakeELReader {
63
- roots : roots ,
67
+ roots : roots ,
68
+ earnerTokenClaimedMap : earnerTokenClaimedMap ,
64
69
}
65
70
}
66
71
@@ -91,6 +96,21 @@ func (f *fakeELReader) GetCurrentClaimableDistributionRoot(
91
96
return rewardscoordinator.IRewardsCoordinatorDistributionRoot {}, errors .New ("no active distribution root found" )
92
97
}
93
98
99
+ func (f * fakeELReader ) GetCumulativeClaimed (
100
+ ctx context.Context ,
101
+ earnerAddress ,
102
+ tokenAddress common.Address ,
103
+ ) (* big.Int , error ) {
104
+ if f .earnerTokenClaimedMap == nil {
105
+ return big .NewInt (0 ), nil
106
+ }
107
+ claimed , ok := f.earnerTokenClaimedMap [earnerAddress ][tokenAddress ]
108
+ if ! ok {
109
+ return big .NewInt (0 ), nil
110
+ }
111
+ return claimed , nil
112
+ }
113
+
94
114
func (f * fakeELReader ) CurrRewardsCalculationEndTimestamp (ctx context.Context ) (uint32 , error ) {
95
115
rootLen , err := f .GetDistributionRootsLength (ctx )
96
116
if err != nil {
@@ -246,7 +266,7 @@ func TestGetClaimDistributionRoot(t *testing.T) {
246
266
},
247
267
}
248
268
249
- reader := newFakeELReader (now )
269
+ reader := newFakeELReader (now , nil )
250
270
logger := logging .NewJsonSLogger (os .Stdout , & logging.SLoggerOptions {})
251
271
252
272
for _ , tt := range tests {
@@ -280,13 +300,18 @@ func TestGetTokensToClaim(t *testing.T) {
280
300
281
301
// Case 1: No token addresses provided, should return all addresses in claimableTokens
282
302
result := getTokensToClaim (claimableTokens , []common.Address {})
283
- expected := []common.Address {addr1 , addr2 }
284
- assert .ElementsMatch (t , result , expected )
303
+ expected := map [common.Address ]* big.Int {
304
+ addr1 : big .NewInt (100 ),
305
+ addr2 : big .NewInt (200 ),
306
+ }
307
+ assert .Equal (t , result , expected )
285
308
286
309
// Case 2: Provided token addresses, should return only those present in claimableTokens
287
310
result = getTokensToClaim (claimableTokens , []common.Address {addr2 , addr3 })
288
- expected = []common.Address {addr2 }
289
- assert .ElementsMatch (t , result , expected )
311
+ expected = map [common.Address ]* big.Int {
312
+ addr2 : big .NewInt (200 ),
313
+ }
314
+ assert .Equal (t , result , expected )
290
315
}
291
316
292
317
func TestGetTokenAddresses (t * testing.T ) {
@@ -300,8 +325,11 @@ func TestGetTokenAddresses(t *testing.T) {
300
325
301
326
// Test that the function returns all addresses in the map
302
327
result := getAllClaimableTokenAddresses (addressesMap )
303
- expected := []common.Address {addr1 , addr2 }
304
- assert .ElementsMatch (t , result , expected )
328
+ expected := map [common.Address ]* big.Int {
329
+ addr1 : big .NewInt (100 ),
330
+ addr2 : big .NewInt (200 ),
331
+ }
332
+ assert .Equal (t , result , expected )
305
333
}
306
334
307
335
func TestFilterClaimableTokenAddresses (t * testing.T ) {
@@ -321,8 +349,70 @@ func TestFilterClaimableTokenAddresses(t *testing.T) {
321
349
}
322
350
323
351
result := filterClaimableTokenAddresses (addressesMap , providedAddresses )
324
- expected := []common.Address {addr1 }
325
- assert .ElementsMatch (t , result , expected )
352
+ expected := map [common.Address ]* big.Int {
353
+ addr1 : big .NewInt (100 ),
354
+ }
355
+ assert .Equal (t , result , expected )
356
+ }
357
+
358
+ func TestFilterClaimableTokens (t * testing.T ) {
359
+ // Set up a mock claimableTokens map
360
+ earnerAddress := common .HexToAddress (testutils .GenerateRandomEthereumAddressString ())
361
+ tokenAddress1 := common .HexToAddress (testutils .GenerateRandomEthereumAddressString ())
362
+ tokenAddress2 := common .HexToAddress (testutils .GenerateRandomEthereumAddressString ())
363
+ amountClaimed1 := big .NewInt (100 )
364
+ amountClaimed2 := big .NewInt (200 )
365
+ elReaderClaimedMap := map [common.Address ]map [common.Address ]* big.Int {
366
+ earnerAddress : {
367
+ tokenAddress1 : amountClaimed1 ,
368
+ tokenAddress2 : amountClaimed2 ,
369
+ },
370
+ }
371
+ now := time .Now ()
372
+ reader := newFakeELReader (now , elReaderClaimedMap )
373
+ tests := []struct {
374
+ name string
375
+ earnerAddress common.Address
376
+ claimableTokensMap map [common.Address ]* big.Int
377
+ expectedClaimableTokens []common.Address
378
+ }{
379
+ {
380
+ name : "all tokens are claimable and non zero" ,
381
+ earnerAddress : earnerAddress ,
382
+ claimableTokensMap : map [common.Address ]* big.Int {
383
+ tokenAddress1 : big .NewInt (2345 ),
384
+ tokenAddress2 : big .NewInt (3345 ),
385
+ },
386
+ expectedClaimableTokens : []common.Address {
387
+ tokenAddress1 ,
388
+ tokenAddress2 ,
389
+ },
390
+ },
391
+ {
392
+ name : "one token is already claimed" ,
393
+ earnerAddress : earnerAddress ,
394
+ claimableTokensMap : map [common.Address ]* big.Int {
395
+ tokenAddress1 : amountClaimed1 ,
396
+ tokenAddress2 : big .NewInt (1234 ),
397
+ },
398
+ expectedClaimableTokens : []common.Address {
399
+ tokenAddress2 ,
400
+ },
401
+ },
402
+ }
403
+
404
+ for _ , tt := range tests {
405
+ t .Run (tt .name , func (t * testing.T ) {
406
+ result , err := filterClaimableTokens (
407
+ context .Background (),
408
+ reader ,
409
+ tt .earnerAddress ,
410
+ tt .claimableTokensMap ,
411
+ )
412
+ assert .NoError (t , err )
413
+ assert .ElementsMatch (t , tt .expectedClaimableTokens , result )
414
+ })
415
+ }
326
416
}
327
417
328
418
func newBigInt (value int64 ) * distribution.BigInt {
0 commit comments