fix(Wallet): Fix uninitialized token map accessed (#3289)

This commit is contained in:
IvanBelyakoff 2023-03-15 18:10:36 +03:00 committed by GitHub
parent 0eef19f80e
commit 612efb9b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 14 deletions

View File

@ -60,9 +60,8 @@ func NewTokenManager(
RPCClient *rpc.Client, RPCClient *rpc.Client,
networkManager *network.Manager, networkManager *network.Manager,
) *Manager { ) *Manager {
defaultStore := newDefaultStore()
// Order of stores is important when merging token lists. The former prevale // Order of stores is important when merging token lists. The former prevale
tokenManager := &Manager{db, RPCClient, networkManager, []store{newUniswapStore(), defaultStore}, nil, nil} tokenManager := &Manager{db, RPCClient, networkManager, []store{newUniswapStore(), newDefaultStore()}, nil, nil}
return tokenManager return tokenManager
} }
@ -105,6 +104,10 @@ func (tm *Manager) inStore(address common.Address, chainID uint64) bool {
return true return true
} }
if !tm.areTokensFetched() {
tm.fetchTokens()
}
tokensMap, ok := tm.tokenMap[chainID] tokensMap, ok := tm.tokenMap[chainID]
if !ok { if !ok {
return false return false
@ -124,8 +127,10 @@ func (tm *Manager) areTokensFetched() bool {
return true return true
} }
func (tm *Manager) fetchTokens() []*Token { func (tm *Manager) fetchTokens() {
result := make([]*Token, 0) tm.tokenList = nil
tm.tokenMap = nil
for _, store := range tm.stores { for _, store := range tm.stores {
tokens, err := store.GetTokens() tokens, err := store.GetTokens()
if err != nil { if err != nil {
@ -133,10 +138,10 @@ func (tm *Manager) fetchTokens() []*Token {
continue continue
} }
result = mergeTokenLists([][]*Token{result, tokens}) tm.tokenList = mergeTokenLists([][]*Token{tm.tokenList, tokens})
} }
return result tm.tokenMap = toTokenMap(tm.tokenList)
} }
func (tm *Manager) FindToken(network *params.Network, tokenSymbol string) *Token { func (tm *Manager) FindToken(network *params.Network, tokenSymbol string) *Token {
@ -195,26 +200,27 @@ func (tm *Manager) GetAllTokensAndNativeCurrencies() ([]*Token, error) {
} }
func (tm *Manager) GetAllTokens() ([]*Token, error) { func (tm *Manager) GetAllTokens() ([]*Token, error) {
if tm.areTokensFetched() { if !tm.areTokensFetched() {
return tm.tokenList, nil tm.fetchTokens()
} }
tm.tokenList = tm.fetchTokens()
tokens, err := tm.GetCustoms() tokens, err := tm.GetCustoms()
if err != nil { if err != nil {
log.Error("can't fetch custom tokens: %s", err) log.Error("can't fetch custom tokens: %s", err)
} }
tm.tokenList = append(tm.tokenList, tokens...) tokens = append(tm.tokenList, tokens...)
tm.tokenMap = toTokenMap(tm.tokenList)
overrideTokensInPlace(tm.networkManager.GetConfiguredNetworks(), tm.tokenList) overrideTokensInPlace(tm.networkManager.GetConfiguredNetworks(), tokens)
return tm.tokenList, nil return tokens, nil
} }
func (tm *Manager) GetTokens(chainID uint64) ([]*Token, error) { func (tm *Manager) GetTokens(chainID uint64) ([]*Token, error) {
if !tm.areTokensFetched() {
tm.fetchTokens()
}
tokensMap, ok := tm.tokenMap[chainID] tokensMap, ok := tm.tokenMap[chainID]
if !ok { if !ok {
return nil, errors.New("no tokens for this network") return nil, errors.New("no tokens for this network")