@@ -16,41 +16,48 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
18
18
/* eslint-disable */
19
- const { Web3 } = require ( '../../../lib/commonjs' ) ;
20
- const { IpcProvider } = require ( '../../../../web3-providers-ipc/lib/commonjs' ) ;
21
- const contractData = require ( '../../../../../fixtures/build/Basic.json' ) ;
19
+ import { Web3 , Contract , Numbers , EventLog } from 'web3' ;
20
+ import { IpcProvider } from 'web3-providers-ipc' ;
21
+ import { BasicBytecode , BasicAbi } from '../../shared_fixtures/build/Basic' ;
22
+ import { Web3Account } from 'web3-eth-accounts' ;
22
23
23
- const providerString = process . env . WEB3_SYSTEM_TEST_PROVIDER ;
24
+ const providerString = String ( process . env . WEB3_SYSTEM_TEST_PROVIDER ) ;
24
25
const isWs = providerString . startsWith ( 'ws' ) ;
25
26
const isIpc = providerString . includes ( 'ipc' ) ;
26
- const contracts = { } ;
27
+ const contracts : { [ key : string ] : Contract < typeof BasicAbi > } = { } ;
27
28
28
- const deployContracts = async ( web3 , accounts ) => {
29
+ const deployContracts = async ( web3 : Web3 , accounts : Web3Account [ ] ) => {
29
30
const prs = [ ] ;
30
31
for ( let i = 0 ; i < accounts . length ; i ++ ) {
31
32
const account = accounts [ i ] ;
32
33
const sendOptions = { from : account . address } ;
33
34
const deployOptions = {
34
- data : contractData . evm . bytecode . object ,
35
- arguments : [ 123 , '' ] ,
35
+ data : BasicBytecode ,
36
+ arguments : [ 123 , '' ] as [ number , string ] ,
36
37
gas : BigInt ( 9000000000000 ) ,
37
38
gasLimit : BigInt ( 9000000000000 ) ,
38
39
type : BigInt ( 0 ) ,
39
40
} ;
40
- const c = new web3 . eth . Contract ( contractData . abi ) ;
41
+ const c = new web3 . eth . Contract < typeof BasicAbi > ( BasicAbi ) ;
41
42
prs . push (
42
43
c
43
44
. deploy ( deployOptions )
44
45
. send ( sendOptions )
45
- . then ( contract => {
46
+ . then ( ( contract : typeof c ) => {
46
47
contracts [ account . address ] = contract ;
47
48
} ) ,
48
49
) ;
49
50
}
50
51
await Promise . all ( prs ) ;
51
52
} ;
52
53
53
- const addAccount = async ( web3 , mainAcc , address , privateKey , nonce ) => {
54
+ const addAccount = async (
55
+ web3 : Web3 ,
56
+ mainAcc : string ,
57
+ address : string ,
58
+ privateKey : string ,
59
+ nonce : Numbers ,
60
+ ) => {
54
61
web3 . eth . accounts . wallet . add ( privateKey ) ;
55
62
return web3 . eth . sendTransaction ( {
56
63
from : mainAcc ,
@@ -61,11 +68,11 @@ const addAccount = async (web3, mainAcc, address, privateKey, nonce) => {
61
68
} ) ;
62
69
} ;
63
70
64
- const prepareAccounts = async ( web3 , n = 1000 ) => {
71
+ const prepareAccounts = async ( web3 : Web3 , n = 1000 ) => {
65
72
const prs = [ ] ;
66
73
const list = await web3 . eth . personal . getAccounts ( ) ;
67
74
const mainAcc = list [ 0 ] ;
68
- const accountList = [ ] ;
75
+ const accountList : Web3Account [ ] = [ ] ;
69
76
const nonce = await web3 . eth . getTransactionCount ( mainAcc ) ;
70
77
for ( let i = 0 ; i < n ; i ++ ) {
71
78
const acc = web3 . eth . accounts . create ( ) ;
@@ -76,20 +83,20 @@ const prepareAccounts = async (web3, n = 1000) => {
76
83
return accountList ;
77
84
} ;
78
85
79
- const sendData = async ( web3 , account ) => {
86
+ const sendData = async ( account : Web3Account ) => {
80
87
const contract = contracts [ account . address ] ;
81
88
return contract . methods
82
89
. firesStringEvent ( `String event: ${ account . address } ` )
83
90
. send ( { from : account . address } ) ;
84
91
} ;
85
92
86
- const getData = async ( web3 , account ) => {
93
+ const getData = async ( account : Web3Account ) => {
87
94
const contract = contracts [ account . address ] ;
88
95
await contract . methods . getStringValue ( ) . call ( ) ;
89
96
} ;
90
97
91
- const receivedEvents = { } ;
92
- const subscribeContract = acc => {
98
+ const receivedEvents : { [ key : string ] : EventLog } = { } ;
99
+ const subscribeContract = ( acc : Web3Account ) => {
93
100
const contract = contracts [ acc . address ] ;
94
101
const event = contract . events . StringEvent ( ) ;
95
102
@@ -100,7 +107,7 @@ const subscribeContract = acc => {
100
107
receivedEvents [ acc . address ] = res ;
101
108
} ) ;
102
109
} ;
103
- const contractSubscriptions = ( web3 , accounts ) => {
110
+ const contractSubscriptions = ( accounts : Web3Account [ ] ) => {
104
111
console . log ( `Subscribe to ${ accounts . length } contracts events` ) ;
105
112
for ( const acc of accounts ) {
106
113
subscribeContract ( acc ) ;
@@ -123,13 +130,13 @@ const test = async () => {
123
130
await deployContracts ( web3 , accounts ) ;
124
131
// if socket subscribe to events
125
132
if ( isIpc || isWs ) {
126
- contractSubscriptions ( web3 , accounts ) ;
133
+ contractSubscriptions ( accounts ) ;
127
134
}
128
135
129
136
console . log ( `Send data from ${ n } accounts in parallel` ) ;
130
137
const sendPrs = [ ] ;
131
138
for ( let i = 0 ; i < n ; i ++ ) {
132
- sendPrs . push ( sendData ( web3 , accounts [ i ] ) ) ;
139
+ sendPrs . push ( sendData ( accounts [ i ] ) ) ;
133
140
}
134
141
await Promise . all ( sendPrs ) ;
135
142
@@ -142,11 +149,11 @@ const test = async () => {
142
149
console . log ( `Get data from ${ n } accounts in parallel` ) ;
143
150
const getPrs = [ ] ;
144
151
for ( let i = 0 ; i < n ; i ++ ) {
145
- getPrs . push ( getData ( web3 , accounts [ i ] ) ) ;
152
+ getPrs . push ( getData ( accounts [ i ] ) ) ;
146
153
}
147
154
await Promise . all ( getPrs ) ;
148
155
if ( isIpc || isWs ) {
149
- web3 . provider . disconnect ( ) ;
156
+ ( web3 . provider as IpcProvider ) . disconnect ( ) ;
150
157
}
151
158
} ;
152
159
0 commit comments