Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra changes to make possibility add eip4844 type as a plugin #7000

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
#### web3-eth

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)
- `getTransactionFromOrToAttr`, `waitForTransactionReceipt`, `trySendTransaction`, `SendTxHelper` was exported (#7000)

#### web3-eth-contract

Expand All @@ -2473,8 +2474,23 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
- Added `signature` to type `AbiFunctionFragment` (#6922)
- update type `Withdrawals`, `block` and `BlockHeaderOutput` to include properties of eip 4844, 4895, 4788 (#6933)

#### web3-utils


### Fixed

#### web3-utils


#### web3-validator

- The JSON schema conversion process now correctly assigns an id when the `abi.name` is not available, for example, in the case of public mappings. (#6981)

### Changed

#### web3-eth

- Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000)

#### web3-rpc-methods

- Change `estimateGas` method to add possibility pass Transaction type (#7000)
5 changes: 5 additions & 0 deletions packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,8 @@ Documentation:
### Added

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)
- `getTransactionFromOrToAttr`, `waitForTransactionReceipt`, `trySendTransaction`, `SendTxHelper` was exported (#7000)

### Changed

- Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000)
5 changes: 4 additions & 1 deletion packages/web3-eth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export * from './utils/format_transaction.js';
export * from './utils/prepare_transaction_for_signing.js';
export * from './web3_subscriptions.js';
export { detectTransactionType } from './utils/detect_transaction_type.js';
export { transactionBuilder } from './utils/transaction_builder.js';
export { transactionBuilder, getTransactionFromOrToAttr } from './utils/transaction_builder.js';
export { waitForTransactionReceipt } from './utils/wait_for_transaction_receipt.js';
export { trySendTransaction } from './utils/try_send_transaction.js';
export { SendTxHelper } from './utils/send_tx_helper.js';

export default Web3Eth;
5 changes: 4 additions & 1 deletion packages/web3-eth/src/utils/send_tx_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
ContractAbiWithSignature,
} from 'web3-types';
import { Web3Context, Web3EventEmitter, Web3PromiEvent } from 'web3-core';
import { isNullish } from 'web3-validator';
import { isNullish, JsonSchema } from 'web3-validator';
import {
ContractExecutionError,
InvalidResponseError,
Expand Down Expand Up @@ -257,9 +257,11 @@ export class SendTxHelper<
public emitConfirmation({
receipt,
transactionHash,
customTransactionReceiptSchema,
}: {
receipt: ResolveType;
transactionHash: TransactionHash;
customTransactionReceiptSchema?: JsonSchema;
}) {
if (this.promiEvent.listenerCount('confirmation') > 0) {
watchTransactionForConfirmations<
Expand All @@ -272,6 +274,7 @@ export class SendTxHelper<
receipt as unknown as TransactionReceipt,
transactionHash,
this.returnFormat,
customTransactionReceiptSchema,
);
}
}
Expand Down
35 changes: 22 additions & 13 deletions packages/web3-eth/src/utils/wait_for_transaction_receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,30 @@
web3Context: Web3Context<EthExecutionAPI>,
transactionHash: Bytes,
returnFormat: ReturnFormat,
customGetTransactionReceipt?: (
web3Context: Web3Context<EthExecutionAPI>,
transactionHash: Bytes,
returnFormat: ReturnFormat,
) => Promise<TransactionReceipt>,
): Promise<TransactionReceipt> {

const pollingInterval =
web3Context.transactionReceiptPollingInterval ?? web3Context.transactionPollingInterval;

const [awaitableTransactionReceipt, IntervalId] = pollTillDefinedAndReturnIntervalId(async () => {
try {
return getTransactionReceipt(web3Context, transactionHash, returnFormat);
} catch (error) {
console.warn('An error happen while trying to get the transaction receipt', error);
return undefined;
}
}, pollingInterval);
const [awaitableTransactionReceipt, IntervalId] = pollTillDefinedAndReturnIntervalId(
async () => {
try {
return (customGetTransactionReceipt ?? getTransactionReceipt)(
web3Context,
transactionHash,
returnFormat,
);
} catch (error) {
console.warn('An error happen while trying to get the transaction receipt', error);
return undefined;

Check warning on line 52 in packages/web3-eth/src/utils/wait_for_transaction_receipt.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth/src/utils/wait_for_transaction_receipt.ts#L51-L52

Added lines #L51 - L52 were not covered by tests
}
},
pollingInterval,
);

const [timeoutId, rejectOnTimeout] = rejectIfTimeout(
web3Context.transactionPollingTimeout,
Expand All @@ -65,10 +76,8 @@
rejectOnBlockTimeout, // this will throw an error on Transaction Block Timeout
]);
} finally {
if(timeoutId)
clearTimeout(timeoutId);
if(IntervalId)
clearInterval(IntervalId);
if (timeoutId) clearTimeout(timeoutId);
if (IntervalId) clearInterval(IntervalId);
blockTimeoutResourceCleaner.clean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { format, numberToHex } from 'web3-utils';
import { ethRpcMethods } from 'web3-rpc-methods';

import { DataFormat } from 'web3-types';
import { JsonSchema } from 'web3-validator';
import { SendSignedTransactionEvents, SendTransactionEvents } from '../types.js';
import { transactionReceiptSchema } from '../schemas.js';

Expand All @@ -30,6 +31,7 @@ export type Web3PromiEventEventTypeBase<ReturnFormat extends DataFormat> =
export type WaitProps<ReturnFormat extends DataFormat, ResolveType = TransactionReceipt> = {
web3Context: Web3Context<EthExecutionAPI>;
transactionReceipt: TransactionReceipt;
customTransactionReceiptSchema?: JsonSchema;
transactionPromiEvent: Web3PromiEvent<ResolveType, Web3PromiEventEventTypeBase<ReturnFormat>>;
returnFormat: ReturnFormat;
};
Expand All @@ -46,6 +48,7 @@ export const watchTransactionByPolling = <
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
}: WaitProps<ReturnFormat, ResolveType>) => {
// Having a transactionReceipt means that the transaction has already been included
Expand All @@ -67,7 +70,11 @@ export const watchTransactionByPolling = <

transactionPromiEvent.emit('confirmation', {
confirmations: format({ format: 'uint' }, confirmations, returnFormat),
receipt: format(transactionReceiptSchema, transactionReceipt, returnFormat),
receipt: format(
customTransactionReceiptSchema ?? transactionReceiptSchema,
transactionReceipt,
returnFormat,
),
latestBlockHash: format(
{ format: 'bytes32' },
nextBlock.hash as Bytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import { NewHeadsSubscription } from '../web3_subscriptions.js';
import { transactionReceiptSchema } from '../schemas.js';
import { WaitProps, watchTransactionByPolling } from './watch_transaction_by_polling.js';

/**
* This function watches a Transaction by subscribing to new heads.
* It is used by `watchTransactionForConfirmations`, in case the provider supports subscription.
Expand All @@ -33,6 +32,7 @@
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
}: WaitProps<ReturnFormat, ResolveType>) => {
// The following variable will stay true except if the data arrived,
Expand Down Expand Up @@ -66,7 +66,11 @@
confirmations as Numbers,
returnFormat,
),
receipt: format(transactionReceiptSchema, transactionReceipt, returnFormat),
receipt: format(
customTransactionReceiptSchema ?? transactionReceiptSchema,

Check warning on line 70 in packages/web3-eth/src/utils/watch_transaction_by_subscription.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth/src/utils/watch_transaction_by_subscription.ts#L70

Added line #L70 was not covered by tests
transactionReceipt,
returnFormat,
),
latestBlockHash: format(
{ format: 'bytes32' },
newBlockHeader.parentHash as Bytes,
Expand All @@ -85,6 +89,7 @@
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
});
});
Expand All @@ -94,6 +99,7 @@
watchTransactionByPolling({
web3Context,
transactionReceipt,
customTransactionReceiptSchema,
transactionPromiEvent,
returnFormat,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
import { Bytes, EthExecutionAPI, Web3BaseProvider, TransactionReceipt } from 'web3-types';
import { Web3Context, Web3PromiEvent } from 'web3-core';
import { format } from 'web3-utils';
import { isNullish } from 'web3-validator';
import { isNullish, JsonSchema } from 'web3-validator';

import {
TransactionMissingReceiptOrBlockHashError,
Expand All @@ -41,6 +41,7 @@ export function watchTransactionForConfirmations<
transactionReceipt: TransactionReceipt,
transactionHash: Bytes,
returnFormat: ReturnFormat,
customTransactionReceiptSchema?: JsonSchema,
) {
if (isNullish(transactionReceipt) || isNullish(transactionReceipt.blockHash))
throw new TransactionMissingReceiptOrBlockHashError({
Expand All @@ -55,7 +56,11 @@ export function watchTransactionForConfirmations<
// As we have the receipt, it's the first confirmation that tx is accepted.
transactionPromiEvent.emit('confirmation', {
confirmations: format({ format: 'uint' }, 1, returnFormat),
receipt: format(transactionReceiptSchema, transactionReceipt, returnFormat),
receipt: format(
customTransactionReceiptSchema ?? transactionReceiptSchema,
transactionReceipt,
returnFormat,
),
latestBlockHash: format({ format: 'bytes32' }, transactionReceipt.blockHash, returnFormat),
});

Expand All @@ -66,13 +71,15 @@ export function watchTransactionForConfirmations<
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
});
} else {
watchTransactionByPolling({
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('sendTransaction', () => {
WaitForTransactionReceipt.waitForTransactionReceipt as jest.Mock
).mockResolvedValueOnce(expectedTransactionReceipt);

const checkRevertBeforeSendingSpy = jest.fn().mockImplementation((transaction) => {
const checkRevertBeforeSendingSpy = jest.fn().mockImplementation(transaction => {
expect(transaction).toBeDefined();

// verify signature part is removed before sending to revert check function
Expand All @@ -78,7 +78,6 @@ describe('sendTransaction', () => {
);

expect(checkRevertBeforeSendingSpy).toHaveBeenCalledTimes(1);

},
);

Expand Down Expand Up @@ -300,6 +299,7 @@ describe('sendTransaction', () => {
formattedTransactionReceipt,
expectedTransactionHash,
DEFAULT_RETURN_FORMAT,
undefined,
);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('sendTransaction', () => {
formattedTransactionReceipt,
expectedTransactionHash,
DEFAULT_RETURN_FORMAT,
undefined,
);
},
);
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-rpc-methods/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,8 @@ Documentation:

- Added `getMaxPriorityFeePerGas` method (#6748)

## [Unreleased]
## [Unreleased]

### Changed

- Change `estimateGas` method to add possibility pass Transaction type (#7000)
4 changes: 2 additions & 2 deletions packages/web3-rpc-methods/src/eth_rpc_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ export async function call(
}

// TODO Not sure how to best validate Partial<TransactionWithSender>
export async function estimateGas(
export async function estimateGas<TransactionType = TransactionWithSenderAPI>(
requestManager: Web3RequestManager,
transaction: Partial<TransactionWithSenderAPI>,
transaction: Partial<TransactionType>,
blockNumber: BlockNumberOrTag,
) {
validator.validate(['blockNumberOrTag'], [blockNumber]);
Expand Down
Loading