chore(wallet)_: returns last timestamps of successful GetWalletToken updates (#5988)
* chore(wallet)_: returns last timestamps of successful GetWalletToken updates * fix_: return timestamp instead of time * address feedback from the PR
This commit is contained in:
parent
46f2212ea9
commit
f754315023
|
@ -89,6 +89,10 @@ func (api *API) GetWalletToken(ctx context.Context, addresses []common.Address)
|
||||||
return api.reader.GetWalletToken(ctx, clients, addresses, currency)
|
return api.reader.GetWalletToken(ctx, clients, addresses, currency)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *API) GetLastWalletTokenUpdate() map[common.Address]int64 {
|
||||||
|
return api.reader.GetLastTokenUpdateTimestamps()
|
||||||
|
}
|
||||||
|
|
||||||
// GetBalancesByChain return a map with key as chain id and value as map of account address and map of token address and balance
|
// 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
|
// [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) {
|
func (api *API) GetBalancesByChain(ctx context.Context, chainIDs []uint64, addresses, tokens []common.Address) (map[uint64]map[common.Address]map[common.Address]*hexutil.Big, error) {
|
||||||
|
|
|
@ -557,6 +557,22 @@ func (r *Reader) GetWalletToken(ctx context.Context, clients map[uint64]chain.Cl
|
||||||
return result, r.persistence.SaveTokens(result)
|
return result, r.persistence.SaveTokens(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLastTokenUpdateTimestamps returns last timestamps of successful token updates
|
||||||
|
func (r *Reader) GetLastTokenUpdateTimestamps() map[common.Address]int64 {
|
||||||
|
result := make(map[common.Address]int64)
|
||||||
|
|
||||||
|
r.lastWalletTokenUpdateTimestamp.Range(func(key, value interface{}) bool {
|
||||||
|
addr, ok1 := key.(common.Address)
|
||||||
|
timestamp, ok2 := value.(int64)
|
||||||
|
if ok1 && ok2 {
|
||||||
|
result[addr] = timestamp
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func isCachedToken(cachedTokens map[common.Address][]token.StorageToken, address common.Address, symbol string, chainID uint64) bool {
|
func isCachedToken(cachedTokens map[common.Address][]token.StorageToken, address common.Address, symbol string, chainID uint64) bool {
|
||||||
if tokens, ok := cachedTokens[address]; ok {
|
if tokens, ok := cachedTokens[address]; ok {
|
||||||
for _, t := range tokens {
|
for _, t := range tokens {
|
||||||
|
|
|
@ -1061,3 +1061,28 @@ func TestFetchOrGetCachedWalletBalances(t *testing.T) {
|
||||||
_, err := reader.FetchOrGetCachedWalletBalances(context.TODO(), clients, addresses, false)
|
_, err := reader.FetchOrGetCachedWalletBalances(context.TODO(), clients, addresses, false)
|
||||||
require.Error(t, err)
|
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)
|
||||||
|
|
||||||
|
// Call the method to retrieve timestamps.
|
||||||
|
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, timestamp1, timestamps[address1], "Timestamp for address1 does not match")
|
||||||
|
assert.Equal(t, timestamp2, timestamps[address2], "Timestamp for address2 does not match")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue