Skip to content

Commit 57fb14d

Browse files
authored
fix: trimmed preceding zeros for gas value (#2174) (#2180)
* fix: trimmed preceding zeros for gas value Signed-off-by: Logan Nguyen <[email protected]> * test: added unit tests for trimPrecedingZeros Signed-off-by: Logan Nguyen <[email protected]> * test: added more cases to trimPrecedingZeros test suite Signed-off-by: Logan Nguyen <[email protected]> * fix: reverted condition to check contractCallResponse for estimateGas() Signed-off-by: Logan Nguyen <[email protected]> * fix: fixed unit test Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
1 parent 49b4c16 commit 57fb14d

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

packages/relay/src/formatters.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ const prepend0x = (input: string): string => {
181181
return input.startsWith(EMPTY_HEX) ? input : EMPTY_HEX + input;
182182
};
183183

184+
const trimPrecedingZeros = (input: string) => {
185+
return parseInt(input, 16).toString(16);
186+
};
187+
184188
const numberTo0x = (input: number | BigNumber | bigint): string => {
185189
return EMPTY_HEX + input.toString(16);
186190
};
@@ -251,6 +255,7 @@ export {
251255
toNullableBigNumber,
252256
toNullIfEmptyHex,
253257
generateRandomHex,
258+
trimPrecedingZeros,
254259
weibarHexToTinyBarInt,
255260
stringToHex,
256261
toHexString,

packages/relay/src/lib/eth.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
toHash32,
4040
toNullableBigNumber,
4141
weibarHexToTinyBarInt,
42+
trimPrecedingZeros,
4243
} from '../formatters';
4344
import crypto from 'crypto';
4445
import HAPIService from './services/hapiService/hapiService';
@@ -573,8 +574,9 @@ export class EthImpl implements Eth {
573574
},
574575
requestIdPrefix,
575576
);
577+
576578
if (contractCallResponse?.result) {
577-
gas = prepend0x(contractCallResponse.result);
579+
gas = prepend0x(trimPrecedingZeros(contractCallResponse.result));
578580
}
579581
} catch (e: any) {
580582
this.logger.error(

packages/relay/tests/lib/formatters.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
toNullIfEmptyHex,
3636
weibarHexToTinyBarInt,
3737
isValidEthereumAddress,
38+
trimPrecedingZeros,
3839
} from '../../src/formatters';
3940
import constants from '../../src/lib/constants';
4041
import { BigNumber as BN } from 'bignumber.js';
@@ -262,6 +263,24 @@ describe('Formatters', () => {
262263
});
263264
});
264265

266+
describe('trimPrecedingZeros', () => {
267+
it('should trim all the unnecessary preceding 0s in a hex value', () => {
268+
expect(trimPrecedingZeros('0x0000000034')).to.eq('34');
269+
expect(trimPrecedingZeros('0x0000039000')).to.eq('39000');
270+
expect('0x' + trimPrecedingZeros('0x0')).to.eq('0x0');
271+
expect('0x' + trimPrecedingZeros('0x00000000603')).to.eq('0x603');
272+
expect('0x' + trimPrecedingZeros('0x00000300042')).to.eq('0x300042');
273+
expect('0x' + trimPrecedingZeros('0x00012000000')).to.eq('0x12000000');
274+
});
275+
276+
it('should return NaN if inputs are invalid number', () => {
277+
expect(trimPrecedingZeros('')).to.eq('NaN');
278+
expect(trimPrecedingZeros('0x')).to.eq('NaN');
279+
expect(trimPrecedingZeros('Relay')).to.eq('NaN');
280+
expect(trimPrecedingZeros('Hedera')).to.eq('NaN');
281+
});
282+
});
283+
265284
describe('numberTo0x', () => {
266285
it('should convert to hex a number type', () => {
267286
expect(numberTo0x(1009)).to.equal('0x3f1');

packages/server/tests/acceptance/rpc_batch2.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('@api-batch-2 RPC Server Acceptance Tests', function () {
6363
const ONE_WEIBAR = Utils.add0xPrefix(Utils.toHex(ethers.parseUnits('1', 18)));
6464

6565
const BASIC_CONTRACT_PING_CALL_DATA = '0x5c36b186';
66-
const PING_CALL_ESTIMATED_GAS = '0x0000000000006122';
66+
const PING_CALL_ESTIMATED_GAS = '0x6122';
6767
const EXCHANGE_RATE_FILE_ID = '0.0.112';
6868
const EXCHANGE_RATE_FILE_CONTENT_DEFAULT = '0a1008b0ea0110f9bb1b1a0608f0cccf9306121008b0ea0110e9c81a1a060880e9cf9306';
6969
const FEE_SCHEDULE_FILE_ID = '0.0.111';

0 commit comments

Comments
 (0)