2023-07-25 17:51:10 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
w_common "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2023-07-25 17:51:10 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupOwnershipDBTest(t *testing.T) (*OwnershipDB, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return NewOwnershipDB(db), func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestCollectibles(chainID w_common.ChainID, count int) (result []thirdparty.CollectibleUniqueID) {
|
|
|
|
result = make([]thirdparty.CollectibleUniqueID, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
bigI := big.NewInt(int64(i))
|
|
|
|
newCollectible := thirdparty.CollectibleUniqueID{
|
2023-07-31 19:41:14 +00:00
|
|
|
ContractID: thirdparty.ContractID{
|
|
|
|
ChainID: chainID,
|
|
|
|
Address: common.BigToAddress(bigI),
|
|
|
|
},
|
|
|
|
TokenID: &bigint.BigInt{Int: bigI},
|
2023-07-25 17:51:10 +00:00
|
|
|
}
|
|
|
|
result = append(result, newCollectible)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateOwnership(t *testing.T) {
|
|
|
|
oDB, cleanDB := setupOwnershipDBTest(t)
|
|
|
|
defer cleanDB()
|
|
|
|
|
|
|
|
ownerAddress1 := common.HexToAddress("0x1234")
|
|
|
|
chainID0 := w_common.ChainID(0)
|
|
|
|
ownedListChain0 := generateTestCollectibles(chainID0, 10)
|
2023-09-12 18:35:31 +00:00
|
|
|
timestampChain0 := int64(1234567890)
|
2023-07-25 17:51:10 +00:00
|
|
|
chainID1 := w_common.ChainID(1)
|
|
|
|
ownedListChain1 := generateTestCollectibles(chainID1, 15)
|
2023-09-12 18:35:31 +00:00
|
|
|
timestampChain1 := int64(1234567891)
|
2023-07-25 17:51:10 +00:00
|
|
|
|
|
|
|
ownedList1 := append(ownedListChain0, ownedListChain1...)
|
|
|
|
|
|
|
|
ownerAddress2 := common.HexToAddress("0x5678")
|
|
|
|
chainID2 := w_common.ChainID(2)
|
|
|
|
ownedListChain2 := generateTestCollectibles(chainID2, 20)
|
2023-09-12 18:35:31 +00:00
|
|
|
timestampChain2 := int64(1234567892)
|
2023-07-25 17:51:10 +00:00
|
|
|
|
|
|
|
ownedList2 := ownedListChain2
|
|
|
|
|
|
|
|
fullList := append(ownedList1, ownedList2...)
|
|
|
|
|
|
|
|
randomAddress := common.HexToAddress("0xABCD")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2023-09-12 18:35:31 +00:00
|
|
|
var loadedTimestamp int64
|
|
|
|
var loadedList []thirdparty.CollectibleUniqueID
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, InvalidTimestamp, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID1)
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-12 18:35:31 +00:00
|
|
|
require.Equal(t, InvalidTimestamp, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress2, chainID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, InvalidTimestamp, loadedTimestamp)
|
|
|
|
|
|
|
|
err = oDB.Update(chainID0, ownerAddress1, ownedListChain0, timestampChain0)
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-12 18:35:31 +00:00
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID0)
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-12 18:35:31 +00:00
|
|
|
require.Equal(t, timestampChain0, loadedTimestamp)
|
2023-07-25 17:51:10 +00:00
|
|
|
|
2023-09-12 18:35:31 +00:00
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, InvalidTimestamp, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress2, chainID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, InvalidTimestamp, loadedTimestamp)
|
|
|
|
|
|
|
|
err = oDB.Update(chainID1, ownerAddress1, ownedListChain1, timestampChain1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = oDB.Update(chainID2, ownerAddress2, ownedListChain2, timestampChain2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, timestampChain0, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress1, chainID1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, timestampChain1, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedTimestamp, err = oDB.GetOwnershipUpdateTimestamp(ownerAddress2, chainID2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, timestampChain2, loadedTimestamp)
|
|
|
|
|
|
|
|
loadedList, err = oDB.GetOwnedCollectibles([]w_common.ChainID{chainID0}, []common.Address{ownerAddress1}, 0, len(fullList))
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ownedListChain0, loadedList)
|
|
|
|
|
|
|
|
loadedList, err = oDB.GetOwnedCollectibles([]w_common.ChainID{chainID0, chainID1}, []common.Address{ownerAddress1, randomAddress}, 0, len(fullList))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ownedList1, loadedList)
|
|
|
|
|
|
|
|
loadedList, err = oDB.GetOwnedCollectibles([]w_common.ChainID{chainID2}, []common.Address{ownerAddress2}, 0, len(fullList))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ownedList2, loadedList)
|
|
|
|
|
|
|
|
loadedList, err = oDB.GetOwnedCollectibles([]w_common.ChainID{chainID0, chainID1, chainID2}, []common.Address{ownerAddress1, ownerAddress2}, 0, len(fullList))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, fullList, loadedList)
|
|
|
|
|
|
|
|
loadedList, err = oDB.GetOwnedCollectibles([]w_common.ChainID{chainID0}, []common.Address{randomAddress}, 0, len(fullList))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, loadedList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLargeTokenID(t *testing.T) {
|
|
|
|
oDB, cleanDB := setupOwnershipDBTest(t)
|
|
|
|
defer cleanDB()
|
|
|
|
|
|
|
|
ownerAddress := common.HexToAddress("0xABCD")
|
|
|
|
chainID := w_common.ChainID(0)
|
|
|
|
|
|
|
|
ownedListChain := []thirdparty.CollectibleUniqueID{
|
|
|
|
{
|
2023-07-31 19:41:14 +00:00
|
|
|
ContractID: thirdparty.ContractID{
|
|
|
|
ChainID: chainID,
|
|
|
|
Address: common.HexToAddress("0x1234"),
|
|
|
|
},
|
|
|
|
TokenID: &bigint.BigInt{Int: big.NewInt(0).SetBytes([]byte("0x1234567890123456789012345678901234567890"))},
|
2023-07-25 17:51:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:35:31 +00:00
|
|
|
timestamp := int64(1234567890)
|
|
|
|
|
2023-07-25 17:51:10 +00:00
|
|
|
var err error
|
|
|
|
|
2023-09-12 18:35:31 +00:00
|
|
|
err = oDB.Update(chainID, ownerAddress, ownedListChain, timestamp)
|
2023-07-25 17:51:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
loadedList, err := oDB.GetOwnedCollectibles([]w_common.ChainID{chainID}, []common.Address{ownerAddress}, 0, len(ownedListChain))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ownedListChain, loadedList)
|
|
|
|
}
|