feat: rehydrate community_tokens table upon account recovery

Closes https://github.com/status-im/status-desktop/issues/11188
This commit is contained in:
Pascal Precht 2023-07-07 15:03:37 +02:00 committed by r4bbit
parent 70c29a828e
commit 4b07960fba
18 changed files with 249 additions and 71 deletions

View File

@ -2003,7 +2003,7 @@ func (b *GethStatusBackend) injectAccountsIntoWakuService(w types.WakuKeyManager
}
if st != nil {
if err := st.InitProtocol(b.statusNode.GethNode().Config().Name, identity, b.appDB, b.statusNode.HTTPServer(), b.multiaccountsDB, acc, b.accountManager, b.statusNode.RPCClient(), b.statusNode.WalletService(), logutils.ZapLogger()); err != nil {
if err := st.InitProtocol(b.statusNode.GethNode().Config().Name, identity, b.appDB, b.statusNode.HTTPServer(), b.multiaccountsDB, acc, b.accountManager, b.statusNode.RPCClient(), b.statusNode.WalletService(), b.statusNode.CollectiblesService(), logutils.ZapLogger()); err != nil {
return err
}
// Set initial connection state

View File

@ -493,6 +493,10 @@ func (b *StatusNode) SetWalletCollectibleMetadataProvider(provider thirdparty.Co
}
}
func (b *StatusNode) CollectiblesService() *collectibles.Service {
return b.collectiblesSrvc
}
func (b *StatusNode) walletService(accountsDB *accounts.Database, accountsFeed *event.Feed) *wallet.Service {
if b.walletSrvc == nil {
b.walletSrvc = wallet.NewService(

View File

@ -19,7 +19,6 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/v1"
"github.com/status-im/status-go/services/wallet/bigint"
)
const signatureLength = 65
@ -457,31 +456,6 @@ func (o *Community) initialize() {
}
}
type DeployState uint8
const (
Failed DeployState = iota
InProgress
Deployed
)
type CommunityToken struct {
TokenType protobuf.CommunityTokenType `json:"tokenType"`
CommunityID string `json:"communityId"`
Address string `json:"address"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Description string `json:"description"`
Supply *bigint.BigInt `json:"supply"`
InfiniteSupply bool `json:"infiniteSupply"`
Transferable bool `json:"transferable"`
RemoteSelfDestruct bool `json:"remoteSelfDestruct"`
ChainID int `json:"chainId"`
DeployState DeployState `json:"deployState"`
Base64Image string `json:"image"`
Decimals int `json:"decimals"`
}
type CommunitySettings struct {
CommunityID string `json:"communityId"`
HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled"`

View File

@ -25,6 +25,7 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/account"
@ -33,11 +34,13 @@ import (
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/common"
community_token "github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/encryption"
"github.com/status-im/status-go/protocol/ens"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/transport"
"github.com/status-im/status-go/services/collectibles"
"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"
@ -78,6 +81,7 @@ type Manager struct {
torrentConfig *params.TorrentConfig
torrentClient *torrent.Client
walletConfig *params.WalletConfig
collectiblesService *collectibles.Service
historyArchiveTasksWaitGroup sync.WaitGroup
historyArchiveTasks sync.Map // stores `chan struct{}`
periodicMembersReevaluationTasks sync.Map // stores `chan struct{}`
@ -112,6 +116,7 @@ type managerOptions struct {
tokenManager TokenManager
collectiblesManager CollectiblesManager
walletConfig *params.WalletConfig
collectiblesService *collectibles.Service
}
type TokenManager interface {
@ -182,6 +187,12 @@ func WithWalletConfig(walletConfig *params.WalletConfig) ManagerOption {
}
}
func WithCollectiblesService(collectiblesService *collectibles.Service) ManagerOption {
return func(opts *managerOptions) {
opts.collectiblesService = collectiblesService
}
}
func NewManager(identity *ecdsa.PrivateKey, db *sql.DB, encryptor *encryption.Protocol, logger *zap.Logger, verifier *ens.Verifier, transport *transport.Transport, torrentConfig *params.TorrentConfig, opts ...ManagerOption) (*Manager, error) {
if identity == nil {
return nil, errors.New("empty identity")
@ -236,6 +247,10 @@ func NewManager(identity *ecdsa.PrivateKey, db *sql.DB, encryptor *encryption.Pr
manager.walletConfig = managerConfig.walletConfig
}
if managerConfig.collectiblesService != nil {
manager.collectiblesService = managerConfig.collectiblesService
}
if verifier != nil {
sub := verifier.Subscribe()
@ -4036,11 +4051,11 @@ func findIndexFile(files []*torrent.File) (index int, ok bool) {
return 0, false
}
func (m *Manager) GetCommunityTokens(communityID string) ([]*CommunityToken, error) {
func (m *Manager) GetCommunityTokens(communityID string) ([]*community_token.CommunityToken, error) {
return m.persistence.GetCommunityTokens(communityID)
}
func (m *Manager) GetAllCommunityTokens() ([]*CommunityToken, error) {
func (m *Manager) GetAllCommunityTokens() ([]*community_token.CommunityToken, error) {
return m.persistence.GetAllCommunityTokens()
}
@ -4065,7 +4080,16 @@ func (m *Manager) ImageToBase64(uri string) string {
return base64img
}
func (m *Manager) SaveCommunityToken(token *CommunityToken, croppedImage *images.CroppedImage) (*CommunityToken, error) {
func (m *Manager) SaveCommunityToken(token *community_token.CommunityToken, croppedImage *images.CroppedImage) (*community_token.CommunityToken, error) {
community, err := m.GetByIDString(token.CommunityID)
if err != nil {
return nil, err
}
if community == nil {
return nil, ErrOrgNotFound
}
if croppedImage != nil && croppedImage.ImagePath != "" {
bytes, err := images.OpenAndAdjustImage(*croppedImage, true)
if err != nil {
@ -4116,7 +4140,7 @@ func (m *Manager) AddCommunityToken(communityID string, chainID int, address str
return m.saveAndPublish(community)
}
func (m *Manager) UpdateCommunityTokenState(chainID int, contractAddress string, deployState DeployState) error {
func (m *Manager) UpdateCommunityTokenState(chainID int, contractAddress string, deployState community_token.DeployState) error {
return m.persistence.UpdateCommunityTokenState(chainID, contractAddress, deployState)
}
@ -4306,3 +4330,76 @@ func (m *Manager) ReevaluatePrivelegedMember(community *Community, tokenPermissi
return alreadyHasPrivilegedRole, nil
}
func (m *Manager) HandleCommunityTokensMetadata(communityID string, communityTokens []*protobuf.CommunityTokenMetadata) error {
for _, tokenMetadata := range communityTokens {
for chainID, address := range tokenMetadata.ContractAddresses {
exists, err := m.persistence.HasCommunityToken(communityID, address, int(chainID))
if err != nil {
return err
}
if !exists {
communityToken := &community_token.CommunityToken{
CommunityID: communityID,
Address: address,
TokenType: tokenMetadata.TokenType,
Name: tokenMetadata.Name,
Symbol: tokenMetadata.Symbol,
Description: tokenMetadata.Description,
Transferable: true,
RemoteSelfDestruct: false,
ChainID: int(chainID),
DeployState: community_token.Deployed,
Base64Image: tokenMetadata.Image,
Decimals: int(tokenMetadata.Decimals),
}
callOpts := &bind.CallOpts{Context: context.Background(), Pending: false}
switch tokenMetadata.TokenType {
case protobuf.CommunityTokenType_ERC721:
contract, err := m.collectiblesService.API().GetContractInstance(chainID, address)
if err != nil {
return err
}
totalSupply, err := contract.TotalSupply(callOpts)
if err != nil {
return err
}
transferable, err := contract.Transferable(callOpts)
if err != nil {
return err
}
remoteBurnable, err := contract.RemoteBurnable(callOpts)
if err != nil {
return err
}
communityToken.Supply = &bigint.BigInt{Int: totalSupply}
communityToken.Transferable = transferable
communityToken.RemoteSelfDestruct = remoteBurnable
communityToken.InfiniteSupply = collectibles.GetInfiniteSupply().Cmp(totalSupply) == 0
case protobuf.CommunityTokenType_ERC20:
contract, err := m.collectiblesService.API().GetAssetContractInstance(chainID, address)
if err != nil {
return err
}
totalSupply, err := contract.TotalSupply(callOpts)
if err != nil {
return err
}
communityToken.Supply = &bigint.BigInt{Int: totalSupply}
communityToken.InfiniteSupply = collectibles.GetInfiniteSupply().Cmp(totalSupply) == 0
}
err = m.persistence.AddCommunityToken(communityToken)
if err != nil {
return err
}
}
}
}
return nil
}

View File

@ -17,6 +17,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/bigint"
)
@ -1176,7 +1177,7 @@ func (p *Persistence) GetCommunityChatIDs(communityID types.HexBytes) ([]string,
return ids, nil
}
func (p *Persistence) GetAllCommunityTokens() ([]*CommunityToken, error) {
func (p *Persistence) GetAllCommunityTokens() ([]*token.CommunityToken, error) {
rows, err := p.db.Query(`SELECT community_id, address, type, name, symbol, description, supply_str,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals
FROM community_tokens`)
@ -1188,7 +1189,7 @@ func (p *Persistence) GetAllCommunityTokens() ([]*CommunityToken, error) {
return p.getCommunityTokensInternal(rows)
}
func (p *Persistence) GetCommunityTokens(communityID string) ([]*CommunityToken, error) {
func (p *Persistence) GetCommunityTokens(communityID string) ([]*token.CommunityToken, error) {
rows, err := p.db.Query(`SELECT community_id, address, type, name, symbol, description, supply_str,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals
FROM community_tokens WHERE community_id = ?`, communityID)
@ -1200,8 +1201,8 @@ func (p *Persistence) GetCommunityTokens(communityID string) ([]*CommunityToken,
return p.getCommunityTokensInternal(rows)
}
func (p *Persistence) GetCommunityToken(communityID string, chainID int, address string) (*CommunityToken, error) {
token := CommunityToken{}
func (p *Persistence) GetCommunityToken(communityID string, chainID int, address string) (*token.CommunityToken, error) {
token := token.CommunityToken{}
var supplyStr string
err := p.db.QueryRow(`SELECT community_id, address, type, name, symbol, description, supply_str, infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals FROM community_tokens WHERE community_id = ? AND chain_id = ? AND address = ?`, communityID, chainID, address).Scan(&token.CommunityID, &token.Address, &token.TokenType, &token.Name,
&token.Symbol, &token.Description, &supplyStr, &token.InfiniteSupply, &token.Transferable,
@ -1220,11 +1221,11 @@ func (p *Persistence) GetCommunityToken(communityID string, chainID int, address
return &token, nil
}
func (p *Persistence) getCommunityTokensInternal(rows *sql.Rows) ([]*CommunityToken, error) {
tokens := []*CommunityToken{}
func (p *Persistence) getCommunityTokensInternal(rows *sql.Rows) ([]*token.CommunityToken, error) {
tokens := []*token.CommunityToken{}
for rows.Next() {
token := CommunityToken{}
token := token.CommunityToken{}
var supplyStr string
err := rows.Scan(&token.CommunityID, &token.Address, &token.TokenType, &token.Name,
&token.Symbol, &token.Description, &supplyStr, &token.InfiniteSupply, &token.Transferable,
@ -1245,7 +1246,16 @@ func (p *Persistence) getCommunityTokensInternal(rows *sql.Rows) ([]*CommunityTo
return tokens, nil
}
func (p *Persistence) AddCommunityToken(token *CommunityToken) error {
func (p *Persistence) HasCommunityToken(communityID string, address string, chainID int) (bool, error) {
var count int
err := p.db.QueryRow(`SELECT count(1) FROM community_tokens WHERE community_id = ? AND address = ? AND chain_id = ?`, communityID, address, chainID).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
func (p *Persistence) AddCommunityToken(token *token.CommunityToken) error {
_, err := p.db.Exec(`INSERT INTO community_tokens (community_id, address, type, name, symbol, description, supply_str,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, token.CommunityID, token.Address, token.TokenType, token.Name,
@ -1254,7 +1264,7 @@ func (p *Persistence) AddCommunityToken(token *CommunityToken) error {
return err
}
func (p *Persistence) UpdateCommunityTokenState(chainID int, contractAddress string, deployState DeployState) error {
func (p *Persistence) UpdateCommunityTokenState(chainID int, contractAddress string, deployState token.DeployState) error {
_, err := p.db.Exec(`UPDATE community_tokens SET deploy_state = ? WHERE address = ? AND chain_id = ?`, deployState, contractAddress, chainID)
return err
}

View File

@ -14,6 +14,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/services/wallet/bigint"
@ -378,7 +379,7 @@ func (s *PersistenceSuite) TestGetCommunityToken() {
s.Require().NoError(err)
s.Require().Len(tokens, 0)
tokenERC721 := CommunityToken{
tokenERC721 := token.CommunityToken{
CommunityID: "123",
TokenType: protobuf.CommunityTokenType_ERC721,
Address: "0x123",
@ -390,7 +391,7 @@ func (s *PersistenceSuite) TestGetCommunityToken() {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: InProgress,
DeployState: token.InProgress,
Base64Image: "ABCD",
}
@ -407,7 +408,7 @@ func (s *PersistenceSuite) TestGetCommunityTokens() {
s.Require().NoError(err)
s.Require().Len(tokens, 0)
tokenERC721 := CommunityToken{
tokenERC721 := token.CommunityToken{
CommunityID: "123",
TokenType: protobuf.CommunityTokenType_ERC721,
Address: "0x123",
@ -419,11 +420,11 @@ func (s *PersistenceSuite) TestGetCommunityTokens() {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: InProgress,
DeployState: token.InProgress,
Base64Image: "ABCD",
}
tokenERC20 := CommunityToken{
tokenERC20 := token.CommunityToken{
CommunityID: "345",
TokenType: protobuf.CommunityTokenType_ERC20,
Address: "0x345",
@ -435,7 +436,7 @@ func (s *PersistenceSuite) TestGetCommunityTokens() {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 2,
DeployState: Failed,
DeployState: token.Failed,
Base64Image: "QWERTY",
Decimals: 21,
}
@ -450,12 +451,12 @@ func (s *PersistenceSuite) TestGetCommunityTokens() {
s.Require().Len(tokens, 1)
s.Require().Equal(tokenERC721, *tokens[0])
err = s.db.UpdateCommunityTokenState(1, "0x123", Deployed)
err = s.db.UpdateCommunityTokenState(1, "0x123", token.Deployed)
s.Require().NoError(err)
tokens, err = s.db.GetCommunityTokens("123")
s.Require().NoError(err)
s.Require().Len(tokens, 1)
s.Require().Equal(Deployed, tokens[0].DeployState)
s.Require().Equal(token.Deployed, tokens[0].DeployState)
tokens, err = s.db.GetCommunityTokens("345")
s.Require().NoError(err)

View File

@ -0,0 +1,31 @@
package token
import (
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/bigint"
)
type DeployState uint8
const (
Failed DeployState = iota
InProgress
Deployed
)
type CommunityToken struct {
TokenType protobuf.CommunityTokenType `json:"tokenType"`
CommunityID string `json:"communityId"`
Address string `json:"address"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Description string `json:"description"`
Supply *bigint.BigInt `json:"supply"`
InfiniteSupply bool `json:"infiniteSupply"`
Transferable bool `json:"transferable"`
RemoteSelfDestruct bool `json:"remoteSelfDestruct"`
ChainID int `json:"chainId"`
DeployState DeployState `json:"deployState"`
Base64Image string `json:"image"`
Decimals int `json:"decimals"`
}

View File

@ -13,6 +13,7 @@ import (
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/services/wallet/bigint"
@ -1689,7 +1690,7 @@ func testEventSenderCannotEditPrivilegedCommunityPermission(base CommunityEvents
}
func testEventSenderAddedCommunityToken(base CommunityEventsTestsInterface, community *communities.Community) {
tokenERC721 := &communities.CommunityToken{
tokenERC721 := &token.CommunityToken{
CommunityID: community.IDString(),
TokenType: protobuf.CommunityTokenType_ERC721,
Address: "0x123",
@ -1701,7 +1702,7 @@ func testEventSenderAddedCommunityToken(base CommunityEventsTestsInterface, comm
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: communities.Deployed,
DeployState: token.Deployed,
Base64Image: "ABCD",
}

View File

@ -13,7 +13,7 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/services/wallet/bigint"
@ -253,7 +253,7 @@ func (s *AdminCommunityEventsSuite) TestAdminPinMessage() {
func (s *AdminCommunityEventsSuite) TestAdminAddCommunityToken() {
community := setUpCommunityAndRoles(s, protobuf.CommunityMember_ROLE_ADMIN)
tokenERC721 := &communities.CommunityToken{
tokenERC721 := &token.CommunityToken{
CommunityID: community.IDString(),
TokenType: protobuf.CommunityTokenType_ERC721,
Address: "0x123",
@ -265,7 +265,7 @@ func (s *AdminCommunityEventsSuite) TestAdminAddCommunityToken() {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: communities.Deployed,
DeployState: token.Deployed,
Base64Image: "ABCD",
}

View File

@ -450,6 +450,10 @@ func NewMessenger(
managerOptions = append(managerOptions, communities.WithWalletConfig(c.walletConfig))
}
if c.collectiblesService != nil {
managerOptions = append(managerOptions, communities.WithCollectiblesService(c.collectiblesService))
}
communitiesManager, err := communities.NewManager(identity, database, encryptionProtocol, logger, ensVerifier, transp, c.torrentConfig, managerOptions...)
if err != nil {
return nil, err

View File

@ -31,6 +31,7 @@ import (
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/discord"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
@ -2549,6 +2550,20 @@ func (m *Messenger) handleSyncCommunity(messageState *ReceivedMessageState, sync
}
}
id := crypto.CompressPubkey(orgPubKey)
savedCommunity, err := m.communitiesManager.GetByID(id)
if err != nil {
return err
}
if savedCommunity.HasPermissionToSendCommunityEvents() || savedCommunity.IsControlNode() {
err := m.handleCommunityTokensMetadata(savedCommunity.IDString(), savedCommunity.CommunityTokensMetadata())
if err != nil {
logger.Debug("m.handleCommunityTokensMetadata", zap.Error(err))
return err
}
}
// if we are not waiting for approval, join or leave the community
if !pending {
var mr *MessengerResponse
@ -2604,6 +2619,10 @@ func (m *Messenger) handleSyncCommunitySettings(messageState *ReceivedMessageSta
return nil
}
func (m *Messenger) handleCommunityTokensMetadata(communityID string, communityTokens []*protobuf.CommunityTokenMetadata) error {
return m.communitiesManager.HandleCommunityTokensMetadata(communityID, communityTokens)
}
func (m *Messenger) InitHistoryArchiveTasks(communities []*communities.Community) {
m.communitiesManager.LogStdout("initializing history archive tasks")
@ -4049,15 +4068,15 @@ func (m *Messenger) chatMessagesToWakuMessages(chatMessages []*common.Message, c
return wakuMessages, nil
}
func (m *Messenger) GetCommunityTokens(communityID string) ([]*communities.CommunityToken, error) {
func (m *Messenger) GetCommunityTokens(communityID string) ([]*token.CommunityToken, error) {
return m.communitiesManager.GetCommunityTokens(communityID)
}
func (m *Messenger) GetAllCommunityTokens() ([]*communities.CommunityToken, error) {
func (m *Messenger) GetAllCommunityTokens() ([]*token.CommunityToken, error) {
return m.communitiesManager.GetAllCommunityTokens()
}
func (m *Messenger) SaveCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
func (m *Messenger) SaveCommunityToken(token *token.CommunityToken, croppedImage *images.CroppedImage) (*token.CommunityToken, error) {
return m.communitiesManager.SaveCommunityToken(token, croppedImage)
}
@ -4065,7 +4084,7 @@ func (m *Messenger) AddCommunityToken(communityID string, chainID int, address s
return m.communitiesManager.AddCommunityToken(communityID, chainID, address)
}
func (m *Messenger) UpdateCommunityTokenState(chainID int, contractAddress string, deployState communities.DeployState) error {
func (m *Messenger) UpdateCommunityTokenState(chainID int, contractAddress string, deployState token.DeployState) error {
return m.communitiesManager.UpdateCommunityTokenState(chainID, contractAddress, deployState)
}

View File

@ -24,6 +24,7 @@ import (
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/protocol/transport"
"github.com/status-im/status-go/protocol/wakusync"
"github.com/status-im/status-go/services/collectibles"
"github.com/status-im/status-go/services/mailservers"
"github.com/status-im/status-go/services/wallet"
)
@ -88,6 +89,7 @@ type config struct {
torrentConfig *params.TorrentConfig
walletConfig *params.WalletConfig
walletService *wallet.Service
collectiblesService *collectibles.Service
httpServer *server.MediaServer
rpcClient *rpc.Client
tokenManager communities.TokenManager
@ -336,6 +338,13 @@ func WithWalletService(s *wallet.Service) Option {
}
}
func WithCollectiblesService(s *collectibles.Service) Option {
return func(c *config) error {
c.collectiblesService = s
return nil
}
}
func WithTokenManager(tokenManager communities.TokenManager) Option {
return func(c *config) error {
c.tokenManager = tokenManager

View File

@ -70,6 +70,10 @@ func (d *DeploymentParameters) GetSupply() *big.Int {
// infinite supply for ERC721 is 2^256-1
func (d *DeploymentParameters) GetInfiniteSupply() *big.Int {
return GetInfiniteSupply()
}
func GetInfiniteSupply() *big.Int {
max := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
max.Sub(max, big.NewInt(1))
return max
@ -407,6 +411,22 @@ func (api *API) EstimateRemoteBurn(ctx context.Context, chainID uint64, contract
return api.estimateMethod(ctx, chainID, contractAddress, "remoteBurn", tempTokenIds)
}
func (api *API) GetContractInstance(chainID uint64, contractAddress string) (*collectibles.Collectibles, error) {
contractInst, err := api.newCollectiblesInstance(chainID, contractAddress)
if err != nil {
return nil, err
}
return contractInst, nil
}
func (api *API) GetAssetContractInstance(chainID uint64, contractAddress string) (*assets.Assets, error) {
contractInst, err := api.newAssetsInstance(chainID, contractAddress)
if err != nil {
return nil, err
}
return contractInst, nil
}
func (api *API) ContractOwner(ctx context.Context, chainID uint64, contractAddress string) (string, error) {
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
tokenType, err := api.db.GetTokenType(chainID, contractAddress)

View File

@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/services/wallet/bigint"
@ -25,7 +25,7 @@ type DatabaseSuite struct {
db *Database
}
func (s *DatabaseSuite) addCommunityToken(db *sql.DB, token *communities.CommunityToken) error {
func (s *DatabaseSuite) addCommunityToken(db *sql.DB, token *token.CommunityToken) error {
_, err := db.Exec(`INSERT INTO community_tokens (community_id, address, type, name, symbol, description, supply_str,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, token.CommunityID, token.Address, token.TokenType, token.Name,
@ -35,7 +35,7 @@ func (s *DatabaseSuite) addCommunityToken(db *sql.DB, token *communities.Communi
}
func (s *DatabaseSuite) setupDatabase(db *sql.DB) error {
token721 := &communities.CommunityToken{
token721 := &token.CommunityToken{
CommunityID: "123",
TokenType: protobuf.CommunityTokenType_ERC721,
Address: "0x123",
@ -47,11 +47,11 @@ func (s *DatabaseSuite) setupDatabase(db *sql.DB) error {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: communities.InProgress,
DeployState: token.InProgress,
Base64Image: "ABCD",
}
token20 := &communities.CommunityToken{
token20 := &token.CommunityToken{
CommunityID: "345",
TokenType: protobuf.CommunityTokenType_ERC20,
Address: "0x345",
@ -63,7 +63,7 @@ func (s *DatabaseSuite) setupDatabase(db *sql.DB) error {
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 2,
DeployState: communities.Failed,
DeployState: token.Failed,
Base64Image: "QWERTY",
Decimals: 21,
}

View File

@ -28,6 +28,10 @@ func (s *Service) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
func (s *Service) API() *API {
return s.api
}
// APIs returns a list of new APIs.
func (s *Service) APIs() []ethRpc.API {
return []ethRpc.API{

View File

@ -25,6 +25,7 @@ import (
"github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/discord"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/linkpreview"
@ -1318,15 +1319,15 @@ func (api *PublicAPI) BuildContact(request *requests.BuildContact) (*protocol.Co
return api.service.messenger.BuildContact(request)
}
func (api *PublicAPI) GetCommunityTokens(communityID string) ([]*communities.CommunityToken, error) {
func (api *PublicAPI) GetCommunityTokens(communityID string) ([]*token.CommunityToken, error) {
return api.service.messenger.GetCommunityTokens(communityID)
}
func (api *PublicAPI) GetAllCommunityTokens() ([]*communities.CommunityToken, error) {
func (api *PublicAPI) GetAllCommunityTokens() ([]*token.CommunityToken, error) {
return api.service.messenger.GetAllCommunityTokens()
}
func (api *PublicAPI) SaveCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
func (api *PublicAPI) SaveCommunityToken(token *token.CommunityToken, croppedImage *images.CroppedImage) (*token.CommunityToken, error) {
return api.service.messenger.SaveCommunityToken(token, croppedImage)
}
@ -1334,7 +1335,7 @@ func (api *PublicAPI) AddCommunityToken(communityID string, chainID int, address
return api.service.messenger.AddCommunityToken(communityID, chainID, address)
}
func (api *PublicAPI) UpdateCommunityTokenState(chainID int, contractAddress string, deployState communities.DeployState) error {
func (api *PublicAPI) UpdateCommunityTokenState(chainID int, contractAddress string, deployState token.DeployState) error {
return api.service.messenger.UpdateCommunityTokenState(chainID, contractAddress, deployState)
}

View File

@ -44,6 +44,7 @@ import (
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/server"
"github.com/status-im/status-go/services/browsers"
"github.com/status-im/status-go/services/collectibles"
"github.com/status-im/status-go/services/ext/mailservers"
localnotifications "github.com/status-im/status-go/services/local-notifications"
mailserversDB "github.com/status-im/status-go/services/mailservers"
@ -115,7 +116,7 @@ func (s *Service) GetPeer(rawURL string) (*enode.Node, error) {
return enode.ParseV4(rawURL)
}
func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db *sql.DB, httpServer *server.MediaServer, multiAccountDb *multiaccounts.Database, acc *multiaccounts.Account, accountManager *account.GethManager, rpcClient *rpc.Client, walletService *wallet.Service, logger *zap.Logger) error {
func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db *sql.DB, httpServer *server.MediaServer, multiAccountDb *multiaccounts.Database, acc *multiaccounts.Account, accountManager *account.GethManager, rpcClient *rpc.Client, walletService *wallet.Service, collectiblesService *collectibles.Service, logger *zap.Logger) error {
var err error
if !s.config.ShhextConfig.PFSEnabled {
return nil
@ -154,7 +155,7 @@ func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db *
s.multiAccountsDB = multiAccountDb
s.account = acc
options, err := buildMessengerOptions(s.config, identity, db, httpServer, s.rpcClient, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, walletService, logger, &MessengerSignalsHandler{})
options, err := buildMessengerOptions(s.config, identity, db, httpServer, s.rpcClient, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, walletService, collectiblesService, logger, &MessengerSignalsHandler{})
if err != nil {
return err
}
@ -411,6 +412,7 @@ func buildMessengerOptions(
envelopesMonitorConfig *transport.EnvelopesMonitorConfig,
accountsDB *accounts.Database,
walletService *wallet.Service,
collectiblesService *collectibles.Service,
logger *zap.Logger,
messengerSignalsHandler protocol.MessengerSignalsHandler,
) ([]protocol.Option, error) {
@ -432,6 +434,7 @@ func buildMessengerOptions(
protocol.WithMessageCSV(config.OutputMessageCSVEnabled),
protocol.WithWalletConfig(&config.WalletConfig),
protocol.WithWalletService(walletService),
protocol.WithCollectiblesService(collectiblesService),
}
if config.ShhextConfig.DataSyncEnabled {

View File

@ -133,7 +133,7 @@ func TestInitProtocol(t *testing.T) {
acc := &multiaccounts.Account{KeyUID: "0xdeadbeef"}
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, nil, zap.NewNop())
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, nil, nil, zap.NewNop())
require.NoError(t, err)
}
@ -198,7 +198,7 @@ func (s *ShhExtSuite) createAndAddNode() {
acc := &multiaccounts.Account{KeyUID: "0xdeadbeef"}
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, nil, zap.NewNop())
err = service.InitProtocol("Test", privateKey, sqlDB, nil, multiAccounts, acc, nil, nil, nil, nil, zap.NewNop())
s.NoError(err)
stack.RegisterLifecycle(service)