From 3b95b5ef52fcda4c3e40066a2cf54f9264389fab Mon Sep 17 00:00:00 2001 From: Nikos Iliakis Date: Mon, 22 May 2023 19:40:07 +0300 Subject: [PATCH] fix estimateGas to accept hex data without 0x prefix (#6103) * Try solution * use isInt * Add test * Update test * update result and add unit test * Revert change in test * Try to fix test for cypress * Use hex string to avoid bigint --- .../contract_estimateGas_without_0x.test.ts | 73 +++++++++++++++++++ packages/web3-utils/src/converters.ts | 4 + .../web3-utils/test/fixtures/converters.ts | 1 + 3 files changed, 78 insertions(+) create mode 100644 packages/web3-eth-contract/test/integration/contract_estimateGas_without_0x.test.ts diff --git a/packages/web3-eth-contract/test/integration/contract_estimateGas_without_0x.test.ts b/packages/web3-eth-contract/test/integration/contract_estimateGas_without_0x.test.ts new file mode 100644 index 00000000000..91d484665eb --- /dev/null +++ b/packages/web3-eth-contract/test/integration/contract_estimateGas_without_0x.test.ts @@ -0,0 +1,73 @@ +/* +This file is part of web3.js. + +web3.js is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +web3.js is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with web3.js. If not, see . +*/ + +import { ETH_DATA_FORMAT } from 'web3-types'; +import { Contract } from '../../src'; +import { getSystemTestProvider, createTempAccount } from '../fixtures/system_test_utils'; + +describe('contract', () => { + // Create a new contract object using the ABI and bytecode + const abi = [ + { + inputs: [{ internalType: 'uint256', name: '_myNumber', type: 'uint256' }], + stateMutability: 'nonpayable', + type: 'constructor', + }, + { + inputs: [], + name: 'myNumber', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: '_myNumber', type: 'uint256' }], + name: 'setMyNumber', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + ]; + let acc: { address: string; privateKey: string }; + let contract: Contract; + + beforeEach(async () => { + acc = await createTempAccount(); + }); + + it('should be able to add `data` input without `0x` prefix', async () => { + contract = new Contract(abi, undefined, { + provider: getSystemTestProvider(), + }); + + const myContract = contract.deploy({ + data: '608060405234801561001057600080fd5b506040516101d93803806101d983398181016040528101906100329190610054565b806000819055505061009e565b60008151905061004e81610087565b92915050565b60006020828403121561006657600080fd5b60006100748482850161003f565b91505092915050565b6000819050919050565b6100908161007d565b811461009b57600080fd5b50565b61012c806100ad6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806323fd0e401460375780636ffd773c146051575b600080fd5b603d6069565b6040516048919060bf565b60405180910390f35b6067600480360381019060639190608c565b606f565b005b60005481565b8060008190555050565b60008135905060868160e2565b92915050565b600060208284031215609d57600080fd5b600060a9848285016079565b91505092915050565b60b98160d8565b82525050565b600060208201905060d2600083018460b2565b92915050565b6000819050919050565b60e98160d8565b811460f357600080fd5b5056fea2646970667358221220d28cf161457f7936995800eb9896635a02a559a0561bff6a09a40bfb81cd056564736f6c63430008000033', + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + arguments: [1], + }); + + const gas = await myContract.estimateGas( + { + from: acc.address, + }, + ETH_DATA_FORMAT, + ); + expect(gas).toBeDefined(); + expect(gas).toMatch(/0[xX][0-9a-fA-F]/i); + }); +}); diff --git a/packages/web3-utils/src/converters.ts b/packages/web3-utils/src/converters.ts index ed0c89d1c33..e1ca62930ff 100644 --- a/packages/web3-utils/src/converters.ts +++ b/packages/web3-utils/src/converters.ts @@ -23,6 +23,7 @@ import { isHex, isHexStrict, isNullish, + isInt, utils as validatorUtils, validator, } from 'web3-validator'; @@ -365,6 +366,9 @@ export const toHex = ( if (isHexStrict(value)) { return returnType ? 'bytes' : value; } + if (isHex(value) && !isInt(value)) { + return returnType ? 'bytes' : `0x${value}`; + } if (!Number.isFinite(value)) { return returnType ? 'string' : utf8ToHex(value); diff --git a/packages/web3-utils/test/fixtures/converters.ts b/packages/web3-utils/test/fixtures/converters.ts index b99efa1baea..ed3f7bb9274 100644 --- a/packages/web3-utils/test/fixtures/converters.ts +++ b/packages/web3-utils/test/fixtures/converters.ts @@ -234,6 +234,7 @@ export const toHexValidData: [Numbers | Bytes | Address | boolean, [HexString, V ['0x72fdb1c1ddd4c67804f42b93de95cf6a8c51d2d1', 'address'], ], ['-0x01', ['-0x1', 'int256']], + ['123c', ['0x123c', 'bytes']], ]; export const toHexInvalidData: [any, string][] = [