Fix: cover case where criteria doesn't have token IDs

This commit is contained in:
Icaro Motta 2024-02-06 02:41:46 -03:00 committed by Andrea Maria Piana
parent dd03b628af
commit 31277f1e9e
2 changed files with 41 additions and 5 deletions

View File

@ -88,6 +88,11 @@ func calculatePermissionedBalancesERC20(
} }
func isERC721CriteriaSatisfied(tokenBalances []thirdparty.TokenBalance, criteria *protobuf.TokenCriteria) bool { func isERC721CriteriaSatisfied(tokenBalances []thirdparty.TokenBalance, criteria *protobuf.TokenCriteria) bool {
// No token IDs to compare against, so the criteria is satisfied.
if len(criteria.TokenIds) == 0 {
return true
}
for _, tokenID := range criteria.TokenIds { for _, tokenID := range criteria.TokenIds {
tokenIDBigInt := new(big.Int).SetUint64(tokenID) tokenIDBigInt := new(big.Int).SetUint64(tokenID)
for _, asset := range tokenBalances { for _, asset := range tokenBalances {
@ -96,10 +101,11 @@ func isERC721CriteriaSatisfied(tokenBalances []thirdparty.TokenBalance, criteria
} }
} }
} }
return false return false
} }
func calculatePermissionedBalancesERC721( func (m *Manager) calculatePermissionedBalancesERC721(
accountAddresses []gethcommon.Address, accountAddresses []gethcommon.Address,
balances CollectiblesByChain, balances CollectiblesByChain,
tokenPermissions []*CommunityTokenPermission, tokenPermissions []*CommunityTokenPermission,
@ -166,7 +172,7 @@ func calculatePermissionedBalancesERC721(
return res return res
} }
func calculatePermissionedBalances( func (m *Manager) calculatePermissionedBalances(
chainIDs []uint64, chainIDs []uint64,
accountAddresses []gethcommon.Address, accountAddresses []gethcommon.Address,
erc20Balances BalancesByChain, erc20Balances BalancesByChain,
@ -175,7 +181,7 @@ func calculatePermissionedBalances(
) map[gethcommon.Address][]PermissionedBalance { ) map[gethcommon.Address][]PermissionedBalance {
res := make(map[gethcommon.Address][]PermissionedBalance, 0) res := make(map[gethcommon.Address][]PermissionedBalance, 0)
aggregatedERC721Balances := calculatePermissionedBalancesERC721(accountAddresses, erc721Balances, tokenPermissions) aggregatedERC721Balances := m.calculatePermissionedBalancesERC721(accountAddresses, erc721Balances, tokenPermissions)
for accountAddress, tokens := range aggregatedERC721Balances { for accountAddress, tokens := range aggregatedERC721Balances {
for _, permissionedToken := range tokens { for _, permissionedToken := range tokens {
if permissionedToken.Amount.Sign() > 0 { if permissionedToken.Amount.Sign() > 0 {
@ -274,5 +280,5 @@ func (m *Manager) GetPermissionedBalances(
erc721Balances = balances erc721Balances = balances
} }
return calculatePermissionedBalances(allChainIDs, accountAddresses, erc20Balances, erc721Balances, tokenPermissions), nil return m.calculatePermissionedBalances(allChainIDs, accountAddresses, erc20Balances, erc721Balances, tokenPermissions), nil
} }

View File

@ -16,6 +16,8 @@ import (
) )
func (s *ManagerSuite) Test_calculatePermissionedBalances() { func (s *ManagerSuite) Test_calculatePermissionedBalances() {
m, _, _ := s.setupManagerForTokenPermissions()
var mainnetID uint64 = 1 var mainnetID uint64 = 1
var arbitrumID uint64 = 42161 var arbitrumID uint64 = 42161
var gnosisID uint64 = 100 var gnosisID uint64 = 100
@ -26,6 +28,7 @@ func (s *ManagerSuite) Test_calculatePermissionedBalances() {
arbitrumETHContractAddress := gethcommon.HexToAddress("0xB") arbitrumETHContractAddress := gethcommon.HexToAddress("0xB")
mainnetTMasterAddress := gethcommon.HexToAddress("0x123") mainnetTMasterAddress := gethcommon.HexToAddress("0x123")
mainnetOwnerAddress := gethcommon.HexToAddress("0x1234") mainnetOwnerAddress := gethcommon.HexToAddress("0x1234")
mainnetTMaster_NoTokenIDsAddress := gethcommon.HexToAddress("0x456")
account1Address := gethcommon.HexToAddress("0x1") account1Address := gethcommon.HexToAddress("0x1")
account2Address := gethcommon.HexToAddress("0x2") account2Address := gethcommon.HexToAddress("0x2")
@ -50,6 +53,12 @@ func (s *ManagerSuite) Test_calculatePermissionedBalances() {
erc20Balances[mainnetID][account2Address] = make(map[gethcommon.Address]*hexutil.Big) erc20Balances[mainnetID][account2Address] = make(map[gethcommon.Address]*hexutil.Big)
erc20Balances[mainnetID][account2Address][mainnetSNTContractAddress] = intToBig(120) erc20Balances[mainnetID][account2Address][mainnetSNTContractAddress] = intToBig(120)
erc721Balances[mainnetID][account2Address] = make(thirdparty.TokenBalancesPerContractAddress) erc721Balances[mainnetID][account2Address] = make(thirdparty.TokenBalancesPerContractAddress)
erc721Balances[mainnetID][account2Address][mainnetTMaster_NoTokenIDsAddress] = []thirdparty.TokenBalance{
thirdparty.TokenBalance{
TokenID: uintToDecBig(1500),
Balance: uintToDecBig(3),
},
}
erc721Balances[mainnetID][account2Address][mainnetTMasterAddress] = []thirdparty.TokenBalance{ erc721Balances[mainnetID][account2Address][mainnetTMasterAddress] = []thirdparty.TokenBalance{
thirdparty.TokenBalance{ thirdparty.TokenBalance{
TokenID: uintToDecBig(456), TokenID: uintToDecBig(456),
@ -85,6 +94,21 @@ func (s *ManagerSuite) Test_calculatePermissionedBalances() {
erc20Balances[gnosisID][gethcommon.HexToAddress("0xF")][gethcommon.HexToAddress("0x99")] = intToBig(5) erc20Balances[gnosisID][gethcommon.HexToAddress("0xF")][gethcommon.HexToAddress("0x99")] = intToBig(5)
tokenPermissions := []*CommunityTokenPermission{ tokenPermissions := []*CommunityTokenPermission{
&CommunityTokenPermission{
CommunityTokenPermission: &protobuf.CommunityTokenPermission{
Type: protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER,
// A criteria without token IDs should be considered satisfied.
TokenCriteria: []*protobuf.TokenCriteria{
&protobuf.TokenCriteria{
Type: protobuf.CommunityTokenType_ERC721,
Symbol: "TM_NO_TOKEN_IDS",
Name: "TMaster-NoTokenIDs",
Amount: "1",
ContractAddresses: map[uint64]string{mainnetID: mainnetTMaster_NoTokenIDsAddress.Hex()},
},
},
},
},
&CommunityTokenPermission{ &CommunityTokenPermission{
CommunityTokenPermission: &protobuf.CommunityTokenPermission{ CommunityTokenPermission: &protobuf.CommunityTokenPermission{
Type: protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER, Type: protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER,
@ -176,7 +200,7 @@ func (s *ManagerSuite) Test_calculatePermissionedBalances() {
}, },
} }
actual := calculatePermissionedBalances( actual := m.calculatePermissionedBalances(
chainIDs, chainIDs,
accountAddresses, accountAddresses,
erc20Balances, erc20Balances,
@ -209,6 +233,12 @@ func (s *ManagerSuite) Test_calculatePermissionedBalances() {
Decimals: 16, Decimals: 16,
Amount: &bigint.BigInt{Int: big.NewInt(120)}, Amount: &bigint.BigInt{Int: big.NewInt(120)},
}, },
PermissionedBalance{
Type: protobuf.CommunityTokenType_ERC721,
Symbol: "TM_NO_TOKEN_IDS",
Name: "TMaster-NoTokenIDs",
Amount: &bigint.BigInt{Int: big.NewInt(1)},
},
PermissionedBalance{ PermissionedBalance{
Type: protobuf.CommunityTokenType_ERC721, Type: protobuf.CommunityTokenType_ERC721,
Symbol: "TMTEST", Symbol: "TMTEST",