|
| 1 | +/*- |
| 2 | + * |
| 3 | + * Hedera JSON RPC Relay |
| 4 | + * |
| 5 | + * Copyright (C) 2024 Hedera Hashgraph, LLC |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +import { expect } from 'chai'; |
| 22 | +import { ethers, WebSocketProvider } from 'ethers'; |
| 23 | +import { WsTestConstant, WsTestHelper } from '../helper'; |
| 24 | +import { Utils } from '@hashgraph/json-rpc-server/tests/helpers/utils'; |
| 25 | + |
| 26 | +describe('@release @web-socket-batch-2 eth_newFilter', async function () { |
| 27 | + let wsFilterObj: any, ethersWsProvider: WebSocketProvider; |
| 28 | + const METHOD_NAME = 'eth_newFilter'; |
| 29 | + const INVALID_PARAMS = [ |
| 30 | + [], |
| 31 | + [ |
| 32 | + { |
| 33 | + address: WsTestConstant.FAKE_TX_HASH, |
| 34 | + fromBlock: '0xhedera', |
| 35 | + toBlock: 'latest', |
| 36 | + }, |
| 37 | + ], |
| 38 | + [ |
| 39 | + { |
| 40 | + address: WsTestConstant.FAKE_TX_HASH, |
| 41 | + fromBlock: 'latest', |
| 42 | + toBlock: '0xhedera', |
| 43 | + }, |
| 44 | + ], |
| 45 | + ]; |
| 46 | + |
| 47 | + const SIMPLE_CONTRACT_BYTECODE = |
| 48 | + '0x6080604052348015600f57600080fd5b507f4e7df42af9a017b7c655a28ef10cbc8f05b2b088f087ee02416cfa1a96ac3be26007604051603e91906091565b60405180910390a160aa565b6000819050919050565b6000819050919050565b6000819050919050565b6000607d6079607584604a565b605e565b6054565b9050919050565b608b816068565b82525050565b600060208201905060a460008301846084565b92915050565b603f8060b76000396000f3fe6080604052600080fdfea264697066735822122084db7fe76bde5c9c041d61bb40294c56dc6d339bdbc8e0cd285fc4008ccefc2c64736f6c63430008180033'; |
| 49 | + |
| 50 | + before(async () => { |
| 51 | + // deploy contract |
| 52 | + const contract = await Utils.deployContract([], SIMPLE_CONTRACT_BYTECODE, global.accounts[0].wallet); |
| 53 | + |
| 54 | + wsFilterObj = { |
| 55 | + address: [contract.target], |
| 56 | + fromBlock: '0x0', |
| 57 | + toBlock: 'latest', |
| 58 | + }; |
| 59 | + }); |
| 60 | + |
| 61 | + beforeEach(async () => { |
| 62 | + ethersWsProvider = new ethers.WebSocketProvider(WsTestConstant.WS_RELAY_URL); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(async () => { |
| 66 | + if (ethersWsProvider) await ethersWsProvider.destroy(); |
| 67 | + }); |
| 68 | + |
| 69 | + after(async () => { |
| 70 | + // expect all the connections to be closed after all |
| 71 | + expect(global.socketServer._connections).to.eq(0); |
| 72 | + }); |
| 73 | + |
| 74 | + describe(WsTestConstant.STANDARD_WEB_SOCKET, () => { |
| 75 | + for (const params of INVALID_PARAMS) { |
| 76 | + it(`Should fail eth_newFilter on Standard Web Socket and throw predefined.INVALID_PARAMETERS if the request's params variable is invalid. params=[${JSON.stringify( |
| 77 | + params, |
| 78 | + )}]`, async () => { |
| 79 | + await WsTestHelper.assertFailInvalidParamsStandardWebSocket(METHOD_NAME, params); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + it(`Should execute eth_newFilter on Standard Web Socket and handle valid requests correctly`, async () => { |
| 84 | + const response = await WsTestHelper.sendRequestToStandardWebSocket(METHOD_NAME, [wsFilterObj]); |
| 85 | + WsTestHelper.assertJsonRpcObject(response); |
| 86 | + const filterId = response.result; |
| 87 | + |
| 88 | + await new Promise((r) => setTimeout(r, 90000)); |
| 89 | + |
| 90 | + expect(filterId).to.exist; |
| 91 | + expect(filterId.startsWith('0x')).to.be.true; |
| 92 | + expect(filterId.slice(2).length).to.eq(32); // 16 bytes |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe(WsTestConstant.ETHERS_WS_PROVIDER, () => { |
| 97 | + for (const params of INVALID_PARAMS) { |
| 98 | + it(`Should fail eth_newFilter on Ethers Web Socket Provider and throw predefined.INVALID_PARAMETERS if the request's params variable is invalid. params=[${JSON.stringify( |
| 99 | + params, |
| 100 | + )}]`, async () => { |
| 101 | + await WsTestHelper.assertFailInvalidParamsEthersWsProvider(ethersWsProvider, METHOD_NAME, params); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + it(`Should execute eth_newFilter on Ethers Web Socket Provider and handle valid requests correctly`, async () => { |
| 106 | + const filterId = await ethersWsProvider.send(METHOD_NAME, [wsFilterObj]); |
| 107 | + |
| 108 | + expect(filterId).to.exist; |
| 109 | + expect(filterId.startsWith('0x')).to.be.true; |
| 110 | + expect(filterId.slice(2).length).to.eq(32); // 16 bytes |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments