diff --git a/protocol/communities_events_eventual_consistency_test.go b/protocol/communities_events_eventual_consistency_test.go index cd2597b0f..a98674300 100644 --- a/protocol/communities_events_eventual_consistency_test.go +++ b/protocol/communities_events_eventual_consistency_test.go @@ -27,6 +27,8 @@ func (s *CommunityEventsEventualConsistencySuite) SetupTest() { s.logger = tt.MustCreateTestLogger() s.collectiblesServiceMock = &CollectiblesServiceMock{} + s.mockedBalances = createMockedWalletBalance(&s.Suite) + config := waku.DefaultConfig config.MinimumAcceptedPoW = 0 shh := waku.New(&config, s.logger) @@ -48,7 +50,6 @@ func (s *CommunityEventsEventualConsistencySuite) SetupTest() { _, err = s.alice.Start() s.Require().NoError(err) - s.mockedBalances = createMockedWalletBalance(&s.Suite) } func (s *CommunityEventsEventualConsistencySuite) TearDownTest() { diff --git a/protocol/communities_events_owner_without_community_key_test.go b/protocol/communities_events_owner_without_community_key_test.go index 52423d20a..4113d766a 100644 --- a/protocol/communities_events_owner_without_community_key_test.go +++ b/protocol/communities_events_owner_without_community_key_test.go @@ -61,6 +61,8 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) SetupTest() { s.logger = tt.MustCreateTestLogger() s.collectiblesServiceMock = &CollectiblesServiceMock{} + s.mockedBalances = createMockedWalletBalance(&s.Suite) + config := waku.DefaultConfig config.MinimumAcceptedPoW = 0 shh := waku.New(&config, s.logger) @@ -76,8 +78,6 @@ func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) SetupTest() { s.Require().NoError(err) _, err = s.alice.Start() s.Require().NoError(err) - - s.mockedBalances = createMockedWalletBalance(&s.Suite) } func (s *OwnerWithoutCommunityKeyCommunityEventsSuite) TearDownTest() { diff --git a/protocol/communities_events_token_master_test.go b/protocol/communities_events_token_master_test.go index e0741f138..8eb3d6551 100644 --- a/protocol/communities_events_token_master_test.go +++ b/protocol/communities_events_token_master_test.go @@ -61,6 +61,8 @@ func (s *TokenMasterCommunityEventsSuite) SetupTest() { s.logger = tt.MustCreateTestLogger() s.collectiblesServiceMock = &CollectiblesServiceMock{} + s.mockedBalances = createMockedWalletBalance(&s.Suite) + config := waku.DefaultConfig config.MinimumAcceptedPoW = 0 shh := waku.New(&config, s.logger) @@ -77,7 +79,6 @@ func (s *TokenMasterCommunityEventsSuite) SetupTest() { _, err = s.alice.Start() s.Require().NoError(err) - s.mockedBalances = createMockedWalletBalance(&s.Suite) } func (s *TokenMasterCommunityEventsSuite) TearDownTest() { diff --git a/protocol/communities_messenger_admin_test.go b/protocol/communities_messenger_admin_test.go index 714544dd9..d25029253 100644 --- a/protocol/communities_messenger_admin_test.go +++ b/protocol/communities_messenger_admin_test.go @@ -67,6 +67,8 @@ func (s *AdminCommunityEventsSuiteBase) SetupTest() { s.logger = tt.MustCreateTestLogger() s.collectiblesServiceMock = &CollectiblesServiceMock{} + s.mockedBalances = createMockedWalletBalance(&s.Suite) + config := waku.DefaultConfig config.MinimumAcceptedPoW = 0 shh := waku.New(&config, s.logger) @@ -82,8 +84,6 @@ func (s *AdminCommunityEventsSuiteBase) SetupTest() { s.Require().NoError(err) _, err = s.alice.Start() s.Require().NoError(err) - - s.mockedBalances = createMockedWalletBalance(&s.Suite) } func (s *AdminCommunityEventsSuiteBase) TearDownTest() { diff --git a/protocol/communities_messenger_helpers_test.go b/protocol/communities_messenger_helpers_test.go index e7e8dc930..14975231b 100644 --- a/protocol/communities_messenger_helpers_test.go +++ b/protocol/communities_messenger_helpers_test.go @@ -5,6 +5,7 @@ import ( "crypto/ecdsa" "encoding/json" "errors" + "math/big" "sync" "time" @@ -24,6 +25,7 @@ import ( "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" walletCommon "github.com/status-im/status-go/services/wallet/common" "github.com/status-im/status-go/services/wallet/thirdparty" walletToken "github.com/status-im/status-go/services/wallet/token" @@ -75,34 +77,84 @@ func (m *TokenManagerMock) FindOrCreateTokenByAddress(ctx context.Context, chain } type CollectiblesManagerMock struct { - response map[thirdparty.CollectibleUniqueID][]thirdparty.AccountBalance + Balances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big + collectibleOwnershipResponse map[string][]thirdparty.AccountBalance } func (m *CollectiblesManagerMock) FetchBalancesByOwnerAndContractAddress(ctx context.Context, chainID walletCommon.ChainID, ownerAddress gethcommon.Address, contractAddresses []gethcommon.Address) (thirdparty.TokenBalancesPerContractAddress, error) { - return nil, errors.New("FetchBalancesByOwnerAndContractAddress is not implemented for testCollectiblesManager") + ret := make(thirdparty.TokenBalancesPerContractAddress) + accountsBalances, ok := (*m.Balances)[uint64(chainID)] + if !ok { + return ret, nil + } + + balances, ok := accountsBalances[ownerAddress] + if !ok { + return ret, nil + } + + for _, contractAddress := range contractAddresses { + balance, ok := balances[contractAddress] + if ok { + ret[contractAddress] = []thirdparty.TokenBalance{ + { + TokenID: &bigint.BigInt{}, + Balance: &bigint.BigInt{ + Int: (*big.Int)(balance), + }, + }, + } + } + } + + return ret, nil } func (m *CollectiblesManagerMock) GetCollectibleOwnership(requestedID thirdparty.CollectibleUniqueID) ([]thirdparty.AccountBalance, error) { - // NOTE: TokenID inside of thirdparty.CollectibleUniqueID is a pointer so m.response[id] is now working - for id, balances := range m.response { - if id.ContractID.Address == requestedID.ContractID.Address && - id.ContractID.ChainID == requestedID.ContractID.ChainID { + for id, balances := range m.collectibleOwnershipResponse { + if id == requestedID.HashKey() { return balances, nil } } return []thirdparty.AccountBalance{}, nil } -func (m *CollectiblesManagerMock) SetResponse(id thirdparty.CollectibleUniqueID, balances []thirdparty.AccountBalance) { - if m.response == nil { - m.response = map[thirdparty.CollectibleUniqueID][]thirdparty.AccountBalance{} +func (m *CollectiblesManagerMock) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress gethcommon.Address) (*thirdparty.CollectibleContractOwnership, error) { + ret := &thirdparty.CollectibleContractOwnership{ + ContractAddress: contractAddress, + Owners: []thirdparty.CollectibleOwner{}, } - m.response[id] = balances + accountsBalances, ok := (*m.Balances)[uint64(chainID)] + if !ok { + return ret, nil + } + + for wallet, collectiblesBalance := range accountsBalances { + balance, ok := collectiblesBalance[contractAddress] + if ok { + ret.Owners = append(ret.Owners, thirdparty.CollectibleOwner{ + OwnerAddress: wallet, + TokenBalances: []thirdparty.TokenBalance{ + { + TokenID: &bigint.BigInt{}, + Balance: &bigint.BigInt{ + Int: (*big.Int)(balance), + }, + }, + }, + }) + } + } + + return ret, nil } -func (m *CollectiblesManagerMock) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress gethcommon.Address) (*thirdparty.CollectibleContractOwnership, error) { - return nil, errors.New("FetchCollectibleOwnersByContractAddress is not implemented for CollectiblesManagerMock") +func (m *CollectiblesManagerMock) SetCollectibleOwnershipResponse(id thirdparty.CollectibleUniqueID, balances []thirdparty.AccountBalance) { + if m.collectibleOwnershipResponse == nil { + m.collectibleOwnershipResponse = map[string][]thirdparty.AccountBalance{} + } + m.collectibleOwnershipResponse[id.HashKey()] = balances } type CollectiblesServiceMock struct { @@ -260,7 +312,9 @@ func newTestCommunitiesMessenger(s *suite.Suite, waku types.Waku, config testCom Balances: config.mockedBalances, } - collectiblesManagerMock := &CollectiblesManagerMock{} + collectiblesManagerMock := &CollectiblesManagerMock{ + Balances: config.mockedBalances, + } options := []Option{ WithAccountManager(accountsManagerMock), diff --git a/protocol/communities_messenger_token_permissions_test.go b/protocol/communities_messenger_token_permissions_test.go index dbd34e8b7..3840c37db 100644 --- a/protocol/communities_messenger_token_permissions_test.go +++ b/protocol/communities_messenger_token_permissions_test.go @@ -158,6 +158,8 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() { s.bobWaku = nil s.aliceWaku = nil + s.resetMockedBalances() + s.logger = tt.MustCreateTestLogger() wakuNodes := CreateWakuV2Network(&s.Suite, s.logger, false, []string{"owner", "bob", "alice"}) @@ -178,9 +180,6 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() { s.Require().NoError(err) _, err = s.alice.Start() s.Require().NoError(err) - - s.resetMockedBalances() - } func (s *MessengerCommunitiesTokenPermissionsSuite) TearDownTest() { @@ -1602,15 +1601,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestMemberRoleGetUpdatedWhen s.Require().Equal(msg.Text, response.Messages()[0].Text) } -func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInOpenCommunity(permissionType protobuf.CommunityTokenPermission_Type) { +func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInOpenCommunity(permissionType protobuf.CommunityTokenPermission_Type, tokenType protobuf.CommunityTokenType) { community, _ := s.createCommunity() createTokenPermission := &requests.CreateCommunityTokenPermission{ CommunityID: community.ID(), Type: permissionType, TokenCriteria: []*protobuf.TokenCriteria{ - &protobuf.TokenCriteria{ - Type: protobuf.CommunityTokenType_ERC20, + { + Type: tokenType, ContractAddresses: map[uint64]string{testChainID1: "0x123"}, Symbol: "TEST", AmountInWei: "100000000000000000000", @@ -1705,23 +1704,31 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg s.Require().False(checkRoleBasedOnThePermissionType(permissionType, &s.alice.identity.PublicKey, community)) } -func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity() { - s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN) +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity_ERC20() { + s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC20) } -func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity() { - s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER) +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInOpenCommunity_ERC721() { + s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC721) } -func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInClosedCommunity(permissionType protobuf.CommunityTokenPermission_Type) { +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity_ERC20() { + s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC20) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInOpenCommunity_ERC721() { + s.testReevaluateMemberPrivilegedRoleInOpenCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC721) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivilegedRoleInClosedCommunity(permissionType protobuf.CommunityTokenPermission_Type, tokenType protobuf.CommunityTokenType) { community, _ := s.createCommunity() createTokenPermission := &requests.CreateCommunityTokenPermission{ CommunityID: community.ID(), Type: permissionType, TokenCriteria: []*protobuf.TokenCriteria{ - &protobuf.TokenCriteria{ - Type: protobuf.CommunityTokenType_ERC20, + { + Type: tokenType, ContractAddresses: map[uint64]string{testChainID1: "0x123"}, Symbol: "TEST", AmountInWei: "100000000000000000000", @@ -1740,8 +1747,8 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg CommunityID: community.ID(), Type: protobuf.CommunityTokenPermission_BECOME_MEMBER, TokenCriteria: []*protobuf.TokenCriteria{ - &protobuf.TokenCriteria{ - Type: protobuf.CommunityTokenType_ERC20, + { + Type: tokenType, ContractAddresses: map[uint64]string{testChainID1: "0x124"}, Symbol: "TEST2", AmountInWei: "100000000000000000000", @@ -1876,12 +1883,20 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) testReevaluateMemberPrivileg s.Require().False(checkRoleBasedOnThePermissionType(permissionType, &s.alice.identity.PublicKey, community)) } -func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity() { - s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN) +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity_ERC20() { + s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC20) } -func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity() { - s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER) +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberAdminRoleInClosedCommunity_ERC721() { + s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_ADMIN, protobuf.CommunityTokenType_ERC721) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity_ERC20() { + s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC20) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberTokenMasterRoleInClosedCommunity_ERC721() { + s.testReevaluateMemberPrivilegedRoleInClosedCommunity(protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, protobuf.CommunityTokenType_ERC721) } func checkRoleBasedOnThePermissionType(permissionType protobuf.CommunityTokenPermission_Type, member *ecdsa.PublicKey, community *communities.Community) bool { diff --git a/protocol/messenger_profile_showcase_test.go b/protocol/messenger_profile_showcase_test.go index 21e79f6de..8c724dee3 100644 --- a/protocol/messenger_profile_showcase_test.go +++ b/protocol/messenger_profile_showcase_test.go @@ -187,7 +187,7 @@ func (s *TestMessengerProfileShowcase) TestSaveAndGetProfileShowcasePreferences( TxTimestamp: 0, }, } - s.collectiblesMock.SetResponse(collectibleID, balances) + s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances) err = s.m.SetProfileShowcasePreferences(request, false) s.Require().NoError(err) @@ -258,7 +258,7 @@ func (s *TestMessengerProfileShowcase) TestFailToSaveProfileShowcasePreferencesW TxTimestamp: 0, }, } - s.collectiblesMock.SetResponse(collectibleID, balances) + s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances) err = s.m.SetProfileShowcasePreferences(request, false) s.Require().Equal(errorAccountVisibilityLowerThanCollectible, err) @@ -441,7 +441,7 @@ func (s *TestMessengerProfileShowcase) TestShareShowcasePreferences() { TxTimestamp: 32443424, }, } - s.collectiblesMock.SetResponse(collectibleID, balances) + s.collectiblesMock.SetCollectibleOwnershipResponse(collectibleID, balances) err = s.m.SetProfileShowcasePreferences(request, false) s.Require().NoError(err)