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

Commit 4ba0978

Browse files
nikoulaispacesailor24
authored andcommitted
Incompatible return types for 4.x implementations (#5205)
* Throw error for not found tx in getTransaction * Fix failing tests * Move mock values to fixtures * Throw error for not found tx in getTransactionReceipt * Create dedicated error * Create dedicated error code
1 parent 6f529c7 commit 4ba0978

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

packages/web3-errors/src/error_codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const ERR_TX_RECEIPT_MISSING_BLOCK_NUMBER = 428;
7373

7474
export const ERR_TX_LOCAL_WALLET_NOT_AVAILABLE = 429;
7575

76+
export const ERR_TX_NOT_FOUND = 430;
7677
// Connection error codes
7778
export const ERR_CONN = 500;
7879
export const ERR_CONN_INVALID = 501;

packages/web3-errors/src/errors/transaction_errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ERR_TX_REVERT_INSTRUCTION,
2727
ERR_TX_REVERT_TRANSACTION,
2828
ERR_TX_REVERT_WITHOUT_REASON,
29+
ERR_TX_NOT_FOUND,
2930
} from '../error_codes';
3031
import { ReceiptInfo } from '../types';
3132
import { Web3Error } from '../web3_error_base';
@@ -125,3 +126,9 @@ export class UndefinedRawTransactionError extends TransactionError {
125126
this.code = ERR_RAW_TX_UNDEFINED;
126127
}
127128
}
129+
export class TransactionNotFound extends TransactionError {
130+
public constructor() {
131+
super('Transaction not found');
132+
this.code = ERR_TX_NOT_FOUND;
133+
}
134+
}

packages/web3-eth/src/web3_eth.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Web3Context,
2525
Web3ContextInitOptions,
2626
} from 'web3-core';
27+
import { TransactionNotFound } from 'web3-errors';
2728
import {
2829
Address,
2930
Bytes,
@@ -207,7 +208,15 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
207208
transactionHash: Bytes,
208209
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
209210
) {
210-
return rpcMethodsWrappers.getTransaction(this, transactionHash, returnFormat);
211+
const response = await rpcMethodsWrappers.getTransaction(
212+
this,
213+
transactionHash,
214+
returnFormat,
215+
);
216+
217+
if (!response) throw new TransactionNotFound();
218+
219+
return response;
211220
}
212221

213222
public async getPendingTransactions<
@@ -234,7 +243,15 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
234243
public async getTransactionReceipt<
235244
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT,
236245
>(transactionHash: Bytes, returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat) {
237-
return rpcMethodsWrappers.getTransactionReceipt(this, transactionHash, returnFormat);
246+
const response = await rpcMethodsWrappers.getTransactionReceipt(
247+
this,
248+
transactionHash,
249+
returnFormat,
250+
);
251+
252+
if (!response) throw new TransactionNotFound();
253+
254+
return response;
238255
}
239256

240257
public async getTransactionCount<

packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
Uint256,
2929
} from 'web3-utils';
3030
import { transactionWithSender } from './rpc_methods_wrappers';
31+
import { ReceiptInfo } from '../../src/types';
3132

3233
/**
3334
* Array consists of:
@@ -1835,3 +1836,39 @@ export const getProofValidData: [
18351836
],
18361837
],
18371838
];
1839+
1840+
export const tx = {
1841+
blockHash: '0xb3a667f84f58c90ab87476073e06c5d1186a0f0b0b69aa3033bfe0e4df264350',
1842+
blockNumber: '123',
1843+
from: '0x01ada9d3470eb9eb3875d9e7948c674804ca43ae',
1844+
gas: '21000',
1845+
gasPrice: '10000',
1846+
hash: '0x84f44dffc3cd90a1b66ad0219a97680308e5e7a77299fbf1e2ebb572cf02cc2d',
1847+
input: '0x',
1848+
nonce: '61',
1849+
to: '0x0000000000000000000000000000000000000000',
1850+
transactionIndex: '0',
1851+
value: '1',
1852+
type: '0x01',
1853+
v: '2710',
1854+
r: '0xbefb00433ef79b2609dc560c28963c2e954370792671f71ab99665b8807b7feb',
1855+
s: '0x6bebe5e2c9f839d3ce0264dc5c7cc521f902e86705c69f5fddffaa3de5aac6d3',
1856+
};
1857+
1858+
export const txReceipt: ReceiptInfo = {
1859+
blockHash: '0xb3a667f84f58c90ab87476073e06c5d1186a0f0b0b69aa3033bfe0e4df264350',
1860+
blockNumber: BigInt(123),
1861+
cumulativeGasUsed: BigInt(21000),
1862+
effectiveGasPrice: BigInt(10000),
1863+
from: '0x01ada9d3470eb9eb3875d9e7948c674804ca43ae',
1864+
gasUsed: BigInt(21000),
1865+
logs: [],
1866+
logsBloom:
1867+
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
1868+
status: BigInt(1),
1869+
to: '0x0000000000000000000000000000000000000000',
1870+
transactionHash: '0x84f44dffc3cd90a1b66ad0219a97680308e5e7a77299fbf1e2ebb572cf02cc2d',
1871+
transactionIndex: BigInt(0),
1872+
type: BigInt(0),
1873+
root: '',
1874+
};

packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ import {
4949
sendSignedTransactionValidData,
5050
signValidData,
5151
submitWorkValidData,
52+
tx,
53+
txReceipt,
5254
} from '../fixtures/web3_eth_methods_with_parameters';
5355

5456
jest.mock('../../src/rpc_methods');
5557
jest.mock('../../src/rpc_method_wrappers');
58+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
59+
jest.spyOn(rpcMethodWrappers, 'getTransaction').mockResolvedValue(tx);
60+
jest.spyOn(rpcMethodWrappers, 'getTransactionReceipt').mockResolvedValue(txReceipt);
5661

5762
describe('web3_eth_methods_with_parameters', () => {
5863
let web3Eth: Web3Eth;

0 commit comments

Comments
 (0)