feat: wallet custom token with communities

This commit is contained in:
Anthony Laibe 2023-08-22 11:32:27 +02:00
parent 09a988607d
commit 9d782edb4d
5 changed files with 44 additions and 2 deletions

View File

@ -121,6 +121,7 @@ type managerOptions struct {
type TokenManager interface { type TokenManager interface {
GetBalancesByChain(ctx context.Context, accounts, tokens []gethcommon.Address, chainIDs []uint64) (map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big, error) 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) GetAllChainIDs() ([]uint64, error)
} }
@ -161,6 +162,10 @@ func (m *DefaultTokenManager) GetBalancesByChain(ctx context.Context, accounts,
return resp, err return resp, err
} }
func (m *DefaultTokenManager) UpsertCustom(t token.Token) error {
return m.tokenManager.UpsertCustom(t)
}
type ManagerOption func(*managerOptions) type ManagerOption func(*managerOptions)
func WithAccountManager(accountsManager account.Manager) ManagerOption { func WithAccountManager(accountsManager account.Manager) ManagerOption {
@ -1303,6 +1308,27 @@ func (m *Manager) handleCommunityDescriptionMessageCommon(community *Community,
} }
pkString := common.PubkeyToHex(&m.identity.PublicKey) 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 the community require membership, we set whether we should leave/join the community after a state change
if community.InvitationOnly() || community.OnRequest() || community.AcceptRequestToJoinAutomatically() { if community.InvitationOnly() || community.OnRequest() || community.AcceptRequestToJoinAutomatically() {

View File

@ -22,6 +22,7 @@ import (
"github.com/status-im/status-go/services/wallet/bigint" "github.com/status-im/status-go/services/wallet/bigint"
walletCommon "github.com/status-im/status-go/services/wallet/common" 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/thirdparty"
"github.com/status-im/status-go/services/wallet/token"
"github.com/status-im/status-go/t/helpers" "github.com/status-im/status-go/t/helpers"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@ -135,6 +136,10 @@ func (m *testTokenManager) GetBalancesByChain(ctx context.Context, accounts, tok
return m.response, nil return m.response, nil
} }
func (m *testTokenManager) UpsertCustom(token token.Token) error {
return nil
}
func (s *ManagerSuite) setupManagerForTokenPermissions() (*Manager, *testCollectiblesManager, *testTokenManager) { func (s *ManagerSuite) setupManagerForTokenPermissions() (*Manager, *testCollectiblesManager, *testTokenManager) {
db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.NoError(err, "creating sqlite db instance") s.NoError(err, "creating sqlite db instance")

View File

@ -30,6 +30,7 @@ import (
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt" "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/t/helpers"
"github.com/status-im/status-go/walletdatabase" "github.com/status-im/status-go/walletdatabase"
) )
@ -73,6 +74,11 @@ func (m *TokenManagerMock) GetBalancesByChain(ctx context.Context, accounts, tok
return *m.Balances, nil 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, 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 { mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big) *Messenger {
accountsManagerMock := &AccountManagerMock{} accountsManagerMock := &AccountManagerMock{}

View File

@ -425,7 +425,7 @@ func NewMessenger(
if c.tokenManager != nil { if c.tokenManager != nil {
managerOptions = append(managerOptions, communities.WithTokenManager(c.tokenManager)) managerOptions = append(managerOptions, communities.WithTokenManager(c.tokenManager))
} else if c.rpcClient != nil { } 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))) managerOptions = append(managerOptions, communities.WithTokenManager(communities.NewDefaultTokenManager(tokenManager)))
} }

View File

@ -300,7 +300,12 @@ func (tm *Manager) GetTokens(chainID uint64) ([]*Token, error) {
res = append(res, token) 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) { func (tm *Manager) DiscoverToken(ctx context.Context, chainID uint64, address common.Address) (*Token, error) {