@@ -5,15 +5,18 @@ const baseHelpers = require("./helpers/base.js");
55const ganache = require ( "./helpers/ganache.js" ) ;
66
77const providers = baseHelpers . providers ;
8+ let accounts ;
89
910providers . forEach ( web3 => {
1011 describe ( "sendTransaction - " + web3 . currentProvider . constructor . name , ( ) => {
12+ before ( async ( ) => {
13+ accounts = await web3 . eth . personal . getAccounts ( ) ;
14+ } ) ;
15+
1116 it ( "should trigger tx events after send" , async ( ) => {
1217 // Works with beta36 HttpProvider but failing with WebsocketProvider(no confirmations received).
1318 // Failing both with beta 51 (sendTransaction never resolves. confirmations are still not received with WebsocketProvider)
1419 return new Promise ( async resolve => {
15- const accounts = await web3 . eth . personal . getAccounts ( ) ;
16-
1720 const transactionHashSpy = sinon . spy ( ) ;
1821 const confirmationSpy = sinon . spy ( ) ;
1922 const receiptSpy = sinon . spy ( ) ;
@@ -23,15 +26,22 @@ providers.forEach(web3 => {
2326 // 2. confirmations event triggered TRANSACTION_CONFIRMATION_BLOCKS times
2427 // 3. tx resolved before confirmation event triggered on the TRANSACTION_CONFIRMATION_BLOCKS time
2528 let resolved = false ;
26- const receipt = await web3 . eth
29+
30+ const tx = web3 . eth
2731 . 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+ } )
3040 . on ( "confirmation" , ( confirmationNumber , receipt ) => {
3141 ganache . advanceBlock ( web3 ) ; // it seems to be blocked by sendTransaction in beta52
3242 ganache . advanceBlock ( web3 ) ;
3343 ganache . advanceBlock ( web3 ) ;
34- console . log ( "confirmation" , confirmationNumber ) ;
44+ console . log ( "confirmation" , confirmationNumber , web3 . currentProvider . constructor . name ) ;
3545 confirmationSpy ( confirmationNumber , receipt ) ;
3646 assert ( receipt . status ) ;
3747 sinon . assert . calledOnce ( transactionHashSpy ) ;
@@ -43,11 +53,52 @@ providers.forEach(web3 => {
4353 }
4454 } ) ;
4555
56+ const receipt = await tx ;
57+ resolved = true ;
58+
4659 assert ( receipt . status ) ;
4760 sinon . assert . calledOnce ( transactionHashSpy ) ;
4861 sinon . assert . calledOnce ( receiptSpy ) ;
49- resolved = true ;
62+ sinon . assert . callCount ( confirmationSpy , baseHelpers . TRANSACTION_CONFIRMATION_BLOCKS ) ;
5063 } ) ;
5164 } ) . 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 ) ;
52103 } ) ;
53104} ) ;
0 commit comments