From 9d782edb4d63d7163da13ad3186b715e60ba907c Mon Sep 17 00:00:00 2001 From: Anthony Laibe <491074+alaibe@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:32:27 +0200 Subject: [PATCH] feat: wallet custom token with communities --- protocol/communities/manager.go | 26 +++++++++++++++++++ protocol/communities/manager_test.go | 5 ++++ .../communities_messenger_helpers_test.go | 6 +++++ protocol/messenger.go | 2 +- services/wallet/token/token.go | 7 ++++- 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 703f219f6..9ea524819 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -121,6 +121,7 @@ type managerOptions struct { type TokenManager interface { GetBalancesByChain(ctx context.Context, accounts, tokens []gethcommon.Address, chainIDs []uint64) (map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big, error) + UpsertCustom(token token.Token) error GetAllChainIDs() ([]uint64, error) } @@ -161,6 +162,10 @@ func (m *DefaultTokenManager) GetBalancesByChain(ctx context.Context, accounts, return resp, err } +func (m *DefaultTokenManager) UpsertCustom(t token.Token) error { + return m.tokenManager.UpsertCustom(t) +} + type ManagerOption func(*managerOptions) func WithAccountManager(accountsManager account.Manager) ManagerOption { @@ -1303,6 +1308,27 @@ func (m *Manager) handleCommunityDescriptionMessageCommon(community *Community, } pkString := common.PubkeyToHex(&m.identity.PublicKey) + if description.CommunityTokensMetadata != nil && len(description.CommunityTokensMetadata) > 0 { + for _, tokenMetadata := range description.CommunityTokensMetadata { + if tokenMetadata.TokenType != protobuf.CommunityTokenType_ERC20 { + continue + } + + for chainID, address := range tokenMetadata.ContractAddresses { + token := token.Token{ + Address: gethcommon.HexToAddress(address), + ChainID: chainID, + Symbol: tokenMetadata.Symbol, + Name: tokenMetadata.Name, + Decimals: uint(tokenMetadata.Decimals), + } + err := m.tokenManager.UpsertCustom(token) + if err != nil { + return nil, err + } + } + } + } // If the community require membership, we set whether we should leave/join the community after a state change if community.InvitationOnly() || community.OnRequest() || community.AcceptRequestToJoinAutomatically() { diff --git a/protocol/communities/manager_test.go b/protocol/communities/manager_test.go index 4cf2a807a..26f1eedce 100644 --- a/protocol/communities/manager_test.go +++ b/protocol/communities/manager_test.go @@ -22,6 +22,7 @@ import ( "github.com/status-im/status-go/services/wallet/bigint" walletCommon "github.com/status-im/status-go/services/wallet/common" "github.com/status-im/status-go/services/wallet/thirdparty" + "github.com/status-im/status-go/services/wallet/token" "github.com/status-im/status-go/t/helpers" "github.com/golang/protobuf/proto" @@ -135,6 +136,10 @@ func (m *testTokenManager) GetBalancesByChain(ctx context.Context, accounts, tok return m.response, nil } +func (m *testTokenManager) UpsertCustom(token token.Token) error { + return nil +} + func (s *ManagerSuite) setupManagerForTokenPermissions() (*Manager, *testCollectiblesManager, *testTokenManager) { db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) s.NoError(err, "creating sqlite db instance") diff --git a/protocol/communities_messenger_helpers_test.go b/protocol/communities_messenger_helpers_test.go index a604f6df6..44bfb2c6e 100644 --- a/protocol/communities_messenger_helpers_test.go +++ b/protocol/communities_messenger_helpers_test.go @@ -30,6 +30,7 @@ import ( "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/protocol/tt" + walletToken "github.com/status-im/status-go/services/wallet/token" "github.com/status-im/status-go/t/helpers" "github.com/status-im/status-go/walletdatabase" ) @@ -73,6 +74,11 @@ func (m *TokenManagerMock) GetBalancesByChain(ctx context.Context, accounts, tok return *m.Balances, nil } +func (m *TokenManagerMock) UpsertCustom(token walletToken.Token) error { + time.Sleep(100 * time.Millisecond) // simulate response time + return nil +} + func newMessenger(s *suite.Suite, shh types.Waku, logger *zap.Logger, password string, walletAddresses []string, mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big) *Messenger { accountsManagerMock := &AccountManagerMock{} diff --git a/protocol/messenger.go b/protocol/messenger.go index 563777dba..90228adcd 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -425,7 +425,7 @@ func NewMessenger( if c.tokenManager != nil { managerOptions = append(managerOptions, communities.WithTokenManager(c.tokenManager)) } else if c.rpcClient != nil { - tokenManager := token.NewTokenManager(database, c.rpcClient, c.rpcClient.NetworkManager) + tokenManager := token.NewTokenManager(c.walletDb, c.rpcClient, c.rpcClient.NetworkManager) managerOptions = append(managerOptions, communities.WithTokenManager(communities.NewDefaultTokenManager(tokenManager))) } diff --git a/services/wallet/token/token.go b/services/wallet/token/token.go index 841838ec5..32ea11e3f 100644 --- a/services/wallet/token/token.go +++ b/services/wallet/token/token.go @@ -300,7 +300,12 @@ func (tm *Manager) GetTokens(chainID uint64) ([]*Token, error) { res = append(res, token) } - return res, nil + tokens, err := tm.GetCustomsByChainID(chainID) + if err != nil { + return nil, err + } + + return append(res, tokens...), nil } func (tm *Manager) DiscoverToken(ctx context.Context, chainID uint64, address common.Address) (*Token, error) {