@@ -42,23 +42,29 @@ class providerService {
42
42
*/
43
43
makeWeb3FromProviderURI ( providerURI ) {
44
44
45
- const provider = / ^ h t t p / . test ( providerURI ) ?
46
- new Web3 . providers . HttpProvider ( providerURI ) :
47
- new Web3 . providers . IpcProvider ( `${ / ^ w i n / . test ( process . platform ) ? '\\\\.\\pipe\\' : '' } ${ providerURI } ` , net ) ;
45
+ if ( / ^ h t t p / . test ( providerURI ) || / ^ w s / . test ( providerURI ) )
46
+ return new Web3 ( providerURI ) ;
48
47
49
- const web3 = new Web3 ( ) ;
50
- web3 . setProvider ( provider ) ;
51
- return web3 ;
48
+ providerURI = `${ / ^ w i n / . test ( process . platform ) ? '\\\\.\\pipe\\' : '' } ${ providerURI } ` ;
49
+ return new Web3 ( providerURI , net ) ;
52
50
}
53
51
54
52
/** @function
55
53
* @description reset the current connection
56
54
* @return {Promise<void> }
57
55
*/
58
56
async resetConnector ( ) {
59
- await this . connector . reset ( ) ;
57
+
58
+ if ( this . filter ) {
59
+ //await Promise.promisify(this.filter.unsubscribe.bind(this.connector))();
60
+ await new Promise ( res => this . filter . unsubscribe ( res ) ) ;
61
+ this . filter = null ;
62
+ }
63
+
64
+ if ( _ . has ( this . connector , 'currentProvider.connection.close' ) )
65
+ this . connector . currentProvider . connection . close ( ) ;
60
66
this . switchConnector ( ) ;
61
- this . events . emit ( 'disconnected' ) ;
67
+
62
68
}
63
69
64
70
/**
@@ -68,16 +74,20 @@ class providerService {
68
74
*/
69
75
async switchConnector ( ) {
70
76
77
+ console . log ( 'switching connector' )
71
78
const providerURI = await Promise . any ( config . web3 . providers . map ( async providerURI => {
72
79
const web3 = this . makeWeb3FromProviderURI ( providerURI ) ;
73
- await Promise . promisify ( web3 . eth . getBlockNumber ) ( ) . timeout ( 5000 ) ;
74
- web3 . reset ( ) ;
80
+ await web3 . eth . getBlockNumber ( ) ;
81
+ if ( _ . has ( web3 , 'currentProvider.connection.close' ) )
82
+ web3 . currentProvider . connection . close ( ) ;
75
83
return providerURI ;
76
84
} ) ) . catch ( ( ) => {
77
85
log . error ( 'no available connection!' ) ;
78
- process . exit ( 0 ) ;
86
+ process . exit ( 0 ) ; //todo move to root
79
87
} ) ;
80
88
89
+ console . log ( 'switched connector' )
90
+
81
91
const fullProviderURI = ! / ^ h t t p / . test ( providerURI ) ? `${ / ^ w i n / . test ( process . platform ) ? '\\\\.\\pipe\\' : '' } ${ providerURI } ` : providerURI ;
82
92
const currentProviderURI = this . connector ? this . connector . currentProvider . path || this . connector . currentProvider . host : '' ;
83
93
@@ -87,31 +97,28 @@ class providerService {
87
97
this . connector = this . makeWeb3FromProviderURI ( providerURI ) ;
88
98
89
99
if ( _ . get ( this . connector . currentProvider , 'connection' ) ) {
90
- this . connector . currentProvider . connection . on ( 'end' , ( ) => this . resetConnector ( ) ) ;
91
- this . connector . currentProvider . connection . on ( 'error' , ( ) => this . resetConnector ( ) ) ;
92
- } else
100
+
101
+ /* this.connector.currentProvider.connection.on('end', () => this.resetConnector());
102
+ this.connector.currentProvider.connection.on('error', () => this.resetConnector());*/
103
+ this . connector . currentProvider . connection . onerror ( ( ) => this . resetConnector ( ) ) ;
104
+ this . connector . currentProvider . connection . onclose ( ( ) => this . resetConnector ( ) ) ;
105
+
106
+
107
+ } else
93
108
this . pingIntervalId = setInterval ( async ( ) => {
94
109
95
- const isConnected = await new Promise ( ( res , rej ) => {
96
- this . connector . currentProvider . sendAsync ( {
97
- id : 9999999999 ,
98
- jsonrpc : '2.0' ,
99
- method : 'net_listening' ,
100
- params : [ ]
101
- } , ( err , result ) => err ? rej ( err ) : res ( result . result ) ) ;
102
- } ) ;
110
+ const isConnected = await this . connector . eth . getProtocolVersion ( ) . catch ( ( ) => null ) ;
103
111
104
112
if ( ! isConnected ) {
105
113
clearInterval ( this . pingIntervalId ) ;
106
114
this . resetConnector ( ) ;
107
115
}
108
116
} , 5000 ) ;
109
-
110
117
111
- this . filter = this . connector . eth . filter ( 'pending' ) ;
112
- this . filter . watch ( ( err , result ) => {
113
- if ( ! err )
114
- this . events . emit ( 'unconfirmedTx' , result ) ;
118
+
119
+ this . filter = this . connector . eth . subscribe ( 'pendingTransactions' ) ;
120
+ this . filter . on ( 'data' , ( transaction ) => {
121
+ this . events . emit ( 'unconfirmedTx' , transaction ) ;
115
122
} ) ;
116
123
117
124
this . events . emit ( 'provider_set' ) ;
@@ -126,6 +133,8 @@ class providerService {
126
133
*/
127
134
async switchConnectorSafe ( ) {
128
135
136
+ console . log ( 'going to switch connector' )
137
+
129
138
return new Promise ( res => {
130
139
sem . take ( async ( ) => {
131
140
await this . switchConnector ( ) ;
@@ -141,7 +150,12 @@ class providerService {
141
150
* @return {Promise<*|bluebird> }
142
151
*/
143
152
async get ( ) {
144
- return this . connector && this . connector . isConnected ( ) ? this . connector : await this . switchConnectorSafe ( ) ;
153
+
154
+ if ( this . connector ) {
155
+ console . log ( 'is listening: ' , await this . connector . eth . getProtocolVersion ( ) . catch ( ( ) => null ) )
156
+ }
157
+
158
+ return this . connector && await this . connector . eth . getProtocolVersion ( ) . catch ( ( ) => false ) ? this . connector : await this . switchConnectorSafe ( ) ;
145
159
}
146
160
147
161
}
0 commit comments