status-go/t/devtests/tranfers_test.go

88 lines
2.3 KiB
Go
Raw Normal View History

package devtests
import (
"context"
"fmt"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/status-im/status-go/services/wallet"
"github.com/status-im/status-go/t/utils"
"github.com/stretchr/testify/suite"
)
func TestTransfersSuite(t *testing.T) {
suite.Run(t, new(TransfersSuite))
}
type TransfersSuite struct {
DevNodeSuite
}
func (s *TransfersSuite) getAllTranfers() (rst []wallet.TransferView, err error) {
2019-07-25 05:35:09 +00:00
return rst, s.Local.Call(&rst, "wallet_getTransfersByAddress", s.DevAccountAddress, (*hexutil.Big)(big.NewInt(0)))
}
func (s *TransfersSuite) sendTx(nonce uint64, to common.Address) {
tx := types.NewTransaction(nonce, to, big.NewInt(1e18), 1e6, big.NewInt(10), nil)
// TODO move signer to DevNodeSuite
tx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(1337)), s.DevAccount)
s.Require().NoError(err)
s.Require().NoError(s.Eth.SendTransaction(context.Background(), tx))
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err = bind.WaitMined(timeout, s.Eth, tx)
cancel()
s.Require().NoError(err)
}
func (s *TransfersSuite) TestNewTransfers() {
2019-07-25 05:35:09 +00:00
s.sendTx(0, s.DevAccountAddress)
s.Require().NoError(utils.Eventually(func() error {
all, err := s.getAllTranfers()
if err != nil {
return err
}
if len(all) != 1 {
return fmt.Errorf("waiting for one transfer")
}
return nil
}, 20*time.Second, 1*time.Second))
go func() {
for i := 1; i < 10; i++ {
2019-07-25 05:35:09 +00:00
s.sendTx(uint64(i), s.DevAccountAddress)
}
}()
s.Require().NoError(utils.Eventually(func() error {
all, err := s.getAllTranfers()
if err != nil {
return err
}
if len(all) != 10 {
return fmt.Errorf("waiting for 10 transfers")
}
return nil
}, 30*time.Second, 1*time.Second))
}
func (s *TransfersSuite) TestHistoricalTransfers() {
for i := 0; i < 30; i++ {
2019-07-25 05:35:09 +00:00
s.sendTx(uint64(i), s.DevAccountAddress)
}
s.Require().NoError(utils.Eventually(func() error {
all, err := s.getAllTranfers()
if err != nil {
return err
}
if len(all) < 30 {
return fmt.Errorf("waiting for atleast 30 transfers, got %d", len(all))
}
return nil
}, 30*time.Second, 1*time.Second))
}