Skip to content

Commit ca044f1

Browse files
authored
fix: added null check for nullable values in formatContractResult() (#3210)
* fix: added null check for nullable values in formatContractResult() Signed-off-by: Logan Nguyen <[email protected]> * fix: fixed unit tests Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
1 parent 0e31b34 commit ca044f1

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

packages/relay/src/formatters.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ const formatContractResult = (cr: any) => {
166166
const commonFields = {
167167
blockHash: toHash32(cr.block_hash),
168168
blockNumber: nullableNumberTo0x(cr.block_number),
169-
from: cr.from.substring(0, 42),
169+
from: cr.from?.substring(0, 42) || null,
170170
gas: nanOrNumberTo0x(cr.gas_used),
171171
gasPrice,
172-
hash: cr.hash.substring(0, 66),
172+
hash: cr.hash?.substring(0, 66) || null,
173173
input: cr.function_parameters,
174174
nonce: nanOrNumberTo0x(cr.nonce),
175175
r: cr.r === null ? '0x0' : stripLeadingZeroForSignatures(cr.r.substring(0, 66)),
176176
s: cr.s === null ? '0x0' : stripLeadingZeroForSignatures(cr.s.substring(0, 66)),
177-
to: cr.to?.substring(0, 42),
177+
to: cr.to?.substring(0, 42) || null,
178178
transactionIndex: nullableNumberTo0x(cr.transaction_index),
179179
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
180180
v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v),

packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio
120120
verifyAggregatedInfo(result);
121121
});
122122

123-
it('eth_getTransactionByBlockHashAndIndex should throw for internal error', async function () {
123+
it('eth_getTransactionByBlockHashAndIndex should NOT throw for internal error if `from` field is null', async function () {
124124
const randomBlock = {
125125
hash: '0x5f827a801c579c84eca738827b65612b28ed425b7578bfdd10177e24fc3db8d4b1a7f3d56d83c39b950cc5e4d175dd64',
126126
count: 9,
@@ -131,16 +131,9 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio
131131
.onGet(contractResultsByHashByIndexURL(randomBlock.hash, randomBlock.count))
132132
.reply(200, defaultContractResultsWithNullableFrom);
133133

134-
const args = [randomBlock.hash, numberTo0x(randomBlock.count), requestDetails];
135-
const errMessage = "Cannot read properties of null (reading 'substring')";
136-
137-
await RelayAssertions.assertRejection(
138-
predefined.INTERNAL_ERROR(errMessage),
139-
ethImpl.getTransactionByBlockHashAndIndex,
140-
true,
141-
ethImpl,
142-
args,
143-
);
134+
const result = await ethImpl.getTransactionByBlockHashAndIndex(randomBlock.hash, randomBlock.count, requestDetails);
135+
expect(result).to.not.be.null;
136+
expect(result.from).to.be.null;
144137
});
145138

146139
it('eth_getTransactionByBlockHashAndIndex with no contract result match', async function () {

packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct
159159
expect(result).to.equal(null);
160160
});
161161

162-
it('eth_getTransactionByBlockNumberAndIndex should throw for internal error', async function () {
162+
it('eth_getTransactionByBlockNumberAndIndex should NOT throw for internal error if `from` field is null', async function () {
163163
const defaultContractResultsWithNullableFrom = _.cloneDeep(defaultContractResults);
164164
defaultContractResultsWithNullableFrom.results[0].from = null;
165165
const randomBlock = {
@@ -170,16 +170,14 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct
170170
.onGet(contractResultsByNumberByIndexURL(randomBlock.number, randomBlock.count))
171171
.reply(200, defaultContractResultsWithNullableFrom);
172172

173-
const args = [numberTo0x(randomBlock.number), numberTo0x(randomBlock.count), requestDetails];
174-
const errMessage = "Cannot read properties of null (reading 'substring')";
175-
176-
await RelayAssertions.assertRejection(
177-
predefined.INTERNAL_ERROR(errMessage),
178-
ethImpl.getTransactionByBlockNumberAndIndex,
179-
true,
180-
ethImpl,
181-
args,
173+
const result = await ethImpl.getTransactionByBlockNumberAndIndex(
174+
numberTo0x(randomBlock.number),
175+
numberTo0x(randomBlock.count),
176+
requestDetails,
182177
);
178+
179+
expect(result).to.not.be.null;
180+
expect(result.from).to.be.null;
183181
});
184182

185183
it('eth_getTransactionByBlockNumberAndIndex with no contract results', async function () {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ describe('Formatters', () => {
340340
const formattedResult = formatContractResult({ ...contractResult, type: undefined });
341341
expect(formattedResult).to.be.null;
342342
});
343+
344+
it('Should return formatted result even when `from` and `hash` fields are null', () => {
345+
const formattedResult: any = formatContractResult({ ...contractResult, from: null, hash: null, to: null });
346+
347+
expect(formattedResult).to.not.be.undefined;
348+
expect(formattedResult.from).to.be.null;
349+
expect(formattedResult.hash).to.be.null;
350+
expect(formattedResult.to).to.be.null;
351+
});
343352
});
344353

345354
describe('prepend0x', () => {

0 commit comments

Comments
 (0)