@@ -38,8 +38,10 @@ import (
3838 "github.com/status-im/status-go/services/wallet/community"
3939 tokenlists "github.com/status-im/status-go/services/wallet/token/token-lists"
4040 "github.com/status-im/status-go/services/wallet/token/token-lists/fetcher"
41+ "github.com/status-im/status-go/services/wallet/token/tokenevent"
4142 tokenTypes "github.com/status-im/status-go/services/wallet/token/types"
4243 "github.com/status-im/status-go/services/wallet/walletevent"
44+ "github.com/status-im/status-go/signal"
4345)
4446
4547const (
@@ -76,6 +78,7 @@ type ManagerInterface interface {
7678 FindOrCreateTokenByAddress (ctx context.Context , chainID uint64 , address common.Address ) * tokenTypes.Token
7779 MarkAsPreviouslyOwnedToken (token * tokenTypes.Token , owner common.Address ) (bool , error )
7880 SignalCommunityTokenReceived (address common.Address , txHash common.Hash , value * big.Int , t * tokenTypes.Token , isFirst bool )
81+ GetTokenPublisher () * pubsub.Publisher
7982}
8083
8184// Manager is used for accessing token store. It changes the token store based on overridden tokens
@@ -90,6 +93,7 @@ type Manager struct {
9093 walletFeed * event.Feed
9194 accountsDB * accounts.Database
9295 accountsPublisher * pubsub.Publisher
96+ tokenPublisher * pubsub.Publisher
9397 tokenBalancesStorage TokenBalancesStorage
9498
9599 tokenLists * tokenlists.TokenLists
@@ -106,6 +110,7 @@ func NewTokenManager(
106110 mediaServer * server.MediaServer ,
107111 walletFeed * event.Feed ,
108112 accountsPublisher * pubsub.Publisher ,
113+ tokenPublisher * pubsub.Publisher ,
109114 accountsDB * accounts.Database ,
110115 tokenBalancesStorage TokenBalancesStorage ,
111116) * Manager {
@@ -127,6 +132,7 @@ func NewTokenManager(
127132 mediaServer : mediaServer ,
128133 walletFeed : walletFeed ,
129134 accountsPublisher : accountsPublisher ,
135+ tokenPublisher : tokenPublisher ,
130136 accountsDB : accountsDB ,
131137 tokenBalancesStorage : tokenBalancesStorage ,
132138 tokenLists : tokensLists ,
@@ -136,6 +142,7 @@ func NewTokenManager(
136142func (tm * Manager ) Start (ctx context.Context , autoRefreshInterval time.Duration , autoRefreshCheckInterval time.Duration ) {
137143 tm .stopCh = make (chan struct {})
138144 tm .startAccountsWatcher ()
145+ tm .startTokenDiscoveryWatcher ()
139146
140147 // For now we don't have the list of tokens lists remotely set so we're uisng the harcoded default lists. Once we have it
141148 //we will just need to update the empty string with the correct URL.
@@ -165,6 +172,35 @@ func (tm *Manager) startAccountsWatcher() {
165172 }()
166173}
167174
175+ func (tm * Manager ) startTokenDiscoveryWatcher () {
176+ if tm .tokenPublisher == nil {
177+ return
178+ }
179+
180+ ch , unsubFn := pubsub .Subscribe [tokenevent.TokenDiscoveryRequestEvent ](tm .tokenPublisher , 100 )
181+ go func () {
182+ defer gocommon .LogOnPanic ()
183+ defer unsubFn ()
184+ for {
185+ select {
186+ case <- tm .stopCh :
187+ return
188+ case event , ok := <- ch :
189+ if ! ok {
190+ return
191+ }
192+ tm .handleTokenDiscoveryRequest (context .Background (), event )
193+ }
194+ }
195+ }()
196+ }
197+
198+ func (tm * Manager ) handleTokenDiscoveryRequest (ctx context.Context , event tokenevent.TokenDiscoveryRequestEvent ) {
199+ // Use existing FindOrCreateTokenByAddress logic
200+ // This will fetch token metadata and send TokenListsUpdated signal to frontend
201+ tm .FindOrCreateTokenByAddress (ctx , event .ChainID , event .Address )
202+ }
203+
168204func (tm * Manager ) Stop () {
169205 if tm .stopCh != nil {
170206 close (tm .stopCh )
@@ -173,6 +209,10 @@ func (tm *Manager) Stop() {
173209 tm .tokenLists .Stop ()
174210}
175211
212+ func (tm * Manager ) GetTokenPublisher () * pubsub.Publisher {
213+ return tm .tokenPublisher
214+ }
215+
176216// overrideTokensInPlace overrides tokens in the store with the ones from the networks
177217// BEWARE: overridden tokens will have their original address removed and replaced by the one in networks
178218func overrideTokensInPlace (networks []params.Network , tokens []* tokenTypes.Token ) {
@@ -298,6 +338,8 @@ func (tm *Manager) FindOrCreateTokenByAddress(ctx context.Context, chainID uint6
298338 }
299339
300340 tm .discoverTokenCommunityID (ctx , token , address )
341+ signal .SendWalletEvent (signal .TokenListsUpdated , nil )
342+
301343 return token
302344}
303345
@@ -437,7 +479,7 @@ func (tm *Manager) getNativeTokens() ([]*tokenTypes.Token, error) {
437479}
438480
439481func (tm * Manager ) GetAllTokens () ([]* tokenTypes.Token , error ) {
440- allTokens , err := tm .GetCustoms (true )
482+ allTokens , err := tm .GetCustoms (false )
441483 if err != nil {
442484 logutils .ZapLogger ().Error ("can't fetch custom tokens" , zap .Error (err ))
443485 }
0 commit comments