Skip to content

Commit a5850b6

Browse files
committed
chore: improvements on getting cached balances
Instead of checking all tokens when fetching balances only relevant tokens are processed.
1 parent 82299f4 commit a5850b6

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

services/wallet/reader.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"github.com/bep/debounce"
1616

17+
"golang.org/x/exp/maps"
18+
1719
"github.com/status-im/go-wallet-sdk/pkg/tokens/types"
1820

1921
"github.com/ethereum/go-ethereum/common"
@@ -354,12 +356,19 @@ func (r *Reader) GetCachedBalances(chainIDs []uint64, addresses []common.Address
354356
return nil, err
355357
}
356358

357-
allTokens, err := r.tokenManager.GetTokensByChains(chainIDs)
359+
balances, err := tokensToBalancesPerChain(cachedTokens)
358360
if err != nil {
359361
return nil, err
360362
}
361363

362-
balances, err := tokensToBalancesPerChain(cachedTokens)
364+
tokensOfInterest := make(map[string]struct{}, 0)
365+
for _, cachedToken := range cachedTokens {
366+
for _, token := range cachedToken {
367+
tokensOfInterest[types.TokenKey(token.TokenChainID, token.TokenAddress)] = struct{}{}
368+
}
369+
}
370+
371+
allTokens, err := r.tokenManager.GetTokensByKeys(maps.Keys(tokensOfInterest))
363372
if err != nil {
364373
return nil, err
365374
}

services/wallet/reader_impl_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ethereum/go-ethereum/common"
1616
"github.com/ethereum/go-ethereum/event"
1717

18+
"github.com/status-im/go-wallet-sdk/pkg/tokens/types"
1819
wsdktypes "github.com/status-im/go-wallet-sdk/pkg/tokens/types"
1920

2021
"github.com/status-im/status-go/pkg/pubsub"
@@ -410,8 +411,12 @@ func TestGetCachedBalancesInternal(t *testing.T) {
410411
},
411412
}
412413

414+
tokensOfInterest := []string{
415+
types.TokenKey(cachedTokens[addresses[1]][0].TokenChainID, cachedTokens[addresses[1]][0].TokenAddress),
416+
}
417+
413418
tokenManager.EXPECT().GetCachedBalances().Return(cachedTokens, nil)
414-
tokenManager.EXPECT().GetTokensByChains(chainIDs).Return(allTokens, nil)
419+
tokenManager.EXPECT().GetTokensByKeys(tokensOfInterest).Return(allTokens, nil)
415420
tokens, err := reader.GetCachedBalances(chainIDs, addresses)
416421
require.NoError(t, err)
417422

services/wallet/reader_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/status-im/status-go/pkg/pubsub"
1818
"github.com/status-im/status-go/services/wallet"
19-
"github.com/status-im/status-go/services/wallet/testutils"
2019
mock_token "github.com/status-im/status-go/services/wallet/token/mock/token"
2120
tokentypes "github.com/status-im/status-go/services/wallet/token/types"
2221
mock_tokenbalances "github.com/status-im/status-go/services/wallet/tokenbalances/mock/storage"
@@ -121,8 +120,12 @@ func TestGetCachedBalances(t *testing.T) {
121120
},
122121
}
123122

123+
tokensOfInterest := []string{
124+
types.TokenKey(cachedTokens[addresses[1]][0].TokenChainID, cachedTokens[addresses[1]][0].TokenAddress),
125+
}
126+
124127
tokenManager.EXPECT().GetCachedBalances().Return(cachedTokens, nil)
125-
tokenManager.EXPECT().GetTokensByChains(testutils.NewUint64SliceMatcher(chainIDs)).Return(allTokens, nil)
128+
tokenManager.EXPECT().GetTokensByKeys(tokensOfInterest).Return(allTokens, nil)
126129
tokens, err := reader.GetCachedBalances(chainIDs, addresses)
127130
require.NoError(t, err)
128131

@@ -191,9 +194,13 @@ func TestFetchBalances(t *testing.T) {
191194
},
192195
}
193196

197+
tokensOfInterest := []string{
198+
types.TokenKey(cachedTokens[addresses[1]][0].TokenChainID, cachedTokens[addresses[1]][0].TokenAddress),
199+
}
200+
194201
// Test GetCachedBalances with cached data
195202
tokenManager.EXPECT().GetCachedBalances().Return(cachedTokens, nil)
196-
tokenManager.EXPECT().GetTokensByChains(testutils.NewUint64SliceMatcher(chainIDs)).Return(allTokens, nil)
203+
tokenManager.EXPECT().GetTokensByKeys(tokensOfInterest).Return(allTokens, nil)
197204

198205
tokens, err := reader.GetCachedBalances(chainIDs, addresses)
199206
require.NoError(t, err)

services/wallet/token/token.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type CommunityTokenImageBuilder interface {
6868
type ManagerInterface interface {
6969
GetTokenByChainAddress(chainID uint64, address common.Address) (*tokentypes.Token, error)
7070
GetTokensByChains(chainIDs []uint64) ([]*tokentypes.Token, error)
71+
GetTokensByKeys(tokenKeys []string) ([]*tokentypes.Token, error)
7172
GetCachedBalances() (map[common.Address][]tokentypes.StorageToken, error)
7273
CacheBalances(balances map[common.Address][]tokentypes.StorageToken) error
7374
FindOrCreateTokenByAddress(ctx context.Context, chainID uint64, address common.Address) (*tokentypes.Token, error)

0 commit comments

Comments
 (0)