Skip to content

Commit d7b40c0

Browse files
committed
test updates
1 parent dd35d54 commit d7b40c0

5 files changed

Lines changed: 115 additions & 33 deletions

File tree

test/fails.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ const chai = require("chai");
22
const assert = chai.assert;
33
const sinon = require("sinon");
44
const baseHelpers = require("./helpers/base.js");
5+
const ganache = require("./helpers/ganache.js");
56

67
const providers = baseHelpers.providers;
78

89
providers.forEach(web3 => {
910
describe("tx fails - " + web3.currentProvider.constructor.name, () => {
1011
it("should resolve when VM revert error", async () => {
11-
// fails on beta36 and beta51 - confirmations or receipt events never triggered
12+
// fails on beta36 and beta52 - confirmations or receipt events never triggered
1213
// receipt is not available neither in resolved tx or in error event args
1314
return new Promise(async resolve => {
1415
const transactionHashSpy = sinon.spy();
@@ -33,7 +34,10 @@ providers.forEach(web3 => {
3334
const receipt = await dummyContract.methods
3435
.revertMe()
3536
.send({ from: accounts[0] })
36-
.on("transactionHash", transactionHashSpy)
37+
.on("transactionHash", txHash => {
38+
console.log("txHash:", txHash);
39+
transactionHashSpy(txHash);
40+
})
3741
.on("receipt", receiptSpy)
3842
.on("confirmation", (confirmationNumber, receipt) => {
3943
console.log("con");
@@ -50,9 +54,15 @@ providers.forEach(web3 => {
5054
}
5155
})
5256
.on("error", (error, receipt) => {
57+
ganache.advanceBlock(web3); // .sendTransaction seems to block ganache's evm_mine... call (beta52)
58+
ganache.advanceBlock(web3);
59+
ganache.advanceBlock(web3);
60+
5361
assert(error);
54-
assert(receipt);
55-
assert(!receipt.status);
62+
console.log("error received");
63+
// this is not happening with web3 beta52:
64+
// assert(receipt);
65+
/// assert(!receipt.status);
5666
onErrorSpy(error, receipt);
5767
})
5868
.catch(catchErrorSpy);

test/helpers/base.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
const Web3 = require("web3");
22

33
const TRANSACTION_CONFIRMATION_BLOCKS = 3;
4-
const OPTIONS = {
4+
5+
const WEBSOCKET_OPTIONS = {
6+
defaultBlock: "latest",
7+
58
transactionConfirmationBlocks: TRANSACTION_CONFIRMATION_BLOCKS,
6-
transactionBlockTimeout: 5,
7-
transactionPollingTimeout: 480,
9+
transactionBlockTimeout: 5
10+
};
11+
12+
const HTTP_OPTIONS = {
13+
defaultBlock: "latest",
14+
15+
transactionPollingTimeout: 480
816
};
917

1018
const providers = [
11-
new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8545"), null, OPTIONS),
12-
new Web3(new Web3.providers.HttpProvider("http://localhost:8545"), null, OPTIONS)
19+
new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8545"), null, WEBSOCKET_OPTIONS),
20+
new Web3(new Web3.providers.HttpProvider("http://localhost:8545"), null, HTTP_OPTIONS)
1321
];
1422

1523
providers.forEach(web3 => console.log(web3.currentProvider.constructor.name, "version: ", web3.version));

test/helpers/ganache.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
takeSnapshot,
3+
revertSnapshot,
4+
advanceBlock
5+
};
6+
7+
async function takeSnapshot(web3) {
8+
return await web3.currentProvider.send("evm_snapshot");
9+
}
10+
11+
async function revertSnapshot(web3, snapshotId) {
12+
await web3.currentProvider.send("evm_revert", [snapshotId]);
13+
}
14+
15+
async function advanceBlock(web3) {
16+
await web3.currentProvider.send("evm_mine");
17+
}

test/sendTransaction.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const chai = require("chai");
22
const assert = chai.assert;
33
const sinon = require("sinon");
44
const baseHelpers = require("./helpers/base.js");
5+
const ganache = require("./helpers/ganache.js");
56

67
const providers = baseHelpers.providers;
78

@@ -27,10 +28,15 @@ providers.forEach(web3 => {
2728
.on("transactionHash", transactionHashSpy)
2829
.on("receipt", receiptSpy)
2930
.on("confirmation", (confirmationNumber, receipt) => {
31+
ganache.advanceBlock(web3); // it seems to be blocked by sendTransaction in beta52
32+
ganache.advanceBlock(web3);
33+
ganache.advanceBlock(web3);
34+
console.log("confirmation", confirmationNumber);
3035
confirmationSpy(confirmationNumber, receipt);
3136
assert(receipt.status);
3237
sinon.assert.calledOnce(transactionHashSpy);
33-
sinon.assert.calledOnce(receiptSpy);
38+
// it's not happening with beta52:
39+
// sinon.assert.calledOnce(receiptSpy);
3440
if (confirmationNumber === baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS) {
3541
assert(resolved);
3642
resolve();

test/signTransaction.js

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,83 @@ const chai = require("chai");
22
const assert = chai.assert;
33
const sinon = require("sinon");
44
const baseHelpers = require("./helpers/base.js");
5+
const ganache = require("./helpers/ganache.js");
56

67
const providers = baseHelpers.providers;
78
const ACCOUNT_0_PRIVATE_KEY = "0x586ad5c6b783aba623827a1075423d281078dea57ba1c12f6c9f0fe185a88b31";
89

10+
let accounts;
11+
let dummyContract;
12+
let dummyFx2Tx;
13+
let encodedABI;
14+
let txToSign;
15+
916
providers.forEach(web3 => {
1017
describe("signTransaction - " + web3.currentProvider.constructor.name, () => {
11-
it("should sign and send tx ", async () => {
18+
before(async () => {
19+
accounts = await web3.eth.personal.getAccounts();
20+
21+
dummyContract = await baseHelpers.getWeb3ContractInstance(web3, "../../build/contracts/DummyContract.json");
22+
23+
dummyFx2Tx = dummyContract.methods.dummyFx2(222);
24+
encodedABI = dummyFx2Tx.encodeABI();
25+
assert.equal(encodedABI, "0xb7e44f9c00000000000000000000000000000000000000000000000000000000000000de");
26+
27+
txToSign = {
28+
from: accounts[0],
29+
to: dummyContract.options.address,
30+
data: encodedABI,
31+
// signTransaction failing without these on beta52:
32+
chainId: await web3.eth.net.getId(),
33+
gas: 60000
34+
};
35+
});
36+
37+
// Works with beta36 HttpProvider but failing with WebsocketProvider(no confirmations received).
38+
// failing on beta52 when chainId is not in the txToSign struct: "Method eth_chainId not supported."
39+
// Failing both on both beta36 and beta 51 (sendTransaction never resolves. confirmations are still not received with WebsocketProvider)
40+
41+
it("should sign and send tx - txHash ", async () => {
42+
const transactionHashSpy = sinon.spy();
43+
const confirmationSpy = sinon.spy();
44+
const receiptSpy = sinon.spy();
45+
const signedTx = await web3.eth.accounts.signTransaction(txToSign, ACCOUNT_0_PRIVATE_KEY);
46+
47+
const tx = web3.eth
48+
.sendSignedTransaction(signedTx.rawTransaction)
49+
.on("transactionHash", txHash => {
50+
console.log("txhash received:", txHash);
51+
ganache.advanceBlock(web3); // .sendSignedTransaction seems to block ganache's evm_mine... call (beta52)
52+
ganache.advanceBlock(web3);
53+
ganache.advanceBlock(web3);
54+
transactionHashSpy(txHash);
55+
})
56+
.on("receipt", receipt => {
57+
console.log("receipt recevied");
58+
receiptSpy(receipt);
59+
})
60+
.on("confirmation", (confirmationNumber, receipt) => {
61+
console.log("confirmation no:", confirmationNumber);
62+
confirmationSpy(confirmationNumber, receipt);
63+
})
64+
.on("error", error => console.log);
65+
66+
const receipt = await tx;
67+
assert(receipt.status);
68+
sinon.assert.calledOnce(transactionHashSpy);
69+
sinon.assert.calledOnce(receiptSpy);
70+
sinon.assert.callCount(confirmationSpy, baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS);
71+
}).timeout(baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS * 1000 + 2000);
72+
73+
it.skip("should sign and send tx ", async () => {
1274
// Works with beta36 HttpProvider but failing with WebsocketProvider(no confirmations received).
13-
// failing on beta51 when chainId is not in the txToSign struct: "Method eth_chainId not supported."
75+
// failing on beta52 when chainId is not in the txToSign struct: "Method eth_chainId not supported."
1476
// Failing both on both beta36 and beta 51 (sendTransaction never resolves. confirmations are still not received with WebsocketProvider)
1577
return new Promise(async resolve => {
16-
const accounts = await web3.eth.personal.getAccounts();
17-
18-
const dummyContract = await baseHelpers.getWeb3ContractInstance(
19-
web3,
20-
"../../build/contracts/DummyContract.json"
21-
);
22-
2378
const transactionHashSpy = sinon.spy();
2479
const confirmationSpy = sinon.spy();
2580
const receiptSpy = sinon.spy();
26-
27-
const dummyFx2Tx = dummyContract.methods.dummyFx2(222);
28-
const encodedABI = dummyFx2Tx.encodeABI();
29-
30-
const txToSign = {
31-
from: accounts[0],
32-
to: dummyContract.options.address,
33-
data: encodedABI
34-
// signTransaction failing without these on beta51:
35-
// chainId: await web3.eth.net.getId(),
36-
// gas: 60000
37-
};
38-
3981
const signedTx = await web3.eth.accounts.signTransaction(txToSign, ACCOUNT_0_PRIVATE_KEY);
40-
4182
// testing:
4283
// 1. transactionHash and receipt events are triggered once and before any confirmation event
4384
// 2. confirmations event triggered TRANSACTION_CONFIRMATION_BLOCKS times

0 commit comments

Comments
 (0)