@@ -2,12 +2,16 @@ package api
22
33import (
44 "context"
5+ "fmt"
6+ "math/big"
57
68 gethcommon "github.com/ethereum/go-ethereum/common"
9+ "github.com/ethereum/go-ethereum/common/hexutil"
710 "github.com/status-im/status-go/protocol/communities"
811 "github.com/status-im/status-go/rpc/network"
912 "github.com/status-im/status-go/services/wallet/token"
1013 tokenTypes "github.com/status-im/status-go/services/wallet/token/types"
14+ "github.com/status-im/status-go/services/wallet/tokenbalances"
1115)
1216
1317var _ communities.NetworkManager = (* CommunitiesNetworkManager )(nil )
@@ -48,28 +52,52 @@ func (m *CommunitiesTokenManager) FindOrCreateTokenByAddress(ctx context.Context
4852}
4953
5054type CommunitiesTokenBalanceManager struct {
51- tokenManager * token.Manager
55+ tokenBalancesFetcher * tokenbalances.Fetcher
56+ tokenBalancesStorage tokenbalances.Storage
5257}
5358
54- func NewCommunitiesTokenBalanceManager (tm * token. Manager ) * CommunitiesTokenBalanceManager {
55- return & CommunitiesTokenBalanceManager {tokenManager : tm }
59+ func NewCommunitiesTokenBalanceManager (f * tokenbalances. Fetcher , s tokenbalances. Storage ) * CommunitiesTokenBalanceManager {
60+ return & CommunitiesTokenBalanceManager {tokenBalancesFetcher : f , tokenBalancesStorage : s }
5661}
5762
5863func (m * CommunitiesTokenBalanceManager ) GetBalancesByChain (ctx context.Context , accounts , tokenAddresses []gethcommon.Address , chainIDs []uint64 ) (communities.BalancesByChain , error ) {
59- chainClients , err := m .tokenManager .RPCClient .EthClients (chainIDs )
60- if err != nil {
61- return nil , err
64+ if m .tokenBalancesFetcher == nil {
65+ return nil , fmt .Errorf ("tokenBalancesFetcher is nil" )
6266 }
63-
64- resp , err := m .tokenManager .GetBalancesByChain (context .Background (), chainClients , accounts , tokenAddresses )
65- return resp , err
67+ ret := make (communities.BalancesByChain )
68+ for _ , chainID := range chainIDs {
69+ ret [chainID ] = make (map [gethcommon.Address ]map [gethcommon.Address ]* hexutil.Big )
70+ balances , err := m .tokenBalancesFetcher .Fetch (ctx , chainID , tokenAddresses , accounts )
71+ if err != nil {
72+ return nil , err
73+ }
74+ ret [chainID ] = balancesToCommunitiesBalances (balances )
75+ }
76+ return ret , nil
6677}
6778
6879func (m * CommunitiesTokenBalanceManager ) GetCachedBalancesByChain (ctx context.Context , accounts , tokenAddresses []gethcommon.Address , chainIDs []uint64 ) (communities.BalancesByChain , error ) {
69- resp , err := m .tokenManager .GetCachedBalancesByChain (accounts , tokenAddresses , chainIDs )
70- if err != nil {
71- return resp , err
80+ if m .tokenBalancesStorage == nil {
81+ return nil , fmt .Errorf ("tokenBalancesStorage is nil" )
82+ }
83+ ret := make (communities.BalancesByChain )
84+ for _ , chainID := range chainIDs {
85+ balances , err := m .tokenBalancesStorage .GetBalances (ctx , chainID , tokenAddresses , accounts )
86+ if err != nil {
87+ return nil , err
88+ }
89+ ret [chainID ] = balancesToCommunitiesBalances (balances )
7290 }
91+ return ret , nil
92+ }
7393
74- return resp , nil
94+ func balancesToCommunitiesBalances (balances map [gethcommon.Address ]map [gethcommon.Address ]* big.Int ) map [gethcommon.Address ]map [gethcommon.Address ]* hexutil.Big {
95+ ret := make (map [gethcommon.Address ]map [gethcommon.Address ]* hexutil.Big )
96+ for account , tokenBalances := range balances {
97+ ret [account ] = make (map [gethcommon.Address ]* hexutil.Big )
98+ for token , balance := range tokenBalances {
99+ ret [account ][token ] = (* hexutil .Big )(balance )
100+ }
101+ }
102+ return ret
75103}
0 commit comments