Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Pass in a context with a timeout to calls to WaitMined so that they a…
Browse files Browse the repository at this point in the history
…re guarenteed to eventually return
  • Loading branch information
fabioberger committed Sep 17, 2019
1 parent 6ed7d4d commit b1530de
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
36 changes: 24 additions & 12 deletions scenario/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err := weth9.Deposit(opts)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -76,7 +77,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = zrx.Transfer(opts, makerAddress, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -87,7 +89,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -98,7 +101,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = weth9.Approve(opts, ganacheAddresses.ERC20Proxy, wethAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -149,7 +153,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err := weth9.Deposit(opts)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -163,7 +168,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = zrx.Transfer(opts, takerAddress, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -174,7 +180,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -185,7 +192,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client,
}
txn, err = weth9.Approve(opts, ganacheAddresses.ERC20Proxy, wethAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -203,7 +211,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m
}
txn, err := dummyERC721Token.Mint(makerOpts, makerAddress, tokenID)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -240,7 +249,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m
// SET NFT allowance
txn, err = dummyERC721Token.SetApprovalForAll(makerOpts, ganacheAddresses.ERC721Proxy, true)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -260,7 +270,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m
}
txn, err = zrx.Transfer(opts, takerAddress, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand All @@ -271,7 +282,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m
}
txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
require.Equal(t, receipt.Status, uint64(1))

Expand Down
36 changes: 24 additions & 12 deletions zeroex/orderwatch/order_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func TestOrderWatcherUnfundedInsufficientERC20Balance(t *testing.T) {
}
txn, err := zrx.Transfer(opts, constants.GanacheAccount4, zrxAmount)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -132,7 +133,8 @@ func TestOrderWatcherUnfundedInsufficientERC721Balance(t *testing.T) {
}
txn, err := dummyERC721Token.TransferFrom(opts, makerAddress, constants.GanacheAccount4, tokenID)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -173,7 +175,8 @@ func TestOrderWatcherUnfundedInsufficientERC721Allowance(t *testing.T) {
ganacheAddresses := ethereum.NetworkIDToContractAddresses[constants.TestNetworkID]
txn, err := dummyERC721Token.SetApprovalForAll(opts, ganacheAddresses.ERC721Proxy, false)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -214,7 +217,8 @@ func TestOrderWatcherUnfundedInsufficientERC20Allowance(t *testing.T) {
}
txn, err := zrx.Approve(opts, ganacheAddresses.ERC20Proxy, big.NewInt(0))
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -254,7 +258,8 @@ func TestOrderWatcherUnfundedThenFundedAgain(t *testing.T) {
}
txn, err := zrx.Transfer(opts, constants.GanacheAccount4, zrxAmount)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -284,7 +289,8 @@ func TestOrderWatcherUnfundedThenFundedAgain(t *testing.T) {
}
txn, err = zrx.Transfer(opts, makerAddress, zrxAmount)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -326,7 +332,8 @@ func TestOrderWatcherNoChange(t *testing.T) {
}
txn, err := zrx.Transfer(opts, makerAddress, zrxAmount)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -359,7 +366,8 @@ func TestOrderWatcherWETHWithdrawAndDeposit(t *testing.T) {
}
txn, err := weth.Withdraw(opts, wethAmount)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -388,7 +396,8 @@ func TestOrderWatcherWETHWithdrawAndDeposit(t *testing.T) {
}
txn, err = weth.Deposit(opts)
require.NoError(t, err)
receipt, err = bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err = bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -423,7 +432,8 @@ func TestOrderWatcherCanceled(t *testing.T) {
orderWithoutExchangeAddress := signedOrder.ConvertToOrderWithoutExchangeAddress()
txn, err := exchange.CancelOrder(opts, orderWithoutExchangeAddress)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -464,7 +474,8 @@ func TestOrderWatcherCancelUpTo(t *testing.T) {
targetOrderEpoch := signedOrder.Salt
txn, err := exchange.CancelOrdersUpTo(opts, targetOrderEpoch)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down Expand Up @@ -505,7 +516,8 @@ func TestOrderWatcherERC20Filled(t *testing.T) {
orderWithoutExchangeAddress := signedOrder.ConvertToOrderWithoutExchangeAddress()
txn, err := exchange.FillOrder(opts, orderWithoutExchangeAddress, wethAmount, signedOrder.Signature)
require.NoError(t, err)
receipt, err := bind.WaitMined(context.Background(), ethClient, txn)
ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second)
receipt, err := bind.WaitMined(ctx, ethClient, txn)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1))

Expand Down

0 comments on commit b1530de

Please sign in to comment.