Skip to content

Commit

Permalink
chore(wallet)_: returns last timestamps of successful GetWalletToken …
Browse files Browse the repository at this point in the history
…updates
  • Loading branch information
friofry committed Oct 24, 2024
1 parent c72f491 commit f9f96c9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions services/wallet/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (api *API) GetWalletToken(ctx context.Context, addresses []common.Address)
return api.reader.GetWalletToken(ctx, clients, addresses, currency)
}

func (api *API) GetLastWalletTokenUpdate() map[common.Address]time.Time {
return api.reader.GetLastTokenUpdateTimestamps()

Check warning on line 141 in services/wallet/api.go

View check run for this annotation

Codecov / codecov/patch

services/wallet/api.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}

// GetBalancesByChain return a map with key as chain id and value as map of account address and map of token address and balance
// [chainID][account][token]balance
func (api *API) GetBalancesByChain(ctx context.Context, chainIDs []uint64, addresses, tokens []common.Address) (map[uint64]map[common.Address]map[common.Address]*hexutil.Big, error) {
Expand Down
16 changes: 16 additions & 0 deletions services/wallet/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ func (r *Reader) GetWalletToken(ctx context.Context, clients map[uint64]chain.Cl
return result, r.persistence.SaveTokens(result)
}

// GetLastTokenUpdateTimestamps returns last timestamps of successful token updates
func (r *Reader) GetLastTokenUpdateTimestamps() map[common.Address]time.Time {
result := make(map[common.Address]time.Time)

r.lastWalletTokenUpdateTimestamp.Range(func(key, value interface{}) bool {
addr, ok1 := key.(common.Address)
timestamp, ok2 := value.(int64)
if ok1 && ok2 {
result[addr] = time.Unix(timestamp, 0)
}
return true
})

return result
}

func isCachedToken(cachedTokens map[common.Address][]token.StorageToken, address common.Address, symbol string, chainID uint64) bool {
if tokens, ok := cachedTokens[address]; ok {
for _, t := range tokens {
Expand Down
25 changes: 25 additions & 0 deletions services/wallet/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,28 @@ func TestFetchOrGetCachedWalletBalances(t *testing.T) {
_, err := reader.FetchOrGetCachedWalletBalances(context.TODO(), clients, addresses, false)
require.Error(t, err)
}

// TestGetLastTokenUpdateTimestamps tests the GetLastTokenUpdateTimestamps method.
func TestGetLastTokenUpdateTimestamps(t *testing.T) {
// Setup the Reader and mock dependencies.
reader, _, _, mockCtrl := setupReader(t)
defer mockCtrl.Finish()

// Define test addresses and specific timestamps.
address1 := testAccAddress1
address2 := testAccAddress2
timestamp1 := time.Now().Add(-1 * time.Hour).Unix()
timestamp2 := time.Now().Add(-2 * time.Hour).Unix()

// Store valid timestamps in the Reader's sync.Map.
reader.lastWalletTokenUpdateTimestamp.Store(address1, timestamp1)
reader.lastWalletTokenUpdateTimestamp.Store(address2, timestamp2)

timestamps := reader.GetLastTokenUpdateTimestamps()
require.Len(t, timestamps, 2, "Expected two timestamps in the result map")

// Verify that the retrieved timestamps match the stored values.
assert.Equal(t, time.Unix(timestamp1, 0), timestamps[address1], "Timestamp for address1 does not match")
assert.Equal(t, time.Unix(timestamp2, 0), timestamps[address2], "Timestamp for address2 does not match")

}

0 comments on commit f9f96c9

Please sign in to comment.