2023-02-15 12:28:19 +00:00
|
|
|
package transfer
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-01 19:35:19 +00:00
|
|
|
"context"
|
2021-09-09 14:28:54 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-03-12 09:15:30 +00:00
|
|
|
wallet_common "github.com/status-im/status-go/services/wallet/common"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-03-01 19:35:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestTransactionDB(t *testing.T) (*TransactionManager, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-29 17:56:27 +00:00
|
|
|
return &TransactionManager{db, nil, nil, nil, nil, nil, nil, nil, nil, nil}, func() {
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
func areMultiTransactionsEqual(mt1, mt2 *MultiTransaction) bool {
|
|
|
|
return mt1.Timestamp == mt2.Timestamp &&
|
|
|
|
mt1.FromNetworkID == mt2.FromNetworkID &&
|
|
|
|
mt1.ToNetworkID == mt2.ToNetworkID &&
|
|
|
|
mt1.FromTxHash == mt2.FromTxHash &&
|
|
|
|
mt1.ToTxHash == mt2.ToTxHash &&
|
|
|
|
mt1.FromAddress == mt2.FromAddress &&
|
|
|
|
mt1.ToAddress == mt2.ToAddress &&
|
|
|
|
mt1.FromAsset == mt2.FromAsset &&
|
|
|
|
mt1.ToAsset == mt2.ToAsset &&
|
|
|
|
mt1.FromAmount.String() == mt2.FromAmount.String() &&
|
|
|
|
mt1.ToAmount.String() == mt2.ToAmount.String() &&
|
|
|
|
mt1.Type == mt2.Type &&
|
|
|
|
mt1.CrossTxID == mt2.CrossTxID
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBridgeMultiTransactions(t *testing.T) {
|
2023-03-01 19:35:19 +00:00
|
|
|
manager, stop := setupTestTransactionDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
trx1 := MultiTransaction{
|
2023-06-13 14:20:48 +00:00
|
|
|
Timestamp: 123,
|
|
|
|
FromNetworkID: 0,
|
|
|
|
ToNetworkID: 1,
|
|
|
|
FromTxHash: common.Hash{5},
|
|
|
|
// Empty ToTxHash
|
2023-03-01 19:35:19 +00:00
|
|
|
FromAddress: common.Address{1},
|
|
|
|
ToAddress: common.Address{2},
|
|
|
|
FromAsset: "fromAsset",
|
|
|
|
ToAsset: "toAsset",
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(123)),
|
2023-06-02 20:08:45 +00:00
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(234)),
|
2023-03-01 19:35:19 +00:00
|
|
|
Type: MultiTransactionBridge,
|
2023-06-13 14:20:48 +00:00
|
|
|
CrossTxID: "crossTxD1",
|
|
|
|
}
|
|
|
|
|
|
|
|
trx2 := MultiTransaction{
|
|
|
|
Timestamp: 321,
|
|
|
|
FromNetworkID: 1,
|
|
|
|
ToNetworkID: 0,
|
|
|
|
//Empty FromTxHash
|
|
|
|
ToTxHash: common.Hash{6},
|
|
|
|
FromAddress: common.Address{2},
|
|
|
|
ToAddress: common.Address{1},
|
|
|
|
FromAsset: "fromAsset",
|
|
|
|
ToAsset: "toAsset",
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(123)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(234)),
|
|
|
|
Type: MultiTransactionBridge,
|
|
|
|
CrossTxID: "crossTxD2",
|
|
|
|
}
|
|
|
|
|
|
|
|
trxs := []*MultiTransaction{&trx1, &trx2}
|
|
|
|
|
|
|
|
var err error
|
2024-03-12 09:15:30 +00:00
|
|
|
ids := make([]wallet_common.MultiTransactionIDType, len(trxs))
|
2023-06-13 14:20:48 +00:00
|
|
|
for i, trx := range trxs {
|
|
|
|
ids[i], err = insertMultiTransaction(manager.db, trx)
|
|
|
|
require.NoError(t, err)
|
2024-03-12 09:15:30 +00:00
|
|
|
require.Equal(t, wallet_common.MultiTransactionIDType(i+1), ids[i])
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rst, err := manager.GetBridgeOriginMultiTransaction(context.Background(), trx1.ToNetworkID, trx1.CrossTxID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, rst)
|
|
|
|
require.True(t, areMultiTransactionsEqual(&trx1, rst))
|
|
|
|
|
|
|
|
rst, err = manager.GetBridgeDestinationMultiTransaction(context.Background(), trx1.ToNetworkID, trx1.CrossTxID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rst)
|
|
|
|
|
|
|
|
rst, err = manager.GetBridgeOriginMultiTransaction(context.Background(), trx2.ToNetworkID, trx2.CrossTxID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rst)
|
|
|
|
|
|
|
|
rst, err = manager.GetBridgeDestinationMultiTransaction(context.Background(), trx2.ToNetworkID, trx2.CrossTxID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, rst)
|
|
|
|
require.True(t, areMultiTransactionsEqual(&trx2, rst))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiTransactions(t *testing.T) {
|
|
|
|
manager, stop := setupTestTransactionDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
trx1 := MultiTransaction{
|
|
|
|
Timestamp: 123,
|
|
|
|
FromNetworkID: 0,
|
|
|
|
ToNetworkID: 1,
|
|
|
|
FromTxHash: common.Hash{5},
|
|
|
|
ToTxHash: common.Hash{6},
|
|
|
|
FromAddress: common.Address{1},
|
|
|
|
ToAddress: common.Address{2},
|
|
|
|
FromAsset: "fromAsset",
|
|
|
|
ToAsset: "toAsset",
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(123)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(234)),
|
|
|
|
Type: MultiTransactionBridge,
|
|
|
|
CrossTxID: "crossTxD",
|
2023-03-01 19:35:19 +00:00
|
|
|
}
|
|
|
|
trx2 := trx1
|
|
|
|
trx2.FromAmount = (*hexutil.Big)(big.NewInt(456))
|
2023-06-02 20:08:45 +00:00
|
|
|
trx2.ToAmount = (*hexutil.Big)(big.NewInt(567))
|
2023-03-01 19:35:19 +00:00
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
trxs := []*MultiTransaction{&trx1, &trx2}
|
|
|
|
|
2023-03-01 19:35:19 +00:00
|
|
|
var err error
|
2024-03-12 09:15:30 +00:00
|
|
|
ids := make([]wallet_common.MultiTransactionIDType, len(trxs))
|
2023-06-13 14:20:48 +00:00
|
|
|
for i, trx := range trxs {
|
|
|
|
ids[i], err = insertMultiTransaction(manager.db, trx)
|
|
|
|
require.NoError(t, err)
|
2024-03-12 09:15:30 +00:00
|
|
|
require.Equal(t, wallet_common.MultiTransactionIDType(i+1), ids[i])
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
2023-03-01 19:35:19 +00:00
|
|
|
|
2024-03-12 09:15:30 +00:00
|
|
|
rst, err := manager.GetMultiTransactions(context.Background(), []wallet_common.MultiTransactionIDType{ids[0], 555})
|
2023-03-01 19:35:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
2023-06-13 14:20:48 +00:00
|
|
|
require.True(t, areMultiTransactionsEqual(trxs[0], rst[0]))
|
|
|
|
|
|
|
|
trx1.FromAmount = (*hexutil.Big)(big.NewInt(789))
|
|
|
|
trx1.ToAmount = (*hexutil.Big)(big.NewInt(890))
|
|
|
|
err = updateMultiTransaction(manager.db, &trx1)
|
|
|
|
require.NoError(t, err)
|
2023-03-01 19:35:19 +00:00
|
|
|
|
|
|
|
rst, err = manager.GetMultiTransactions(context.Background(), ids)
|
|
|
|
require.NoError(t, err)
|
2023-06-13 14:20:48 +00:00
|
|
|
require.Equal(t, len(ids), len(rst))
|
2023-03-01 19:35:19 +00:00
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
for i, id := range ids {
|
2023-03-01 19:35:19 +00:00
|
|
|
found := false
|
|
|
|
for _, trx := range rst {
|
2024-03-12 09:15:30 +00:00
|
|
|
if id == trx.ID {
|
2023-06-13 14:20:48 +00:00
|
|
|
found = true
|
|
|
|
require.True(t, areMultiTransactionsEqual(trxs[i], trx))
|
|
|
|
break
|
|
|
|
}
|
2023-03-01 19:35:19 +00:00
|
|
|
}
|
|
|
|
require.True(t, found, "result contains transaction with id %d", id)
|
|
|
|
}
|
|
|
|
}
|