|
21 | 21 | // external resources |
22 | 22 | import { expect } from 'chai'; |
23 | 23 | import { ethers, WebSocketProvider } from 'ethers'; |
| 24 | +import { WsTestConstant, WsTestHelper } from '../helper'; |
24 | 25 | import { Utils } from '@hashgraph/json-rpc-server/tests/helpers/utils'; |
25 | 26 | import ERC20MockJson from '@hashgraph/json-rpc-server/tests/contracts/ERC20Mock.json'; |
26 | 27 | import { AliasAccount } from '@hashgraph/json-rpc-server/tests/clients/servicesClient'; |
27 | 28 |
|
28 | 29 | describe('@release @web-socket eth_call', async function () { |
29 | | - const WS_RELAY_URL = `${process.env.WS_RELAY_URL}`; |
30 | 30 | const METHOD_NAME = 'eth_call'; |
31 | | - const FAKE_TX_HASH = `0x${'00'.repeat(20)}`; |
32 | 31 | const INVALID_PARAMS = [ |
33 | 32 | ['{}', false, '0x0'], |
34 | 33 | ["{ to: '0xabcdef', data: '0x1a2b3c4d' }", 36, ''], |
35 | | - [{ to: FAKE_TX_HASH, data: 36 }, 'latest'], |
36 | 34 | ]; |
37 | 35 |
|
38 | 36 | const INVALID_TX_INFO = [ |
@@ -60,76 +58,111 @@ describe('@release @web-socket eth_call', async function () { |
60 | 58 | }, |
61 | 59 | ]; |
62 | 60 |
|
63 | | - let accounts: AliasAccount[] = []; |
64 | | - let requestId: string, wsProvider: WebSocketProvider; |
65 | | - let erc20TokenAddr: string, erc20EtherInterface: ethers.Interface; |
| 61 | + let requestId: string, |
| 62 | + erc20TokenAddr: string, |
| 63 | + accounts: AliasAccount[] = [], |
| 64 | + ethersWsProvider: WebSocketProvider, |
| 65 | + erc20EtherInterface: ethers.Interface; |
66 | 66 |
|
67 | 67 | before(async () => { |
68 | | - // @ts-ignore |
69 | | - const { servicesNode, relay } = global; |
70 | | - |
71 | | - accounts[0] = await servicesNode.createAliasAccount(100, relay.provider, requestId); |
72 | | - await new Promise((r) => setTimeout(r, 3000)); |
| 68 | + accounts[0] = await global.servicesNode.createAliasAccount(100, global.relay.provider, requestId); |
| 69 | + await new Promise((r) => setTimeout(r, 1000)); // wait for accounts[0] to propagate |
73 | 70 |
|
74 | 71 | const erc20Contract = await Utils.deployContractWithEthers( |
75 | 72 | [TOKEN_NAME, TOKEN_SYMBOL, accounts[0].address, TOKEN_INIT_SUPPLY], |
76 | 73 | ERC20MockJson, |
77 | 74 | accounts[0].wallet, |
78 | | - relay, |
| 75 | + global.relay, |
79 | 76 | ); |
80 | 77 |
|
81 | 78 | erc20TokenAddr = await erc20Contract.getAddress(); |
82 | 79 | erc20EtherInterface = new ethers.Interface(ERC20MockJson.abi); |
83 | 80 | }); |
84 | 81 |
|
85 | 82 | beforeEach(async () => { |
86 | | - wsProvider = new ethers.WebSocketProvider(WS_RELAY_URL); |
87 | | - await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 83 | + ethersWsProvider = new ethers.WebSocketProvider(WsTestConstant.WS_RELAY_URL); |
88 | 84 | }); |
89 | 85 |
|
90 | 86 | afterEach(async () => { |
91 | | - if (wsProvider) { |
92 | | - await wsProvider.destroy(); |
93 | | - await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 87 | + if (ethersWsProvider) await ethersWsProvider.destroy(); |
| 88 | + }); |
| 89 | + |
| 90 | + after(async () => { |
| 91 | + // expect all the connections to be closed after all |
| 92 | + expect(global.socketServer._connections).to.eq(0); |
| 93 | + }); |
| 94 | + |
| 95 | + describe(WsTestConstant.STANDARD_WEB_SOCKET, () => { |
| 96 | + for (const params of INVALID_PARAMS) { |
| 97 | + it(`Should fail ${METHOD_NAME} on ${WsTestConstant.STANDARD_WEB_SOCKET} and throw predefined.INVALID_PARAMETERS if the request's params variable is invalid. params=[${params}]`, async () => { |
| 98 | + await WsTestHelper.assertFailInvalidParamsStandardWebSocket(METHOD_NAME, params); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + for (const params of INVALID_TX_INFO) { |
| 103 | + it(`Should fail ${METHOD_NAME} on ${ |
| 104 | + WsTestConstant.STANDARD_WEB_SOCKET |
| 105 | + } and handle invalid TX_INFO. params=[${JSON.stringify(params)}]`, async () => { |
| 106 | + await WsTestHelper.assertFailInvalidParamsStandardWebSocket(METHOD_NAME, params); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + for (const data of VALID_DATA) { |
| 111 | + it(`Should execute ${METHOD_NAME} on ${WsTestConstant.STANDARD_WEB_SOCKET} and handle valid requests correctly`, async () => { |
| 112 | + const tx = { |
| 113 | + to: erc20TokenAddr, |
| 114 | + data: data.sighash, |
| 115 | + }; |
| 116 | + |
| 117 | + const response = await WsTestHelper.sendRequestToStandardWebSocket(METHOD_NAME, [tx, 'latest']); |
| 118 | + WsTestHelper.assertJsonRpcObject(response); |
| 119 | + |
| 120 | + const outputASCII = erc20EtherInterface.decodeFunctionResult( |
| 121 | + erc20EtherInterface.getFunction(data.sighash)!, |
| 122 | + response.result, |
| 123 | + ); |
| 124 | + expect(outputASCII[0]).to.eq(data.output); |
| 125 | + }); |
94 | 126 | } |
95 | 127 | }); |
96 | 128 |
|
97 | | - for (const params of INVALID_PARAMS) { |
98 | | - it(`Should throw predefined.INVALID_PARAMETERS if the request's params variable is invalid. params=[${params}]`, async () => { |
99 | | - try { |
100 | | - await wsProvider.send(METHOD_NAME, params); |
101 | | - expect(true).to.eq(false); |
102 | | - } catch (error) { |
103 | | - expect(error.info).to.exist; |
104 | | - expect(error.info.error.code).to.eq(-32602); |
105 | | - } |
106 | | - }); |
107 | | - } |
108 | | - |
109 | | - for (const params of INVALID_TX_INFO) { |
110 | | - it(`Should handle invalid TX_INFO A. params=[${JSON.stringify(params)}]`, async () => { |
111 | | - try { |
112 | | - await wsProvider.send(METHOD_NAME, [...params]); |
113 | | - expect(true).to.eq(false); |
114 | | - } catch (error) { |
115 | | - expect(error).to.exist; |
116 | | - expect(error.argument).to.eq('address'); |
117 | | - expect(error.code).to.eq('INVALID_ARGUMENT'); |
118 | | - expect(error.shortMessage).to.eq('invalid address'); |
119 | | - } |
120 | | - }); |
121 | | - } |
122 | | - |
123 | | - for (const data of VALID_DATA) { |
124 | | - it(`Should handle valid requests correctly`, async () => { |
125 | | - const tx = { |
126 | | - to: erc20TokenAddr, |
127 | | - data: data.sighash, |
128 | | - }; |
129 | | - |
130 | | - const output = await wsProvider.send(METHOD_NAME, [tx, 'latest']); |
131 | | - const result = erc20EtherInterface.decodeFunctionResult(erc20EtherInterface.getFunction(data.sighash)!, output); |
132 | | - expect(result[0]).to.eq(data.output); |
133 | | - }); |
134 | | - } |
| 129 | + describe(WsTestConstant.ETHERS_WS_PROVIDER, () => { |
| 130 | + for (const params of INVALID_PARAMS) { |
| 131 | + it(`Should fail ${METHOD_NAME} on ${WsTestConstant.ETHERS_WS_PROVIDER} and throw predefined.INVALID_PARAMETERS if the request's params variable is invalid. params=[${params}]`, async () => { |
| 132 | + await WsTestHelper.assertFailInvalidParamsEthersWsProvider(ethersWsProvider, METHOD_NAME, params); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + for (const params of INVALID_TX_INFO) { |
| 137 | + it(`Should fail ${METHOD_NAME} on ${ |
| 138 | + WsTestConstant.ETHERS_WS_PROVIDER |
| 139 | + } and handle invalid TX_INFO. params=[${JSON.stringify(params)}]`, async () => { |
| 140 | + try { |
| 141 | + await ethersWsProvider.send(METHOD_NAME, params); |
| 142 | + expect(true).to.eq(false); |
| 143 | + } catch (error) { |
| 144 | + expect(error).to.exist; |
| 145 | + expect(error.argument).to.eq('address'); |
| 146 | + expect(error.code).to.eq('INVALID_ARGUMENT'); |
| 147 | + expect(error.shortMessage).to.eq('invalid address'); |
| 148 | + } |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + for (const data of VALID_DATA) { |
| 153 | + it(`Should execute ${METHOD_NAME} on ${WsTestConstant.ETHERS_WS_PROVIDER} and handle valid requests correctly`, async () => { |
| 154 | + const tx = { |
| 155 | + to: erc20TokenAddr, |
| 156 | + data: data.sighash, |
| 157 | + }; |
| 158 | + |
| 159 | + const output = await ethersWsProvider.send(METHOD_NAME, [tx, 'latest']); |
| 160 | + const outputASCII = erc20EtherInterface.decodeFunctionResult( |
| 161 | + erc20EtherInterface.getFunction(data.sighash)!, |
| 162 | + output, |
| 163 | + ); |
| 164 | + expect(outputASCII[0]).to.eq(data.output); |
| 165 | + }); |
| 166 | + } |
| 167 | + }); |
135 | 168 | }); |
0 commit comments