|
| 1 | +/* |
| 2 | +This file is part of web3.js. |
| 3 | +
|
| 4 | +web3.js is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU Lesser General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +web3.js is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Lesser General Public License |
| 15 | +along with web3.js. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +import WebSocketProvider from 'web3-providers-ws'; |
| 18 | +import { Contract } from 'web3-eth-contract'; |
| 19 | +import { TransactionRevertError } from 'web3-errors'; |
| 20 | +import Web3 from '../../src/index'; |
| 21 | +import { |
| 22 | + createNewAccount, |
| 23 | + getSystemTestProvider, |
| 24 | + isWs, |
| 25 | +} from '../shared_fixtures/system_tests_utils'; |
| 26 | +import { BasicAbi, BasicBytecode } from '../shared_fixtures/build/Basic'; |
| 27 | + |
| 28 | +Error.stackTraceLimit = Infinity; |
| 29 | + |
| 30 | +describe('eth', () => { |
| 31 | + let web3: Web3; |
| 32 | + let accounts: string[] = []; |
| 33 | + let clientUrl: string; |
| 34 | + |
| 35 | + let contract: Contract<typeof BasicAbi>; |
| 36 | + let deployOptions: Record<string, unknown>; |
| 37 | + let sendOptions: Record<string, unknown>; |
| 38 | + |
| 39 | + beforeAll(async () => { |
| 40 | + clientUrl = getSystemTestProvider(); |
| 41 | + const acc1 = await createNewAccount({ unlock: true, refill: true }); |
| 42 | + const acc2 = await createNewAccount({ unlock: true, refill: true }); |
| 43 | + accounts = [acc1.address, acc2.address]; |
| 44 | + web3 = new Web3(getSystemTestProvider()); |
| 45 | + if (isWs) { |
| 46 | + web3 = new Web3( |
| 47 | + new WebSocketProvider( |
| 48 | + clientUrl, |
| 49 | + {}, |
| 50 | + { delay: 1, autoReconnect: false, maxAttempts: 1 }, |
| 51 | + ), |
| 52 | + ); |
| 53 | + } else { |
| 54 | + web3 = new Web3(clientUrl); |
| 55 | + } |
| 56 | + |
| 57 | + if (isWs) { |
| 58 | + contract = new web3.eth.Contract(BasicAbi, undefined, { |
| 59 | + provider: new WebSocketProvider( |
| 60 | + clientUrl, |
| 61 | + {}, |
| 62 | + { delay: 1, autoReconnect: false, maxAttempts: 1 }, |
| 63 | + ), |
| 64 | + }); |
| 65 | + } else { |
| 66 | + contract = new web3.eth.Contract(BasicAbi, undefined, { |
| 67 | + provider: clientUrl, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + deployOptions = { |
| 72 | + data: BasicBytecode, |
| 73 | + arguments: [10, 'string init value'], |
| 74 | + }; |
| 75 | + |
| 76 | + sendOptions = { from: accounts[0], gas: '1000000' }; |
| 77 | + |
| 78 | + contract = await contract.deploy(deployOptions).send(sendOptions); |
| 79 | + }); |
| 80 | + afterAll(() => { |
| 81 | + if (isWs && web3?.provider) { |
| 82 | + (web3.provider as WebSocketProvider).disconnect(); |
| 83 | + (contract.provider as WebSocketProvider).disconnect(); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + describe('handleRevert', () => { |
| 88 | + // todo enable when figure out what happening in eth_call (doesn't throw error) |
| 89 | + // eslint-disable-next-line jest/expect-expect |
| 90 | + it('should get revert reason', async () => { |
| 91 | + contract.handleRevert = true; |
| 92 | + await expect(contract.methods.reverts().send({ from: accounts[0] })).rejects.toThrow( |
| 93 | + new TransactionRevertError( |
| 94 | + 'Returned error: execution reverted: REVERTED WITH REVERT', |
| 95 | + ), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should get revert reason for eth tx', async () => { |
| 100 | + web3.eth.handleRevert = true; |
| 101 | + await expect( |
| 102 | + web3.eth.sendTransaction({ |
| 103 | + from: accounts[0], |
| 104 | + gas: '0x3d0900', |
| 105 | + gasPrice: '0x3B9ACBF4', |
| 106 | + input: '0x608060405234801561001057600080fdklkl', |
| 107 | + nonce: '0x10', |
| 108 | + to: undefined, |
| 109 | + value: '0x0', |
| 110 | + type: '0x0', |
| 111 | + v: '0xa96', |
| 112 | + r: '0x1ba80b16306d1de8ff809c00f67c305e8636326096aba282828d331aa2ec30a1', |
| 113 | + s: '0x39f77e0b68d5524826e4385ad4e1f01e748f32c177840184ae65d9592fdfe5c', |
| 114 | + }), |
| 115 | + ).rejects.toThrow( |
| 116 | + new TransactionRevertError( |
| 117 | + 'Returned error: invalid argument 0: json: cannot unmarshal invalid hex string into Go struct field TransactionArgs.data of type hexutil.Bytes', |
| 118 | + ), |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should execute transaction', async () => { |
| 123 | + web3.eth.handleRevert = true; |
| 124 | + await expect( |
| 125 | + web3.eth.sendTransaction({ |
| 126 | + from: accounts[0], |
| 127 | + to: accounts[1], |
| 128 | + gas: '0x76c0', |
| 129 | + gasPrice: '0x9184e72a000', |
| 130 | + value: '0x9184e72a', |
| 131 | + data: '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675', |
| 132 | + }), |
| 133 | + ).resolves.toBeDefined(); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments