Skip to content

Commit f3a20c5

Browse files
committed
chore: pull token manager out of services and pass it
Token manager instantiated and started by status node, and pass to services and other types that need it.
1 parent 08a5b8c commit f3a20c5

11 files changed

+89
-39
lines changed

api/geth_backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,7 @@ func (b *GethStatusBackend) initProtocol() error {
23412341
AccountsPublisher: b.statusNode.AccountsPublisher(),
23422342
TimeSource: b.statusNode.TimeSource(),
23432343
MetricsEnabled: b.prometheusMetrics != nil,
2344+
TokenManager: b.statusNode.TokenManager(),
23442345
}
23452346
err = st.InitProtocol(params)
23462347
if err != nil {

node/get_status_node.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ import (
1515

1616
"go.uber.org/zap"
1717

18-
gethrpc "github.com/ethereum/go-ethereum/rpc"
19-
2018
"github.com/ethereum/go-ethereum/event"
19+
gethrpc "github.com/ethereum/go-ethereum/rpc"
2120

2221
accsmanagement "github.com/status-im/status-go/accounts-management"
2322
common2 "github.com/status-im/status-go/common"
2423
"github.com/status-im/status-go/connection"
2524
"github.com/status-im/status-go/crypto"
2625
"github.com/status-im/status-go/ipfs"
2726
"github.com/status-im/status-go/multiaccounts"
27+
"github.com/status-im/status-go/multiaccounts/accounts"
2828
"github.com/status-im/status-go/node/backup"
2929
rpc2 "github.com/status-im/status-go/node/rpc"
3030
"github.com/status-im/status-go/params"
@@ -50,6 +50,8 @@ import (
5050
"github.com/status-im/status-go/services/updates"
5151
"github.com/status-im/status-go/services/wakuv2ext"
5252
"github.com/status-im/status-go/services/wallet"
53+
"github.com/status-im/status-go/services/wallet/community"
54+
"github.com/status-im/status-go/services/wallet/token"
5355
"github.com/status-im/status-go/timesource"
5456
"github.com/status-im/status-go/transactions"
5557
)
@@ -82,6 +84,8 @@ type StatusNode struct {
8284
mediaServerEnableTLS *bool
8385
httpServer *server.MediaServer
8486

87+
tokenManager *token.Manager
88+
8589
logger *zap.Logger
8690

8791
gethAccountsManager *accsmanagement.AccountsManager
@@ -291,6 +295,10 @@ func (n *StatusNode) startWithDB(config *params.NodeConfig) error {
291295
return err
292296
}
293297

298+
if err := n.createAndStartTokenManager(); err != nil {
299+
return err
300+
}
301+
294302
if err := n.initServices(config, n.httpServer); err != nil {
295303
return err
296304
}
@@ -325,6 +333,34 @@ func (n *StatusNode) startWithDB(config *params.NodeConfig) error {
325333
return nil
326334
}
327335

336+
func (n *StatusNode) createAndStartTokenManager() error {
337+
accDB, err := accounts.NewDB(n.appDB)
338+
if err != nil {
339+
return err
340+
}
341+
342+
n.tokenManager = token.NewTokenManager(n.walletDB, n.rpcClient, community.NewManager(n.appDB, n.httpServer, nil),
343+
n.rpcClient.GetNetworkManager(), n.appDB, n.httpServer, &n.walletFeed, n.accountsPublisher, accDB,
344+
token.NewPersistence(n.walletDB))
345+
346+
const (
347+
defaultAutoRefreshInterval = 30 * time.Minute // interval after which we should fetch the token lists from the remote source (or use the default one if remote source is not set)
348+
defaultAutoRefreshCheckInterval = 3 * time.Minute // interval after which we should check if we should trigger the auto-refresh
349+
)
350+
351+
autoRefreshInterval := defaultAutoRefreshInterval
352+
autoRefreshCheckInterval := defaultAutoRefreshCheckInterval
353+
if n.config.WalletConfig.TokensListsAutoRefreshInterval > 0 &&
354+
n.config.WalletConfig.TokensListsAutoRefreshCheckInterval > 0 &&
355+
n.config.WalletConfig.TokensListsAutoRefreshInterval > n.config.WalletConfig.TokensListsAutoRefreshCheckInterval {
356+
autoRefreshInterval = time.Duration(n.config.WalletConfig.TokensListsAutoRefreshInterval) * time.Second
357+
autoRefreshCheckInterval = time.Duration(n.config.WalletConfig.TokensListsAutoRefreshCheckInterval) * time.Second
358+
}
359+
360+
n.tokenManager.Start(context.Background(), autoRefreshInterval, autoRefreshCheckInterval)
361+
return nil
362+
}
363+
328364
func (n *StatusNode) setupRPCClient() (err error) {
329365
config := rpc.ClientConfig{
330366
UpstreamChainID: n.config.NetworkID,
@@ -443,3 +479,7 @@ func (n *StatusNode) SetWalletDB(db *sql.DB) {
443479
func (n *StatusNode) GetWalletDB() *sql.DB {
444480
return n.walletDB
445481
}
482+
483+
func (n *StatusNode) TokenManager() *token.Manager {
484+
return n.tokenManager
485+
}

node/status_node_services.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ func (b *StatusNode) walletService(accountsDB *accounts.Database, appDB *sql.DB,
291291
b.pendingTracker,
292292
walletFeed,
293293
b.httpServer,
294+
b.tokenManager,
294295
statusProxyStageName,
295296
)
296297
}

protocol/communities_messenger_helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func newTestCommunitiesMessenger(s *suite.Suite, messagingEnv *messaging.TestMes
313313

314314
options := []Option{
315315
WithAccountsManager(accountsManagerMock),
316-
WithTokenManager(tokenManagerMock),
316+
WithCommunityTokenManager(tokenManagerMock),
317317
WithMessageSigner(NewSignerStub()),
318318
WithCollectiblesManager(config.collectiblesManager),
319319
WithCommunityTokensService(config.collectiblesService),

protocol/messenger.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ import (
5555
localnotifications "github.com/status-im/status-go/services/local-notifications"
5656
mailserversDB "github.com/status-im/status-go/services/mailservers"
5757
"github.com/status-im/status-go/services/wallet"
58-
"github.com/status-im/status-go/services/wallet/community"
59-
"github.com/status-im/status-go/services/wallet/token"
6058
"github.com/status-im/status-go/signal"
6159

6260
_ "github.com/mmcdole/gofeed"
@@ -332,11 +330,11 @@ func NewMessenger(
332330
managerOptions = append(managerOptions, communities.WithCollectiblesManager(c.collectiblesManager))
333331
}
334332

335-
if c.tokenManager != nil {
336-
managerOptions = append(managerOptions, communities.WithTokenManager(c.tokenManager))
337-
} else if c.rpcClient != nil {
338-
tokenManager := token.NewTokenManager(c.walletDb, c.rpcClient, community.NewManager(database, c.httpServer, nil), c.rpcClient.GetNetworkManager(), database, c.httpServer, nil, nil, nil, token.NewPersistence(c.walletDb))
339-
managerOptions = append(managerOptions, communities.WithTokenManager(communities.NewDefaultTokenManager(tokenManager, c.rpcClient.GetNetworkManager())))
333+
if c.communityTokenManager != nil {
334+
managerOptions = append(managerOptions, communities.WithTokenManager(c.communityTokenManager))
335+
} else if c.rpcClient != nil && c.tokenManager != nil {
336+
managerOptions = append(managerOptions, communities.WithTokenManager(
337+
communities.NewDefaultTokenManager(c.tokenManager, c.rpcClient.GetNetworkManager())))
340338
}
341339

342340
if c.communityTokensService != nil {

protocol/messenger_builder_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"github.com/status-im/status-go/protocol/protobuf"
2424
"github.com/status-im/status-go/protocol/sqlite"
2525
"github.com/status-im/status-go/protocol/tt"
26+
"github.com/status-im/status-go/rpc/network"
2627
"github.com/status-im/status-go/services/browsers"
28+
"github.com/status-im/status-go/services/wallet/token"
2729
"github.com/status-im/status-go/t/helpers"
2830
"github.com/status-im/status-go/walletdatabase"
2931
)
@@ -144,6 +146,8 @@ func newTestMessenger(messagingEnv *messaging.TestMessagingEnvironment, config t
144146
"",
145147
)
146148

149+
tokenManager := token.NewTokenManager(walletDb, nil, nil, network.NewManager(appDb, nil), appDb, nil, nil, nil, nil, token.NewPersistence(walletDb))
150+
147151
options := []Option{
148152
WithCustomLogger(config.logger),
149153
WithDatabase(appDb),
@@ -156,6 +160,7 @@ func newTestMessenger(messagingEnv *messaging.TestMessagingEnvironment, config t
156160
WithStubOnlineChecker(),
157161
WithENSVerifier(ensVerifier),
158162
WithMessageSigner(NewSignerStub()),
163+
WithTokenManager(tokenManager),
159164
}
160165
options = append(options, config.extraOptions...)
161166

protocol/messenger_config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/status-im/status-go/rpc"
1010
"github.com/status-im/status-go/server"
1111
"github.com/status-im/status-go/services/browsers"
12+
"github.com/status-im/status-go/services/wallet/token"
1213

1314
"go.uber.org/zap"
1415

@@ -83,7 +84,8 @@ type config struct {
8384
communityTokensService communities.CommunityTokensServiceInterface
8485
httpServer *server.MediaServer
8586
rpcClient *rpc.Client
86-
tokenManager communities.TokenManager
87+
tokenManager *token.Manager
88+
communityTokenManager communities.TokenManager
8789
collectiblesManager communities.CollectiblesManager
8890
accountsManager AccountsManager
8991
signer communities.MessageSigner
@@ -294,7 +296,14 @@ func WithCommunityTokensService(s communities.CommunityTokensServiceInterface) O
294296
}
295297
}
296298

297-
func WithTokenManager(tokenManager communities.TokenManager) Option {
299+
func WithCommunityTokenManager(tokenManager communities.TokenManager) Option {
300+
return func(c *config) error {
301+
c.communityTokenManager = tokenManager
302+
return nil
303+
}
304+
}
305+
306+
func WithTokenManager(tokenManager *token.Manager) Option {
298307
return func(c *config) error {
299308
c.tokenManager = tokenManager
300309
return nil

services/ext/service.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"github.com/status-im/status-go/protocol"
4040
"github.com/status-im/status-go/protocol/common"
4141
"github.com/status-im/status-go/protocol/communities"
42-
"github.com/status-im/status-go/protocol/communities/token"
42+
communitiestoken "github.com/status-im/status-go/protocol/communities/token"
4343
"github.com/status-im/status-go/protocol/ens"
4444
"github.com/status-im/status-go/protocol/protobuf"
4545
"github.com/status-im/status-go/protocol/pushnotificationclient"
@@ -54,6 +54,7 @@ import (
5454
"github.com/status-im/status-go/services/wallet/collectibles"
5555
w_common "github.com/status-im/status-go/services/wallet/common"
5656
"github.com/status-im/status-go/services/wallet/thirdparty"
57+
"github.com/status-im/status-go/services/wallet/token"
5758
"github.com/status-im/status-go/signal"
5859
)
5960

@@ -111,6 +112,7 @@ type InitProtocolParams struct {
111112
AccountsPublisher *pubsub.Publisher
112113
TimeSource timesource.TimeSource
113114
MetricsEnabled bool
115+
TokenManager *token.Manager
114116
}
115117

116118
func (s *Service) InitProtocol(params InitProtocolParams) error {
@@ -193,7 +195,10 @@ func (s *Service) InitProtocol(params InitProtocolParams) error {
193195
s.config.ShhextConfig.VerifyENSContractAddress,
194196
)
195197

196-
options, err := buildMessengerOptions(s.config, params.Identity, params.AppDB, params.WalletDB, params.HTTPServer, s.rpcClient, s.multiAccountsDB, params.Account, envelopeEventsConfig, s.accountsDB, params.WalletService, params.CommunityTokensService, s.logger, &MessengerSignalsHandler{}, params.AccountsManager, params.AccountsPublisher, ensVerifier)
198+
options, err := buildMessengerOptions(s.config, params.Identity, params.AppDB, params.WalletDB, params.HTTPServer,
199+
s.rpcClient, s.multiAccountsDB, params.Account, envelopeEventsConfig, s.accountsDB, params.WalletService,
200+
params.CommunityTokensService, s.logger, &MessengerSignalsHandler{}, params.AccountsManager, params.AccountsPublisher,
201+
ensVerifier, params.TokenManager)
197202
if err != nil {
198203
return err
199204
}
@@ -430,6 +435,7 @@ func buildMessengerOptions(
430435
accountsManager *accsmanagement.AccountsManager,
431436
accountsPublisher *pubsub.Publisher,
432437
ensVerifier *ens.Verifier,
438+
tokenManager *token.Manager,
433439
) ([]protocol.Option, error) {
434440
personalService := personal.New()
435441
options := []protocol.Option{
@@ -455,6 +461,7 @@ func buildMessengerOptions(
455461
protocol.WithAccountsPublisher(accountsPublisher),
456462
protocol.WithNewsFeed(),
457463
protocol.WithMessageSigner(personalService),
464+
protocol.WithTokenManager(tokenManager),
458465
}
459466

460467
if config.ShhextConfig.DataSyncEnabled {
@@ -586,7 +593,7 @@ func (s *Service) FillCollectibleMetadata(community *communities.Community, coll
586593

587594
permission := fetchCommunityCollectiblePermission(community, id)
588595

589-
privilegesLevel := token.CommunityLevel
596+
privilegesLevel := communitiestoken.CommunityLevel
590597
if permission != nil {
591598
privilegesLevel = permissionTypeToPrivilegesLevel(permission.GetType())
592599
}
@@ -621,14 +628,14 @@ func (s *Service) FillCollectibleMetadata(community *communities.Community, coll
621628
return nil
622629
}
623630

624-
func permissionTypeToPrivilegesLevel(permissionType protobuf.CommunityTokenPermission_Type) token.PrivilegesLevel {
631+
func permissionTypeToPrivilegesLevel(permissionType protobuf.CommunityTokenPermission_Type) communitiestoken.PrivilegesLevel {
625632
switch permissionType {
626633
case protobuf.CommunityTokenPermission_BECOME_TOKEN_OWNER:
627-
return token.OwnerLevel
634+
return communitiestoken.OwnerLevel
628635
case protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER:
629-
return token.MasterLevel
636+
return communitiestoken.MasterLevel
630637
default:
631-
return token.CommunityLevel
638+
return communitiestoken.CommunityLevel
632639
}
633640
}
634641

@@ -720,7 +727,7 @@ func (s *Service) fetchCommunityInfoForCollectibles(communityID string, ids []th
720727
return community, nil
721728
}
722729

723-
func (s *Service) fetchCommunityToken(communityID string, contractID thirdparty.ContractID) (*token.CommunityToken, error) {
730+
func (s *Service) fetchCommunityToken(communityID string, contractID thirdparty.ContractID) (*communitiestoken.CommunityToken, error) {
724731
if s.messenger == nil {
725732
return nil, fmt.Errorf("messenger not ready")
726733
}
@@ -826,7 +833,7 @@ func boolToString(value bool) string {
826833
return "No"
827834
}
828835

829-
func getCollectibleCommunityTraits(token *token.CommunityToken) []thirdparty.CollectibleTrait {
836+
func getCollectibleCommunityTraits(token *communitiestoken.CommunityToken) []thirdparty.CollectibleTrait {
830837
if token == nil {
831838
return make([]thirdparty.CollectibleTrait, 0)
832839
}

services/wallet/api_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/status-im/status-go/rpc"
3131
"github.com/status-im/status-go/services/wallet/onramp"
3232
"github.com/status-im/status-go/services/wallet/requests"
33+
"github.com/status-im/status-go/services/wallet/token"
3334
tokentypes "github.com/status-im/status-go/services/wallet/token/types"
3435
"github.com/status-im/status-go/services/wallet/walletconnect"
3536
"github.com/status-im/status-go/t/helpers"
@@ -156,7 +157,9 @@ func TestAPI_GetAddressDetails(t *testing.T) {
156157
c, err := rpc.NewClient(config)
157158
require.NoError(t, err)
158159

159-
service := NewService(db, accountsDb, appDB, c, accountsPublisher, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil, "")
160+
tokenManager := token.NewTokenManager(db, c, nil, nil, appDB, nil, nil, nil, accountsDb, token.NewPersistence(db))
161+
162+
service := NewService(db, accountsDb, appDB, c, accountsPublisher, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil, tokenManager, "")
160163

161164
api := &API{
162165
s: service,

services/wallet/keycard_pairings_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func TestKeycardPairingsFile(t *testing.T) {
3232
AccountsPublisher: accountsPublisher,
3333
})
3434
require.NoError(t, err)
35-
service := NewService(db, accountsDb, appDB, rpcClient, accountsPublisher, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil, "")
35+
36+
service := NewService(db, accountsDb, appDB, rpcClient, accountsPublisher, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil, nil, "")
3637

3738
data, err := service.KeycardPairings().GetPairingsJSONFileContent()
3839
require.NoError(t, err)

0 commit comments

Comments
 (0)