@@ -2,42 +2,83 @@ const chai = require("chai");
22const assert = chai . assert ;
33const sinon = require ( "sinon" ) ;
44const baseHelpers = require ( "./helpers/base.js" ) ;
5+ const ganache = require ( "./helpers/ganache.js" ) ;
56
67const providers = baseHelpers . providers ;
78const ACCOUNT_0_PRIVATE_KEY = "0x586ad5c6b783aba623827a1075423d281078dea57ba1c12f6c9f0fe185a88b31" ;
89
10+ let accounts ;
11+ let dummyContract ;
12+ let dummyFx2Tx ;
13+ let encodedABI ;
14+ let txToSign ;
15+
916providers . 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