@@ -5,15 +5,18 @@ const baseHelpers = require("./helpers/base.js");
5
5
const ganache = require ( "./helpers/ganache.js" ) ;
6
6
7
7
const providers = baseHelpers . providers ;
8
+ let accounts ;
8
9
9
10
providers . forEach ( web3 => {
10
11
describe ( "sendTransaction - " + web3 . currentProvider . constructor . name , ( ) => {
12
+ before ( async ( ) => {
13
+ accounts = await web3 . eth . personal . getAccounts ( ) ;
14
+ } ) ;
15
+
11
16
it ( "should trigger tx events after send" , async ( ) => {
12
17
// Works with beta36 HttpProvider but failing with WebsocketProvider(no confirmations received).
13
18
// Failing both with beta 51 (sendTransaction never resolves. confirmations are still not received with WebsocketProvider)
14
19
return new Promise ( async resolve => {
15
- const accounts = await web3 . eth . personal . getAccounts ( ) ;
16
-
17
20
const transactionHashSpy = sinon . spy ( ) ;
18
21
const confirmationSpy = sinon . spy ( ) ;
19
22
const receiptSpy = sinon . spy ( ) ;
@@ -23,15 +26,22 @@ providers.forEach(web3 => {
23
26
// 2. confirmations event triggered TRANSACTION_CONFIRMATION_BLOCKS times
24
27
// 3. tx resolved before confirmation event triggered on the TRANSACTION_CONFIRMATION_BLOCKS time
25
28
let resolved = false ;
26
- const receipt = await web3 . eth
29
+
30
+ const tx = web3 . eth
27
31
. sendTransaction ( { to : accounts [ 1 ] , from : accounts [ 0 ] , value : web3 . utils . toWei ( "0.1" , "ether" ) } )
28
- . on ( "transactionHash" , transactionHashSpy )
29
- . on ( "receipt" , receiptSpy )
32
+ . on ( "transactionHash" , txHash => {
33
+ console . log ( "transactionHash:" , txHash ) ;
34
+ transactionHashSpy ( txHash ) ;
35
+ } )
36
+ . on ( "receipt" , receipt => {
37
+ console . log ( "receipt received" ) ;
38
+ receiptSpy ( receipt ) ;
39
+ } )
30
40
. on ( "confirmation" , ( confirmationNumber , receipt ) => {
31
41
ganache . advanceBlock ( web3 ) ; // it seems to be blocked by sendTransaction in beta52
32
42
ganache . advanceBlock ( web3 ) ;
33
43
ganache . advanceBlock ( web3 ) ;
34
- console . log ( "confirmation" , confirmationNumber ) ;
44
+ console . log ( "confirmation" , confirmationNumber , web3 . currentProvider . constructor . name ) ;
35
45
confirmationSpy ( confirmationNumber , receipt ) ;
36
46
assert ( receipt . status ) ;
37
47
sinon . assert . calledOnce ( transactionHashSpy ) ;
@@ -43,11 +53,52 @@ providers.forEach(web3 => {
43
53
}
44
54
} ) ;
45
55
56
+ const receipt = await tx ;
57
+ resolved = true ;
58
+
46
59
assert ( receipt . status ) ;
47
60
sinon . assert . calledOnce ( transactionHashSpy ) ;
48
61
sinon . assert . calledOnce ( receiptSpy ) ;
49
- resolved = true ;
62
+ sinon . assert . callCount ( confirmationSpy , baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS ) ;
50
63
} ) ;
51
64
} ) . timeout ( baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS * 1000 + 2000 ) ;
65
+
66
+ it ( "should trigger tx events after send - non async" , done => {
67
+ const transactionHashSpy = sinon . spy ( ) ;
68
+ const confirmationSpy = sinon . spy ( ) ;
69
+ const receiptSpy = sinon . spy ( ) ;
70
+
71
+ let resolved = false ;
72
+
73
+ web3 . eth
74
+ . sendTransaction ( { to : accounts [ 1 ] , from : accounts [ 0 ] , value : web3 . utils . toWei ( "0.1" , "ether" ) } )
75
+ . on ( "transactionHash" , txHash => {
76
+ console . log ( "transactionHash:" , txHash ) ;
77
+ transactionHashSpy ( txHash ) ;
78
+ } )
79
+ . on ( "receipt" , receipt => {
80
+ console . log ( "receipt received" ) ;
81
+ receiptSpy ( receipt ) ;
82
+ } )
83
+ . on ( "confirmation" , ( confirmationNumber , receipt ) => {
84
+ console . log ( "confirmation" , confirmationNumber , web3 . currentProvider . constructor . name ) ;
85
+ confirmationSpy ( confirmationNumber , receipt ) ;
86
+ assert ( receipt . status ) ;
87
+ sinon . assert . calledOnce ( transactionHashSpy ) ;
88
+ // it's not happening with beta52:
89
+ // sinon.assert.calledOnce(receiptSpy);
90
+ if ( confirmationNumber === baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS ) {
91
+ assert ( resolved ) ;
92
+ }
93
+ } )
94
+ . then ( receipt => {
95
+ resolved = true ;
96
+ assert ( receipt . status ) ;
97
+ sinon . assert . calledOnce ( transactionHashSpy ) ;
98
+ sinon . assert . calledOnce ( receiptSpy ) ;
99
+ sinon . assert . callCount ( confirmationSpy , baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS ) ;
100
+ done ( ) ;
101
+ } ) ;
102
+ } ) . timeout ( baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS * 1000 + 2000 ) ;
52
103
} ) ;
53
104
} ) ;
0 commit comments