Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 3b95b5e

Browse files
authoredMay 22, 2023
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
1 parent 8c3a17b commit 3b95b5e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
18+
import { ETH_DATA_FORMAT } from 'web3-types';
19+
import { Contract } from '../../src';
20+
import { getSystemTestProvider, createTempAccount } from '../fixtures/system_test_utils';
21+
22+
describe('contract', () => {
23+
// Create a new contract object using the ABI and bytecode
24+
const abi = [
25+
{
26+
inputs: [{ internalType: 'uint256', name: '_myNumber', type: 'uint256' }],
27+
stateMutability: 'nonpayable',
28+
type: 'constructor',
29+
},
30+
{
31+
inputs: [],
32+
name: 'myNumber',
33+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
34+
stateMutability: 'view',
35+
type: 'function',
36+
},
37+
{
38+
inputs: [{ internalType: 'uint256', name: '_myNumber', type: 'uint256' }],
39+
name: 'setMyNumber',
40+
outputs: [],
41+
stateMutability: 'nonpayable',
42+
type: 'function',
43+
},
44+
];
45+
let acc: { address: string; privateKey: string };
46+
let contract: Contract<typeof abi>;
47+
48+
beforeEach(async () => {
49+
acc = await createTempAccount();
50+
});
51+
52+
it('should be able to add `data` input without `0x` prefix', async () => {
53+
contract = new Contract(abi, undefined, {
54+
provider: getSystemTestProvider(),
55+
});
56+
57+
const myContract = contract.deploy({
58+
data: '608060405234801561001057600080fd5b506040516101d93803806101d983398181016040528101906100329190610054565b806000819055505061009e565b60008151905061004e81610087565b92915050565b60006020828403121561006657600080fd5b60006100748482850161003f565b91505092915050565b6000819050919050565b6100908161007d565b811461009b57600080fd5b50565b61012c806100ad6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806323fd0e401460375780636ffd773c146051575b600080fd5b603d6069565b6040516048919060bf565b60405180910390f35b6067600480360381019060639190608c565b606f565b005b60005481565b8060008190555050565b60008135905060868160e2565b92915050565b600060208284031215609d57600080fd5b600060a9848285016079565b91505092915050565b60b98160d8565b82525050565b600060208201905060d2600083018460b2565b92915050565b6000819050919050565b60e98160d8565b811460f357600080fd5b5056fea2646970667358221220d28cf161457f7936995800eb9896635a02a559a0561bff6a09a40bfb81cd056564736f6c63430008000033',
59+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
60+
// @ts-expect-error
61+
arguments: [1],
62+
});
63+
64+
const gas = await myContract.estimateGas(
65+
{
66+
from: acc.address,
67+
},
68+
ETH_DATA_FORMAT,
69+
);
70+
expect(gas).toBeDefined();
71+
expect(gas).toMatch(/0[xX][0-9a-fA-F]/i);
72+
});
73+
});

‎packages/web3-utils/src/converters.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isHex,
2424
isHexStrict,
2525
isNullish,
26+
isInt,
2627
utils as validatorUtils,
2728
validator,
2829
} from 'web3-validator';
@@ -365,6 +366,9 @@ export const toHex = (
365366
if (isHexStrict(value)) {
366367
return returnType ? 'bytes' : value;
367368
}
369+
if (isHex(value) && !isInt(value)) {
370+
return returnType ? 'bytes' : `0x${value}`;
371+
}
368372

369373
if (!Number.isFinite(value)) {
370374
return returnType ? 'string' : utf8ToHex(value);

‎packages/web3-utils/test/fixtures/converters.ts

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export const toHexValidData: [Numbers | Bytes | Address | boolean, [HexString, V
234234
['0x72fdb1c1ddd4c67804f42b93de95cf6a8c51d2d1', 'address'],
235235
],
236236
['-0x01', ['-0x1', 'int256']],
237+
['123c', ['0x123c', 'bytes']],
237238
];
238239

239240
export const toHexInvalidData: [any, string][] = [

0 commit comments

Comments
 (0)
This repository has been archived.