forked from iovlabs-qa/json-rpc-web3js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethereum-js-test.js
494 lines (419 loc) · 22.4 KB
/
ethereum-js-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
const assert = require('chai').assert;
const Web3 = require('web3');
const {
ethers
} = require("ethers");
const BN = require('bignumber.js');
const fs = require('fs');
const path = require('path');
/*
const {
StaticJsonRpcProvider
} = require('@ethersproject/providers');
function getLibrary(provider) {
const library = new StaticJsonRpcProvider(provider);
library.pollingInterval = 15000;
// Fix transaction format error from etherjs getTransactionReceipt as transactionReceipt format
// checks root to be a 32 bytes hash when on RSK its 0x01
const formats = library.formatter && library.formatter.formats;
if (formats) {
formats.receipt.root = formats.receipt.logsBloom;
}
Object.assign(library.formatter, {
formats
})
return library;
}
*/
const rskj_host = process.env.RSKJ_HOST || "127.0.0.1";
const rskj_port = process.env.RSKJ_PORT || 4444;
const rskj_proto = process.env.RSKJ_PROTO || "http";
const rskj_endpoint = `${rskj_proto}://${rskj_host}:${rskj_port}`;
describe(`Rskj ethers.js Smoke Tests`, function () {
this.timeout(10000);
let PRIVATE_KEY = '0xc85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4'; //cow
let testAccount = '0x0000000000000000000000000000000001000006';
let contractAddress = '';
let trxHash = '';
let web3;
var provider;
let web3Provider;
before(async () => {
provider = /*getLibrary(*/new ethers.providers.StaticJsonRpcProvider(rskj_endpoint)/*)*/;
const signer = provider.getSigner();
web3 = new Web3(rskj_endpoint, null, {
transactionConfirmationBlocks: 1
});
web3.evm = {
mine: () => web3.currentProvider.send('evm_mine')
};
/* let currentProvider = new Web3.providers.HttpProvider(rskj_endpoint);
web3Provider = new ethers.providers.Web3Provider(currentProvider);*/
});
it('Network should be RSK', async () => {
// web3_clientVersion
let network = await provider.getNetwork();
assert(network.chainId == 33, "ChainId should be 33 but it's : " + network.chainId);
let clientVersion = await provider.send("web3_clientVersion")
assert(clientVersion.indexOf('RskJ') >= 0, "Network should be RSK but is :" + clientVersion);
})
it('Should advance until block 5', async () => {
let blockNumber = await provider.getBlockNumber();
for (let i = blockNumber; i < 5; i++) {
await provider.send('evm_mine');
}
blockNumber = await provider.getBlockNumber();
assert.isAbove(blockNumber, 4);
})
it('Should have all the simple cached methods work', async () => {
// eth_hashrate
let hashRate = await provider.send("eth_hashrate");
assert.equal(hashRate, '0x0');
// eth_syncing
let isSyncing = await provider.send('eth_syncing');
if (typeof isSyncing === 'object') {
assert.containsAllKeys(isSyncing, ['currentBlock', 'highestBlock', 'startingBlock']);
assert.isAbove(isSyncing.currentBlock, 0);
assert.isAbove(isSyncing.highestBlock, 0);
assert.isAbove(isSyncing.startingBlock, 0);
} else {
assert.equal(isSyncing, false);
}
// net_listening
let isListening = await provider.send("net_listening");
assert(isListening);
// net_peerCount
let peerCount = await provider.send("net_peerCount");
assert(peerCount == '0x0', "Peer count is expected to be 0x0 but it is " + peerCount);
// net_version
let networkId = await provider.send("net_version");
assert.equal(networkId, 33);
// eth_accounts
let accounts = await provider.send("eth_accounts");
assert.isArray(accounts);
// eth_protocolVersion
let protocolVersion = await provider.send("eth_protocolVersion");
assert.equal(protocolVersion, '0x3e');
// eth_mining
let isMining = await provider.send("eth_mining");
assert.equal(isMining, true);
});
// eth_blockNumber
it('eth_blockNumber: Should get the current block number', async () => {
let blockNumber = await provider.getBlockNumber();
assert.isAbove(blockNumber, 4);
});
// eth_gasPrice
it('eth_gasPrice: Should get the gas price', async () => {
let gasPrice = await provider.getGasPrice();
assert.equal(gasPrice.toNumber(), '0');
});
// eth_getTransactionCount
it('eth_getTransactionCount: Should get an accounts transaction count', async () => {
let transactionCount = await provider.getTransactionCount(
testAccount,
'latest'
);
assert.equal(transactionCount, '0');
});
// eth_getBalance
it('eth_getBalance: Should get a the right balance for an account', async () => {
let balance = await provider.getBalance(
testAccount,
'latest'
);
assert.equal(balance, '21000000000000000000000000');
let historicBalance = await provider.getBalance(
testAccount,
0
);
let expectedHistoricBalance = new BN(21000000000000000000000000);
assert.equal(historicBalance - expectedHistoricBalance, 0);
let unusedAccountBalance = await provider.getBalance(
'0x09a1eda29f664ac8f68106f6567276df0c65d859',
'latest'
);
assert.equal(unusedAccountBalance, '1000000000000000000000000000000');
});
// eth_getTransactionByBlockHashAndIndex
// eth_getTransactionByBlockNumberAndIndex
it('eth_getTransactionByBlockHashAndIndex-eth_getTransactionByBlockNumberAndIndex: Should get transactions by block number and hash', async () => {
let blockNumber = "0x1";
let byBlockNumber = await provider.send("eth_getTransactionByBlockNumberAndIndex", [blockNumber, "0x0"]);
let blockHash = byBlockNumber.blockHash;
let byHash = await provider.send("eth_getTransactionByBlockHashAndIndex", [blockHash, "0x0"]);
assert.deepEqual(byBlockNumber, byHash);
try {
let invalidBlock = await provider.send("eth_getTransactionByBlockNumberAndIndex", ['0xdeadbeef0fb9424aad2417321cac62915f6c83827f4d3c8c8c06900a61c4236c', 0]);
} catch (err) {
assert.equal(err.status,400);
assert.include(err.body,'"error":{"code":-32602,"message":"Invalid argument \\"0\\": param should be a hex value string."}');
}
//assert.isNull(invalidBlock);
});
// eth_getBlockTransactionCountByHash
// eth_getBlockTransactionCountByNumber
it(`eth_getBlockTransactionCountByHash-eth_getBlockTransactionCountByNumber: Should return the right number of block transactions`, async () => {
let expectedCount = 1;
let blockNumber = "0x04";
let byBlockNumber = await provider.send("eth_getBlockTransactionCountByNumber", [blockNumber]);
let block = await provider.getBlock(4);
let blockHash = block.hash;
let byHash = await provider.send("eth_getBlockTransactionCountByHash", [blockHash]);
assert.equal(byHash, expectedCount);
assert.equal(byBlockNumber, expectedCount);
});
it('eth_sendRawTransaction & eth_call: Should compile and deploy a contract successfully and interact with that contract', async function () {
this.timeout(40000);
let compiledHelloWorldPath = path.resolve(__dirname, 'Contracts', 'HelloWorld.json');
let compiledContract = fs.readFileSync(compiledHelloWorldPath, 'UTF-8');
let contractOutput = JSON.parse(compiledContract);
let abi = contractOutput.abi;
let contract = new web3.eth.Contract(abi);
let signedAccount = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
let deployment = contract.deploy({
data: '0x6080604052600560005534801561001557600080fd5b5060ff806100246000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b606860c1565b6040518082815260200191505060405180910390f35b806000819055507f93fe6d397c74fdf1402a8b72e47b68512f0510d7b98a4bc4cbdf6ac7108b3c596000546040518082815260200191505060405180910390a150565b6000805490509056fea265627a7a72305820c73a787ed29a46f8a85631abd07c906d900ca03c03b631cc85fe396408072ee164736f6c634300050a0032',
arguments: []
});
let contractData = deployment.encodeABI();
let transaction = {
value: 0,
gasPrice: web3.utils.toHex(10000000),
gas: web3.utils.toHex(1000000),
data: contractData,
chainId: 33
};
let signedTx = await signedAccount.signTransaction(transaction);
let txReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
assert(txReceipt.contractAddress);
contractAddress = txReceipt.contractAddress;
let deployedContract = new web3.eth.Contract(abi, txReceipt.contractAddress);
let getCall = deployedContract.methods.get();
let callParams = {
to: txReceipt.contractAddress,
data: getCall.encodeABI(),
};
let currentVal = await web3.eth.call(callParams);
assert.equal(currentVal, '0x0000000000000000000000000000000000000000000000000000000000000005');
let currentValLatest = await web3.eth.call(callParams, "latest");
assert.equal(currentValLatest, '0x0000000000000000000000000000000000000000000000000000000000000005');
let currentValPending = await web3.eth.call(callParams, "pending");
assert.equal(currentValPending, '0x0000000000000000000000000000000000000000000000000000000000000005');
let setCall = deployedContract.methods.set(34);
let setGasEstimate = await setCall.estimateGas({
from: signedAccount.address
});
let transactionParameters = {
to: txReceipt.contractAddress,
from: signedAccount.address,
gasPrice: '0x4A817C800', // 20000000000
gas: setGasEstimate,
data: setCall.encodeABI(),
chainId: 33
};
let setSignedTx = await signedAccount.signTransaction(transactionParameters);
// Send the transaction.
let receipt = await web3.eth.sendSignedTransaction(setSignedTx.rawTransaction)
.once('transactionHash', (hash) => {
assert.isString(hash);
trxHash = hash;
})
.on('error', (error) => {
assert(false, `Unexpected error sending set transaction: $`);
});
assert.isObject(receipt);
let receiptString = JSON.stringify(receipt);
assert(receiptString.indexOf('transactionHash') > 0, "transactionHash is not being returned and it's expected!");
assert(receiptString.indexOf('transactionIndex') > 0, "transactionIndex is not being returned and it's expected!");
assert(receiptString.indexOf('blockHash') > 0, "blockHash is not being returned and it's expected!");
assert(receiptString.indexOf('blockNumber') > 0, "blockNumber is not being returned and it's expected!");
assert(receiptString.indexOf('cumulativeGasUsed') > 0, "cumulativeGasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('gasUsed') > 0, "gasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('contractAddress') > 0, "contractAddress is not being returned and it's expected!");
assert(receiptString.indexOf('logs') > 0, "logs is not being returned and it's expected!");
assert(receiptString.indexOf('from') > 0, "from is not being returned and it's expected!");
assert(receiptString.indexOf('to') > 0, "to is not being returned and it's expected!");
assert(receiptString.indexOf('status') > 0, "status is not being returned and it's expected!");
assert(receiptString.indexOf('logsBloom') > 0, "logsBloom is not being returned and it's expected!");
await new Promise((res) => setTimeout(res, 5000));
await deployedContract.getPastEvents('ValueChanged', {
fromBlock: 0,
toBlock: 'latest'
}, (error, eventLogs) => {
assert(!error, `Unexpected error reading logs ${error}`);
assert.equal(eventLogs[0].returnValues.newValue, "34");
});
});
it.skip('eth_sendRawTransaction & eth_call: Should compile and deploy a contract successfully and interact with that contract', async function () {
this.timeout(20000);
let wallet = new ethers.Wallet(PRIVATE_KEY, provider);
let compiledHelloWorldPath = path.resolve(__dirname, 'Contracts', 'HelloWorld.json');
let compiledContract = fs.readFileSync(compiledHelloWorldPath, 'UTF-8');
let contractOutput = JSON.parse(compiledContract);
let abi = contractOutput.abi;
let bytecode = contractOutput.bytecode;
let factory = new ethers.ContractFactory(abi, bytecode, wallet);
let contract = await factory.deploy();
contractAddress = contract.address;
assert(contractAddress);
await provider.send('evm_mine');
await contract.deployed();
// First 4 bytes of the hash of "get()" for the Hello World contract
let getData = ethers.utils.hexDataSlice(ethers.utils.id('get()'), 0, 4);
let getTransaction = {
to: contractAddress,
data: getData
}
let callPromise = provider.call(getTransaction);
callPromise.then((result) => {
assert.equal(result, '0x0000000000000000000000000000000000000000000000000000000000000005');
});
// let tx = await contract.set(34);
//console.log("tx: "+ JSON.stringify(tx));
let deployedContract = new web3.eth.Contract(abi, txReceipt.contractAddress);
let getCall = deployedContract.methods.get();
let callParams = {
to: txReceipt.contractAddress,
data: getCall.encodeABI(),
};
let currentVal = await web3.eth.call(callParams);
assert.equal(currentVal, '0x0000000000000000000000000000000000000000000000000000000000000005');
let currentValLatest = await web3.eth.call(callParams, "latest");
assert.equal(currentValLatest, '0x0000000000000000000000000000000000000000000000000000000000000005');
let currentValPending = await web3.eth.call(callParams, "pending");
assert.equal(currentValPending, '0x0000000000000000000000000000000000000000000000000000000000000005');
let setCall = deployedContract.methods.set(34);
let setGasEstimate = await setCall.estimateGas({ from: signedAccount.address });
let transactionParameters = {
to: txReceipt.contractAddress,
from: signedAccount.address,
gasPrice: '0x4A817C800', // 20000000000
gas: setGasEstimate,
data: setCall.encodeABI(),
chainId: 33
};
let setSignedTx = await signedAccount.signTransaction(transactionParameters);
// Send the transaction.
let receipt = await web3.eth.sendSignedTransaction(setSignedTx.rawTransaction)
.once('transactionHash', (hash) => {
assert.isString(hash);
trxHash = hash;
})
.on('error', (error) => {
assert(false, `Unexpected error sending set transaction: $`);
});
let receiptString = JSON.stringify(receipt);
assert(receiptString.indexOf('transactionHash') > 0, "transactionHash is not being returned and it's expected!");
assert(receiptString.indexOf('transactionIndex') > 0, "transactionIndex is not being returned and it's expected!");
assert(receiptString.indexOf('blockHash') > 0, "blockHash is not being returned and it's expected!");
assert(receiptString.indexOf('blockNumber') > 0, "blockNumber is not being returned and it's expected!");
assert(receiptString.indexOf('cumulativeGasUsed') > 0, "cumulativeGasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('gasUsed') > 0, "gasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('contractAddress') > 0, "contractAddress is not being returned and it's expected!");
assert(receiptString.indexOf('logs') > 0, "logs is not being returned and it's expected!");
assert(receiptString.indexOf('from') > 0, "from is not being returned and it's expected!");
assert(receiptString.indexOf('to') > 0, "to is not being returned and it's expected!");
assert(receiptString.indexOf('status') >0, "status is not being returned and it's expected!");
assert(receiptString.indexOf('logsBloom') > 0, "logsBloom is not being returned and it's expected!");
await new Promise((res) => setTimeout(res, 5000));
await deployedContract.getPastEvents('ValueChanged', { fromBlock: 0, toBlock: 'latest' }, (error, eventLogs) => {
assert(!error, `Unexpected error reading logs ${error}`);
assert.equal(eventLogs[0].returnValues.newValue, "34");
});
});
// eth_getLogs
it(`eth_getLogs: Should get the logs of a contract`, async () => {
let logs = await provider.getLogs({
'fromBlock': "0x0",
'toBlock': await provider.getBlockNumber(),
'address': contractAddress,
});
assert.isObject(logs[0]);
let logTrxHash = logs[0].transactionHash;
assert.equal(logTrxHash, trxHash);
let logsString = JSON.stringify(logs[0]);
assert(logsString.indexOf('logIndex') >= 0, "logIndex: is not being returned and it's expected!");
assert(logsString.indexOf('blockNumber') >= 0, "blockNumber: is not being returned and it's expected!");
assert(logsString.indexOf('blockHash') >= 0, "blockHash: is not being returned and it's expected!");
assert(logsString.indexOf('transactionIndex') >= 0, "transactionIndex: is not being returned and it's expected!");
assert(logsString.indexOf('address') >= 0, "address: is not being returned and it's expected!");
assert(logsString.indexOf('data') >= 0, "data: is not being returned and it's expected!");
assert(logsString.indexOf('topics') >= 0, "topics: is not being returned and it's expected!");
assert(logsString.indexOf('transactionHash') >= 0, "transactionHash is not being returned and it's expected!");
});
it(`eth_getTransactionByHash: Should get a transaction by its hash`, async () => {
let tx = await provider.getTransaction(trxHash);
assert.isObject(tx);
let txString = JSON.stringify(tx);
assert(txString.indexOf('hash') > 0, "hash is not being returned and it's expected!");
assert(txString.indexOf('transactionIndex') > 0, "transactionIndex is not being returned and it's expected!");
assert(txString.indexOf('blockHash') > 0, "blockHash is not being returned and it's expected!");
assert(txString.indexOf('blockNumber') > 0, "blockNumber is not being returned and it's expected!");
assert(txString.indexOf('value') > 0, "value is not being returned and it's expected!");
assert(txString.indexOf('from') > 0, "from is not being returned and it's expected!");
assert(txString.indexOf('to') > 0, "to is not being returned and it's expected!");
assert(txString.indexOf('gasPrice') > 0, "gasPrice is not being returned and it's expected!");
assert(txString.indexOf('gas') > 0, "gas is not being returned and it's expected!");
assert(txString.indexOf('"v"') > 0, "v: is not being returned and it's expected!");
assert(txString.indexOf('"r"') > 0, "r: is not being returned and it's expected!");
assert(txString.indexOf('"s"') > 0, "s: is not being returned and it's expected!");
let invalidTx = await provider.getTransaction('0x5eae996aa609c0b9db434c7a2411437fefc3ff16046b71ad102453cfdeadbeef');
assert.isNull(invalidTx);
});
// eth_getTransactionReceipt
it.skip(`eth_getTransactionReceipt: Should get transaction receipt`, async () => {
let receipt = await provider.getTransactionReceipt(trxHash);
assert.isObject(receipt);
let receiptString = JSON.stringify(receipt);
assert(receiptString.indexOf('transactionHash') >= 0, "transactionHash is not being returned and it's expected!");
assert(receiptString.indexOf('transactionIndex') >= 0, "transactionIndex is not being returned and it's expected!");
assert(receiptString.indexOf('blockHash') >= 0, "blockHash is not being returned and it's expected!");
assert(receiptString.indexOf('blockNumber') >= 0, "blockNumber is not being returned and it's expected!");
assert(receiptString.indexOf('cumulativeGasUsed') >= 0, "cumulativeGasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('gasUsed') >= 0, "gasUsed is not being returned and it's expected!");
assert(receiptString.indexOf('contractAddress') >= 0, "contractAddress is not being returned and it's expected!");
assert(receiptString.indexOf('logs') >= 0, "logs is not being returned and it's expected!");
assert(receiptString.indexOf('from') >= 0, "from is not being returned and it's expected!");
assert(receiptString.indexOf('to') >= 0, "to is not being returned and it's expected!");
assert(receiptString.indexOf('status') >= 0, "status is not being returned and it's expected!");
assert(receiptString.indexOf('logsBloom') >= 0, "logsBloom is not being returned and it's expected!");
// let invalidTx = await provider.getTransactionReceipt('0xd05274b72ca6346bcce89a64cd42ddd28d885fdd06772efe0fe7d19fdeadbeef');
// assert.isNull(invalidTx);
});
// eth_getStorageAt
it(`eth_getStorageAt: Should get storage at a specific location`, async () => {
let storageValue = await provider.getStorageAt(
contractAddress,
0,
'latest'
);
assert.equal(storageValue, '0x0000000000000000000000000000000000000000000000000000000000000022');
});
//eth_coinbase
it(`eth_coinbase: Should return coinbase from RskJ`, async () => {
let coinbase = await provider.send("eth_coinbase");
assert.equal(coinbase, '0xec4ddeb4380ad69b3e509baad9f158cdf4e4681d');
});
// eth_getCode
it(`eth_getCode: Should return the contract's code`, async () => {
let contractCode = await provider.getCode(contractAddress, 'latest');
assert.equal(contractCode, '0x6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b606860c1565b6040518082815260200191505060405180910390f35b806000819055507f93fe6d397c74fdf1402a8b72e47b68512f0510d7b98a4bc4cbdf6ac7108b3c596000546040518082815260200191505060405180910390a150565b6000805490509056fea265627a7a72305820c73a787ed29a46f8a85631abd07c906d900ca03c03b631cc85fe396408072ee164736f6c634300050a0032');
let accountCount = await provider.getCode(testAccount, 'earliest');
assert.equal('0x', accountCount);
let invalidAccount = await provider.getCode('0x0000000000000000000000000000000000000001', 'latest');
assert.equal('0x', invalidAccount);
});
// eth_getBlockByHash
// eth_getBlockByNumber
it(`eth_getBlockByHash-eth_getBlockByNumber: Should get the block by hash and number`, async () => {
this.timeout(20000);
let blockNumber = '0x2';
let byNumber = await provider.getBlock(blockNumber);
let blockHash = byNumber.hash;
let byHash = await provider.getBlock(blockHash);
assert.deepEqual(byHash, byNumber);
let withTransactions = await provider.getBlock(blockHash, true);
assert.equal(withTransactions.transactions.length, 1);
assert.isNotNull(withTransactions.transactions[0]);
});
});