2021-09-09 14:28:54 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestTransactionDB(t *testing.T) (*TransactionManager, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "wallet-transactions-tests-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "wallet-tests")
|
|
|
|
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())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
Value: bigint.BigInt{big.NewInt(123)},
|
|
|
|
GasLimit: bigint.BigInt{big.NewInt(21000)},
|
|
|
|
GasPrice: bigint.BigInt{big.NewInt(1)},
|
|
|
|
ChainID: 777,
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:48:30 +00:00
|
|
|
rst, err := manager.getAllPendings([]uint64{777})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, rst)
|
|
|
|
|
2022-07-04 07:48:30 +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)
|
|
|
|
|
|
|
|
err = manager.addPending(trx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-04 07:48:30 +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])
|
|
|
|
|
2022-07-04 07:48:30 +00:00
|
|
|
rst, err = manager.getAllPendings([]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])
|
|
|
|
|
2022-07-04 07:48:30 +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)
|
|
|
|
|
|
|
|
err = manager.deletePending(777, trx.Hash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-04 07:48:30 +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))
|
|
|
|
|
2022-07-04 07:48:30 +00:00
|
|
|
rst, err = manager.getAllPendings([]uint64{777})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(rst))
|
|
|
|
}
|