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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestTransactionDB(t *testing.T) (*TransactionManager, func()) {
|
2023-03-01 19:35:19 +00:00
|
|
|
db, err := appdatabase.SetupTestMemorySQLDB("wallet-transfer-transaction-tests")
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-15 08:53:56 +00:00
|
|
|
return &TransactionManager{db, nil, nil, nil, nil}, func() {
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPendingTransactions(t *testing.T) {
|
|
|
|
manager, stop := setupTestTransactionDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
trx := PendingTransaction{
|
|
|
|
Hash: common.Hash{1},
|
|
|
|
From: common.Address{1},
|
|
|
|
To: common.Address{2},
|
|
|
|
Type: RegisterENS,
|
|
|
|
AdditionalData: "someuser.stateofus.eth",
|
2023-01-13 17:12:46 +00:00
|
|
|
Value: bigint.BigInt{Int: big.NewInt(123)},
|
|
|
|
GasLimit: bigint.BigInt{Int: big.NewInt(21000)},
|
|
|
|
GasPrice: bigint.BigInt{Int: big.NewInt(1)},
|
2021-09-09 14:28:54 +00:00
|
|
|
ChainID: 777,
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err := manager.GetAllPending([]uint64{777})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetPendingByAddress([]uint64{777}, trx.From)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
err = manager.AddPending(trx)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetPendingByAddress([]uint64{777}, trx.From)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
|
|
|
require.Equal(t, trx, *rst[0])
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetAllPending([]uint64{777})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
|
|
|
require.Equal(t, trx, *rst[0])
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetPendingByAddress([]uint64{777}, common.Address{2})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
err = manager.DeletePending(777, trx.Hash)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetPendingByAddress([]uint64{777}, trx.From)
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst))
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rst, err = manager.GetAllPending([]uint64{777})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst))
|
|
|
|
}
|
2023-03-01 19:35:19 +00:00
|
|
|
|
|
|
|
func TestMultiTransactions(t *testing.T) {
|
|
|
|
manager, stop := setupTestTransactionDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
trx1 := MultiTransaction{
|
|
|
|
Timestamp: 123,
|
|
|
|
FromAddress: common.Address{1},
|
|
|
|
ToAddress: common.Address{2},
|
|
|
|
FromAsset: "fromAsset",
|
|
|
|
ToAsset: "toAsset",
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(123)),
|
|
|
|
Type: MultiTransactionBridge,
|
|
|
|
}
|
|
|
|
trx2 := trx1
|
|
|
|
trx2.FromAmount = (*hexutil.Big)(big.NewInt(456))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
ids := make([]MultiTransactionIDType, 2)
|
|
|
|
ids[0], err = insertMultiTransaction(manager.db, &trx1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, MultiTransactionIDType(1), ids[0])
|
|
|
|
ids[1], err = insertMultiTransaction(manager.db, &trx2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, MultiTransactionIDType(2), ids[1])
|
|
|
|
|
|
|
|
rst, err := manager.GetMultiTransactions(context.Background(), []MultiTransactionIDType{ids[0], 555})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(rst))
|
|
|
|
|
|
|
|
rst, err = manager.GetMultiTransactions(context.Background(), ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(rst))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
found := false
|
|
|
|
for _, trx := range rst {
|
|
|
|
found = found || id == MultiTransactionIDType(trx.ID)
|
|
|
|
}
|
|
|
|
require.True(t, found, "result contains transaction with id %d", id)
|
|
|
|
}
|
|
|
|
}
|