From b375f72004bdf992ddd45d62f8ea05c0e3a18933 Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Mon, 4 Nov 2024 23:55:38 -0500 Subject: [PATCH] chore: cherry picked #3120 to release/0.59 (#3211) fix: added null check for nullable values in formatContractResult() (#3210) * fix: added null check for nullable values in formatContractResult() * fix: fixed unit tests --------- Signed-off-by: Logan Nguyen --- packages/relay/src/formatters.ts | 6 +++--- ...h_getTransactionByBlockHashAndIndex.spec.ts | 15 ++++----------- ...getTransactionByBlockNumberAndIndex.spec.ts | 18 ++++++++---------- packages/relay/tests/lib/formatters.spec.ts | 9 +++++++++ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/relay/src/formatters.ts b/packages/relay/src/formatters.ts index 4142d0db8..396dca90f 100644 --- a/packages/relay/src/formatters.ts +++ b/packages/relay/src/formatters.ts @@ -166,15 +166,15 @@ const formatContractResult = (cr: any) => { const commonFields = { blockHash: toHash32(cr.block_hash), blockNumber: nullableNumberTo0x(cr.block_number), - from: cr.from.substring(0, 42), + from: cr.from?.substring(0, 42) || null, gas: nanOrNumberTo0x(cr.gas_used), gasPrice, - hash: cr.hash.substring(0, 66), + hash: cr.hash?.substring(0, 66) || null, input: cr.function_parameters, nonce: nanOrNumberTo0x(cr.nonce), r: cr.r === null ? '0x0' : stripLeadingZeroForSignatures(cr.r.substring(0, 66)), s: cr.s === null ? '0x0' : stripLeadingZeroForSignatures(cr.s.substring(0, 66)), - to: cr.to?.substring(0, 42), + to: cr.to?.substring(0, 42) || null, transactionIndex: nullableNumberTo0x(cr.transaction_index), type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type), v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v), diff --git a/packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts b/packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts index f42903aa4..dd3f6444d 100644 --- a/packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts +++ b/packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts @@ -120,7 +120,7 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio verifyAggregatedInfo(result); }); - it('eth_getTransactionByBlockHashAndIndex should throw for internal error', async function () { + it('eth_getTransactionByBlockHashAndIndex should NOT throw for internal error if `from` field is null', async function () { const randomBlock = { hash: '0x5f827a801c579c84eca738827b65612b28ed425b7578bfdd10177e24fc3db8d4b1a7f3d56d83c39b950cc5e4d175dd64', count: 9, @@ -131,16 +131,9 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio .onGet(contractResultsByHashByIndexURL(randomBlock.hash, randomBlock.count)) .reply(200, defaultContractResultsWithNullableFrom); - const args = [randomBlock.hash, numberTo0x(randomBlock.count), requestDetails]; - const errMessage = "Cannot read properties of null (reading 'substring')"; - - await RelayAssertions.assertRejection( - predefined.INTERNAL_ERROR(errMessage), - ethImpl.getTransactionByBlockHashAndIndex, - true, - ethImpl, - args, - ); + const result = await ethImpl.getTransactionByBlockHashAndIndex(randomBlock.hash, randomBlock.count, requestDetails); + expect(result).to.not.be.null; + expect(result.from).to.be.null; }); it('eth_getTransactionByBlockHashAndIndex with no contract result match', async function () { diff --git a/packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts b/packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts index e2c335117..ea613ea40 100644 --- a/packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts +++ b/packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts @@ -159,7 +159,7 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct expect(result).to.equal(null); }); - it('eth_getTransactionByBlockNumberAndIndex should throw for internal error', async function () { + it('eth_getTransactionByBlockNumberAndIndex should NOT throw for internal error if `from` field is null', async function () { const defaultContractResultsWithNullableFrom = _.cloneDeep(defaultContractResults); defaultContractResultsWithNullableFrom.results[0].from = null; const randomBlock = { @@ -170,16 +170,14 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct .onGet(contractResultsByNumberByIndexURL(randomBlock.number, randomBlock.count)) .reply(200, defaultContractResultsWithNullableFrom); - const args = [numberTo0x(randomBlock.number), numberTo0x(randomBlock.count), requestDetails]; - const errMessage = "Cannot read properties of null (reading 'substring')"; - - await RelayAssertions.assertRejection( - predefined.INTERNAL_ERROR(errMessage), - ethImpl.getTransactionByBlockNumberAndIndex, - true, - ethImpl, - args, + const result = await ethImpl.getTransactionByBlockNumberAndIndex( + numberTo0x(randomBlock.number), + numberTo0x(randomBlock.count), + requestDetails, ); + + expect(result).to.not.be.null; + expect(result.from).to.be.null; }); it('eth_getTransactionByBlockNumberAndIndex with no contract results', async function () { diff --git a/packages/relay/tests/lib/formatters.spec.ts b/packages/relay/tests/lib/formatters.spec.ts index 5aed252af..91643a1ca 100644 --- a/packages/relay/tests/lib/formatters.spec.ts +++ b/packages/relay/tests/lib/formatters.spec.ts @@ -340,6 +340,15 @@ describe('Formatters', () => { const formattedResult = formatContractResult({ ...contractResult, type: undefined }); expect(formattedResult).to.be.null; }); + + it('Should return formatted result even when `from` and `hash` fields are null', () => { + const formattedResult: any = formatContractResult({ ...contractResult, from: null, hash: null, to: null }); + + expect(formattedResult).to.not.be.undefined; + expect(formattedResult.from).to.be.null; + expect(formattedResult.hash).to.be.null; + expect(formattedResult.to).to.be.null; + }); }); describe('prepend0x', () => {