Skip to content

Commit

Permalink
fix: Applied fix using trimPrecedingZeros. (#2499)
Browse files Browse the repository at this point in the history
fix: fix for gasPrice.



fix: Clean up

Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
ebadiere authored May 16, 2024
1 parent 94bf628 commit b63b22b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 18 deletions.
33 changes: 26 additions & 7 deletions packages/relay/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,28 @@ const formatContractResult = (cr: any) => {
return null;
}

const gasPrice =
cr.gas_price === null || cr.gas_price === '0x'
? '0x0'
: isHex(cr.gas_price)
? cr.gas_price
: nanOrNumberTo0x(cr.gas_price);

const commonFields = {
blockHash: toHash32(cr.block_hash),
blockNumber: nullableNumberTo0x(cr.block_number),
from: cr.from.substring(0, 42),
gas: nanOrNumberTo0x(cr.gas_used),
gasPrice: toNullIfEmptyHex(cr.gas_price),
gasPrice,
hash: cr.hash.substring(0, 66),
input: cr.function_parameters,
nonce: nanOrNumberTo0x(cr.nonce),
r: cr.r === null ? null : cr.r.substring(0, 66),
s: cr.s === null ? null : cr.s.substring(0, 66),
r: cr.r === null ? '0x0' : cr.r.substring(0, 66),
s: cr.s === null ? '0x0' : cr.s.substring(0, 66),
to: cr.to?.substring(0, 42),
transactionIndex: nullableNumberTo0x(cr.transaction_index),
type: nullableNumberTo0x(cr.type),
v: cr.type === null ? null : nanOrNumberTo0x(cr.v),
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
v: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.v),
value: nanOrNumberTo0x(cr.amount),
// for legacy EIP155 with tx.chainId=0x0, mirror-node will return a '0x' (EMPTY_HEX) value for contract result's chain_id
// which is incompatibile with certain tools (i.e. foundry). By setting this field, chainId, to undefined, the end jsonrpc
Expand All @@ -178,8 +185,14 @@ const formatContractResult = (cr: any) => {
return new Transaction1559({
...commonFields,
accessList: [],
maxPriorityFeePerGas: toNullIfEmptyHex(cr.max_priority_fee_per_gas),
maxFeePerGas: toNullIfEmptyHex(cr.max_fee_per_gas),
maxPriorityFeePerGas:
cr.max_priority_fee_per_gas === null || cr.max_priority_fee_per_gas === '0x'
? '0x0'
: prepend0x(trimPrecedingZeros(cr.max_priority_fee_per_gas)),
maxFeePerGas:
cr.max_fee_per_gas === null || cr.max_fee_per_gas === '0x'
? '0x0'
: prepend0x(trimPrecedingZeros(cr.max_fee_per_gas)),
}); // eip 1559 fields
case null:
return new Transaction(commonFields); //hapi
Expand Down Expand Up @@ -248,6 +261,11 @@ const isValidEthereumAddress = (address: string): boolean => {
return new RegExp(constants.BASE_HEX_REGEX + '{40}$').test(address);
};

const isHex = (value: string): boolean => {
const hexRegex = /^0x[0-9a-fA-F]+$/;
return hexRegex.test(value);
};

export {
hashNumber,
formatRequestIdMessage,
Expand All @@ -270,4 +288,5 @@ export {
stringToHex,
toHexString,
isValidEthereumAddress,
isHex,
};
2 changes: 2 additions & 0 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ export class EthImpl implements Eth {

const fromAddress = await this.resolveEvmAddress(contractResult.from, requestIdPrefix, [constants.TYPE_ACCOUNT]);
const toAddress = await this.resolveEvmAddress(contractResult.to, requestIdPrefix);
contractResult.chain_id = contractResult.chain_id || this.chain;

return formatContractResult({
...contractResult,
Expand Down Expand Up @@ -2149,6 +2150,7 @@ export class EthImpl implements Eth {
constants.TYPE_ACCOUNT,
]);
contractResult.to = await this.resolveEvmAddress(contractResult.to, requestIdPrefix);
contractResult.chain_id = contractResult.chain_id || this.chain;

transactionArray.push(showDetails ? formatContractResult(contractResult) : contractResult.hash);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/relay/tests/lib/eth/eth_getTransactionByHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,22 @@ describe('@ethGetTransactionByHash eth_getTransactionByHash tests', async functi
});

it('returns correct transaction for existing hash w no sigs', async function () {
const detailedResultsWithNullNullableValues = {
const detailedResultsWithZeroXZeroValues = {
...defaultDetailedContractResultByHash,
r: null,
s: null,
};

const uniqueTxHash = '0x97cad7b827375d12d73af57b6a3f84353645fd31305ea58ff52dda53ec640533';

restMock.onGet(`contracts/results/${uniqueTxHash}`).reply(200, detailedResultsWithNullNullableValues);
restMock.onGet(`contracts/results/${uniqueTxHash}`).reply(200, detailedResultsWithZeroXZeroValues);
const result = await ethImpl.getTransactionByHash(uniqueTxHash);
RelayAssertions.assertTransaction(result, {
...DEFAULT_TRANSACTION,
maxFeePerGas: '0x55',
maxPriorityFeePerGas: '0x43',
r: null,
s: null,
r: '0x0',
s: '0x0',
});
});

Expand Down
52 changes: 45 additions & 7 deletions packages/relay/tests/lib/formatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
weibarHexToTinyBarInt,
isValidEthereumAddress,
trimPrecedingZeros,
isHex,
} from '../../src/formatters';
import constants from '../../src/lib/constants';
import { BigNumber as BN } from 'bignumber.js';
Expand Down Expand Up @@ -202,10 +203,10 @@ describe('Formatters', () => {
expect(formattedResult.chainId).to.equal('0x12a');
expect(formattedResult.from).to.equal('0x05fba803be258049a27b820088bab1cad2058871');
expect(formattedResult.gas).to.equal('0x61a80');
expect(formattedResult.gasPrice).to.equal(null);
expect(formattedResult.gasPrice).to.equal('0x0');
expect(formattedResult.hash).to.equal('0xfc4ab7133197016293d2e14e8cf9c5227b07357e6385184f1cd1cb40d783cfbd');
expect(formattedResult.input).to.equal('0x08090033');
expect(formattedResult.maxPriorityFeePerGas).to.equal(null);
expect(formattedResult.maxPriorityFeePerGas).to.equal('0x0');
expect(formattedResult.maxFeePerGas).to.equal('0x59');
expect(formattedResult.nonce).to.equal('0x2');
expect(formattedResult.r).to.equal('0x2af9d41244c702764ed86c5b9f1a734b075b91c4d9c65e78bc584b0e35181e42');
Expand Down Expand Up @@ -238,14 +239,14 @@ describe('Formatters', () => {
expect(formattedResult.chainId).to.equal('0x12a');
expect(formattedResult.from).to.equal('0x05fba803be258049a27b820088bab1cad2058871');
expect(formattedResult.gas).to.equal('0x0');
expect(formattedResult.gasPrice).to.equal(null);
expect(formattedResult.gasPrice).to.equal('0x0');
expect(formattedResult.hash).to.equal('0xfc4ab7133197016293d2e14e8cf9c5227b07357e6385184f1cd1cb40d783cfbd');
expect(formattedResult.input).to.equal('0x08090033');
expect(formattedResult.maxPriorityFeePerGas).to.equal(null);
expect(formattedResult.maxFeePerGas).to.equal(null);
expect(formattedResult.maxPriorityFeePerGas).to.equal('0x0');
expect(formattedResult.maxFeePerGas).to.equal('0x0');
expect(formattedResult.nonce).to.equal('0x0');
expect(formattedResult.r).to.equal(null);
expect(formattedResult.s).to.equal(null);
expect(formattedResult.r).to.equal('0x0');
expect(formattedResult.s).to.equal('0x0');
expect(formattedResult.to).to.equal('0x0000000000000000000000000000000000000409');
expect(formattedResult.transactionIndex).to.equal(null);
expect(formattedResult.v).to.equal(`0x0`);
Expand Down Expand Up @@ -427,4 +428,41 @@ describe('Formatters', () => {
expect(isValidEthereumAddress(address)).to.equal(false);
});
});
describe('isHex Function', () => {
it('should return true for valid lowercase hexadecimal string', () => {
expect(isHex('0x1a3f')).to.be.true;
});

it('should return true for valid uppercase hexadecimal string', () => {
expect(isHex('0xABC')).to.be.true;
});

it('should return true for mixed-case hexadecimal string', () => {
expect(isHex('0xAbC123')).to.be.true;
});

it('should return false for string without 0x prefix', () => {
expect(isHex('1a3f')).to.be.false;
});

it('should return false for string with invalid characters', () => {
expect(isHex('0x1g3f')).to.be.false;
});

it('should return false for string with only 0x prefix', () => {
expect(isHex('0x')).to.be.false;
});

it('should return false for empty string', () => {
expect(isHex('')).to.be.false;
});

it('should return false for string with spaces', () => {
expect(isHex('0x 1a3f')).to.be.false;
});

it('should return true for a known gasPrice', () => {
expect(isHex('0x58')).to.be.true;
});
});
});

0 comments on commit b63b22b

Please sign in to comment.