Skip to content

Commit

Permalink
Add extra changes to make possibility add eip4844 type as a plugin (#…
Browse files Browse the repository at this point in the history
…7000)

* export utils

* add customJsonSchema

* fix tests

* add changelog
  • Loading branch information
avkos authored May 1, 2024
1 parent ebbbf1e commit 7a470c9
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 26 deletions.
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 @@ export async function waitForTransactionReceipt<ReturnFormat extends DataFormat>
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;
}
},
pollingInterval,
);

const [timeoutId, rejectOnTimeout] = rejectIfTimeout(
web3Context.transactionPollingTimeout,
Expand All @@ -65,10 +76,8 @@ export async function waitForTransactionReceipt<ReturnFormat extends DataFormat>
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();
}
}
9 changes: 8 additions & 1 deletion packages/web3-eth/src/utils/watch_transaction_by_polling.ts
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
10 changes: 8 additions & 2 deletions packages/web3-eth/src/utils/watch_transaction_by_subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { DataFormat } from 'web3-types';
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 @@ export const watchTransactionBySubscription = <
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 @@ export const watchTransactionBySubscription = <
confirmations as Numbers,
returnFormat,
),
receipt: format(transactionReceiptSchema, transactionReceipt, returnFormat),
receipt: format(
customTransactionReceiptSchema ?? transactionReceiptSchema,
transactionReceipt,
returnFormat,
),
latestBlockHash: format(
{ format: 'bytes32' },
newBlockHeader.parentHash as Bytes,
Expand All @@ -85,6 +89,7 @@ export const watchTransactionBySubscription = <
web3Context,
transactionReceipt,
transactionPromiEvent,
customTransactionReceiptSchema,
returnFormat,
});
});
Expand All @@ -94,6 +99,7 @@ export const watchTransactionBySubscription = <
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

1 comment on commit 7a470c9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7a470c9 Previous: 6c075db Ratio
processingTx 9160 ops/sec (±3.49%) 9301 ops/sec (±4.81%) 1.02
processingContractDeploy 38110 ops/sec (±6.70%) 39129 ops/sec (±7.62%) 1.03
processingContractMethodSend 18031 ops/sec (±8.26%) 19443 ops/sec (±5.19%) 1.08
processingContractMethodCall 37459 ops/sec (±6.11%) 38971 ops/sec (±6.34%) 1.04
abiEncode 41509 ops/sec (±7.27%) 44252 ops/sec (±6.92%) 1.07
abiDecode 28823 ops/sec (±8.43%) 30419 ops/sec (±8.89%) 1.06
sign 1543 ops/sec (±1.04%) 1656 ops/sec (±4.08%) 1.07
verify 366 ops/sec (±0.66%) 373 ops/sec (±0.78%) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.