From fc2468fa534413a6e24c3c05f33329b301d83950 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 30 Aug 2024 09:21:23 +0200 Subject: [PATCH 01/16] feat: add custom transaction schema to formatTransaction --- packages/web3-core/src/web3_config.ts | 10 ++-- packages/web3-core/src/web3_context.ts | 27 +++++++--- .../web3-core/src/web3_request_manager.ts | 9 ++++ .../src/rpc_method_wrappers.ts | 8 ++- packages/web3-eth/src/rpc_method_wrappers.ts | 51 +++++++++++++------ .../src/utils/decode_signed_transaction.ts | 11 +++- .../utils/prepare_transaction_for_signing.ts | 13 +++-- packages/web3-eth/src/validation.ts | 16 +++++- .../test/unit/format_transaction.test.ts | 4 ++ 9 files changed, 112 insertions(+), 37 deletions(-) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index e647e05a9d5..bf944dc9ba5 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -25,6 +25,7 @@ import { } from 'web3-types'; import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; import { isNullish, toHex } from 'web3-utils'; +import { ValidationSchemaInput } from 'web3-validator'; import { TransactionTypeParser } from './types.js'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context.js'; @@ -59,6 +60,7 @@ export interface Web3ConfigOptions { }; transactionBuilder?: TransactionBuilder; transactionTypeParser?: TransactionTypeParser; + customTransactionSchema?: ValidationSchemaInput; defaultReturnFormat: DataFormat; } @@ -101,6 +103,7 @@ export abstract class Web3Config }, transactionBuilder: undefined, transactionTypeParser: undefined, + customTransactionSchema: undefined, defaultReturnFormat: DEFAULT_RETURN_FORMAT, }; @@ -115,10 +118,11 @@ export abstract class Web3Config for (const key of keys) { this._triggerConfigChange(key, options[key]); - if(!isNullish(options[key]) && + if ( + !isNullish(options[key]) && typeof options[key] === 'number' && - key === 'maxListenersWarningThreshold' ) - { + key === 'maxListenersWarningThreshold' + ) { // additionally set in event emitter this.setMaxListenerWarningThreshold(Number(options[key])); } diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 867f10f2350..d030b375b9a 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -18,8 +18,15 @@ along with web3.js. If not, see . import { ExistingPluginNamespaceError } from 'web3-errors'; import { EthExecutionAPI, - HexString, Numbers, SupportedProviders, Transaction, Web3AccountProvider, Web3APISpec, Web3BaseProvider, Web3BaseWallet, - Web3BaseWalletAccount + HexString, + Numbers, + SupportedProviders, + Transaction, + Web3AccountProvider, + Web3APISpec, + Web3BaseProvider, + Web3BaseWallet, + Web3BaseWalletAccount, } from 'web3-types'; import { isNullish } from 'web3-utils'; import { BaseTransaction, TransactionFactory } from 'web3-eth-accounts'; @@ -112,6 +119,7 @@ export class Web3Context< isSupportedProvider(providerOrContext as SupportedProviders) ) { this._requestManager = new Web3RequestManager( + {}, providerOrContext as undefined | string | SupportedProviders, ); this._subscriptionManager = new Web3SubscriptionManager( @@ -130,7 +138,7 @@ export class Web3Context< registeredSubscriptions, accountProvider, wallet, - requestManagerMiddleware + requestManagerMiddleware, } = providerOrContext as Web3ContextInitOptions; this.setConfig(config ?? {}); @@ -138,9 +146,10 @@ export class Web3Context< this._requestManager = requestManager ?? new Web3RequestManager( + this.config, provider, config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout, - requestManagerMiddleware + requestManagerMiddleware, ); if (subscriptionManager) { @@ -217,11 +226,13 @@ export class Web3Context< this.on(Web3ConfigEvent.CONFIG_CHANGE, event => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment newContextChild.setConfig({ [event.name]: event.newValue }); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + this._requestManager.setConfig({ [event.name]: event.newValue }); }); // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context' this[ContextRef.name] = newContextChild; - + return newContextChild; } @@ -240,6 +251,8 @@ export class Web3Context< parentContext.on(Web3ConfigEvent.CONFIG_CHANGE, event => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment this.setConfig({ [event.name]: event.newValue }); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + this._requestManager.setConfig({ [event.name]: event.newValue }); }); } @@ -359,10 +372,10 @@ export class Web3Context< return true; } - public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware){ + public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware) { this.requestManager.setMiddleware(requestManagerMiddleware); } - + /** * Will return the {@link Web3BatchRequest} constructor. */ diff --git a/packages/web3-core/src/web3_request_manager.ts b/packages/web3-core/src/web3_request_manager.ts index 21783229a5f..087d8b3cbd8 100644 --- a/packages/web3-core/src/web3_request_manager.ts +++ b/packages/web3-core/src/web3_request_manager.ts @@ -53,6 +53,7 @@ import { } from './utils.js'; import { Web3EventEmitter } from './web3_event_emitter.js'; import { RequestManagerMiddleware } from './types.js'; +import { Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js'; export enum Web3RequestManagerEvent { PROVIDER_CHANGED = 'PROVIDER_CHANGED', @@ -74,15 +75,19 @@ export class Web3RequestManager< }> { private _provider?: SupportedProviders; private readonly useRpcCallSpecification?: boolean; + public config: Web3ConfigOptions; public middleware?: RequestManagerMiddleware; public constructor( + config: Web3ConfigOptions, provider?: SupportedProviders | string, useRpcCallSpecification?: boolean, requestManagerMiddleware?: RequestManagerMiddleware, ) { super(); + this.config = config; + if (!isNullish(provider)) { this.setProvider(provider); } @@ -148,6 +153,10 @@ export class Web3RequestManager< return true; } + public setConfig(options: Partial) { + Object.assign(this.config, options); + } + public setMiddleware(requestManagerMiddleware: RequestManagerMiddleware) { this.middleware = requestManagerMiddleware; } diff --git a/packages/web3-eth-personal/src/rpc_method_wrappers.ts b/packages/web3-eth-personal/src/rpc_method_wrappers.ts index 51491714881..f7e398c5f8e 100644 --- a/packages/web3-eth-personal/src/rpc_method_wrappers.ts +++ b/packages/web3-eth-personal/src/rpc_method_wrappers.ts @@ -73,7 +73,9 @@ export const sendTransaction = async ( tx: Transaction, passphrase: string, ) => { - const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT); + const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { + transactionSchema: requestManager.config.customTransactionSchema, + }); return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase); }; @@ -83,7 +85,9 @@ export const signTransaction = async ( tx: Transaction, passphrase: string, ) => { - const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT); + const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { + transactionSchema: requestManager.config.customTransactionSchema, + }); return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase); }; diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 02fb9fbb173..9931c56364f 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -296,10 +296,10 @@ export async function getBlock( const result = { ...res, transactions: res.transactions ?? [], - } + }; return result; } - + return res; } @@ -429,6 +429,7 @@ export async function getTransaction( return isNullish(response) ? response : formatTransaction(response, returnFormat, { + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }); } @@ -448,6 +449,7 @@ export async function getPendingTransactions( transaction as unknown as Transaction, returnFormat ?? web3Context.defaultReturnFormat, { + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }, ), @@ -488,6 +490,7 @@ export async function getTransactionFromBlock( return isNullish(response) ? response : formatTransaction(response, returnFormat ?? web3Context.defaultReturnFormat, { + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }); } @@ -508,26 +511,30 @@ export async function getTransactionReceipt( ); let response; try { - response = await ethRpcMethods.getTransactionReceipt( + response = await ethRpcMethods.getTransactionReceipt( web3Context.requestManager, transactionHashFormatted, ); } catch (error) { // geth indexing error, we poll until transactions stopped indexing - if (typeof error === 'object' && !isNullish(error) && 'message' in error && (error as { message: string }).message === 'transaction indexing is in progress') { - console.warn('Transaction indexing is in progress.') + if ( + typeof error === 'object' && + !isNullish(error) && + 'message' in error && + (error as { message: string }).message === 'transaction indexing is in progress' + ) { + console.warn('Transaction indexing is in progress.'); } else { throw error; } - } return isNullish(response) ? response - : (format( + : format( transactionReceiptSchema, response as unknown as TransactionReceipt, returnFormat ?? web3Context.defaultReturnFormat, - )); + ); } /** @@ -572,7 +579,7 @@ export function sendTransaction< | TransactionWithFromAndToLocalWalletIndex, returnFormat: ReturnFormat, options: SendTransactionOptions = { checkRevertBeforeSending: true }, - transactionMiddleware?: TransactionMiddleware + transactionMiddleware?: TransactionMiddleware, ): Web3PromiEvent> { const promiEvent = new Web3PromiEvent>( (resolve, reject) => { @@ -585,9 +592,9 @@ export function sendTransaction< returnFormat, }); - let transaction = {...transactionObj}; - - if(!isNullish(transactionMiddleware)){ + let transaction = { ...transactionObj }; + + if (!isNullish(transactionMiddleware)) { transaction = await transactionMiddleware.processTransaction(transaction); } @@ -602,6 +609,9 @@ export function sendTransaction< to: getTransactionFromOrToAttr('to', web3Context, transaction), }, ETH_DATA_FORMAT, + { + transactionSchema: web3Context.config.customTransactionSchema, + }, ); try { @@ -843,7 +853,9 @@ export async function signTransaction( ) { const response = await ethRpcMethods.signTransaction( web3Context.requestManager, - formatTransaction(transaction, ETH_DATA_FORMAT), + formatTransaction(transaction, ETH_DATA_FORMAT, { + transactionSchema: web3Context.config.customTransactionSchema, + }), ); // Some clients only return the encoded signed transaction (e.g. Ganache) // while clients such as Geth return the desired SignedTransactionInfoAPI object @@ -858,6 +870,7 @@ export async function signTransaction( returnFormat, ), tx: formatTransaction((response as SignedTransactionInfoAPI).tx, returnFormat, { + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }), }; @@ -881,7 +894,9 @@ export async function call( const response = await ethRpcMethods.call( web3Context.requestManager, - formatTransaction(transaction, ETH_DATA_FORMAT), + formatTransaction(transaction, ETH_DATA_FORMAT, { + transactionSchema: web3Context.config.customTransactionSchema, + }), blockNumberFormatted, ); @@ -899,7 +914,9 @@ export async function estimateGas( blockNumber: BlockNumberOrTag = web3Context.defaultBlock, returnFormat: ReturnFormat, ) { - const transactionFormatted = formatTransaction(transaction, ETH_DATA_FORMAT); + const transactionFormatted = formatTransaction(transaction, ETH_DATA_FORMAT, { + transactionSchema: web3Context.config.customTransactionSchema, + }); const blockNumberFormatted = isBlockTag(blockNumber as string) ? (blockNumber as BlockTag) : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT); @@ -1070,7 +1087,9 @@ export async function createAccessList( const response = (await ethRpcMethods.createAccessList( web3Context.requestManager, - formatTransaction(transaction, ETH_DATA_FORMAT), + formatTransaction(transaction, ETH_DATA_FORMAT, { + transactionSchema: web3Context.config.customTransactionSchema, + }), blockNumberFormatted, )) as unknown as AccessListResult; diff --git a/packages/web3-eth/src/utils/decode_signed_transaction.ts b/packages/web3-eth/src/utils/decode_signed_transaction.ts index 943ea188e2c..3adfb690228 100644 --- a/packages/web3-eth/src/utils/decode_signed_transaction.ts +++ b/packages/web3-eth/src/utils/decode_signed_transaction.ts @@ -24,6 +24,7 @@ import { bytesToHex, format, hexToBytes, keccak256 } from 'web3-utils'; import { TransactionFactory } from 'web3-eth-accounts'; import { detectRawTransactionType } from './detect_transaction_type.js'; import { formatTransaction } from './format_transaction.js'; +import { ValidationSchemaInput } from 'web3-validator'; /** * Decodes an [RLP](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/#top) encoded transaction. @@ -35,7 +36,10 @@ import { formatTransaction } from './format_transaction.js'; export function decodeSignedTransaction( encodedSignedTransaction: HexStringBytes, returnFormat: ReturnFormat, - options: { fillInputAndData?: boolean } = { fillInputAndData: false }, + options: { fillInputAndData?: boolean; transactionSchema?: ValidationSchemaInput } = { + fillInputAndData: false, + transactionSchema: undefined, + }, ): SignedTransactionInfoAPI { return { raw: format({ format: 'bytes' }, encodedSignedTransaction, returnFormat), @@ -48,7 +52,10 @@ export function decodeSignedTransaction( type: detectRawTransactionType(hexToBytes(encodedSignedTransaction)), } as TransactionSignedAPI, returnFormat, - { fillInputAndData: options.fillInputAndData }, + { + fillInputAndData: options.fillInputAndData, + transactionSchema: options.transactionSchema, + }, ), }; } diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index fe761ada1f6..6178a07d132 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -134,14 +134,17 @@ export const prepareTransactionForSigning = async ( fillGasPrice, fillGasLimit, })) as unknown as PopulatedUnsignedTransaction; - const formattedTransaction = formatTransaction( - populatedTransaction, - ETH_DATA_FORMAT, - ) as unknown as FormatType; + const formattedTransaction = formatTransaction(populatedTransaction, ETH_DATA_FORMAT, { + transactionSchema: web3Context.config.customTransactionSchema, + }) as unknown as FormatType; + validateTransactionForSigning( formattedTransaction as unknown as FormatType, + { + transactionSchema: web3Context.config.customTransactionSchema, + }, ); - + return TransactionFactory.fromTxData( getEthereumjsTxDataFromTransaction(formattedTransaction), getEthereumjsTransactionOptions(formattedTransaction, web3Context), diff --git a/packages/web3-eth/src/validation.ts b/packages/web3-eth/src/validation.ts index a818cc66c10..1d00e9f3bd6 100644 --- a/packages/web3-eth/src/validation.ts +++ b/packages/web3-eth/src/validation.ts @@ -27,7 +27,14 @@ import { TransactionWithSenderAPI, ETH_DATA_FORMAT, } from 'web3-types'; -import { isAddress, isHexStrict, isHexString32Bytes, isNullish, isUInt } from 'web3-validator'; +import { + ValidationSchemaInput, + isAddress, + isHexStrict, + isHexString32Bytes, + isNullish, + isUInt, +} from 'web3-validator'; import { ChainMismatchError, HardforkMismatchError, @@ -282,6 +289,9 @@ export const validateGas = (transaction: InternalTransaction) => { export const validateTransactionForSigning = ( transaction: InternalTransaction, overrideMethod?: (transaction: InternalTransaction) => void, + options: { + transactionSchema?: ValidationSchemaInput; + } = { transactionSchema: undefined }, ) => { if (!isNullish(overrideMethod)) { overrideMethod(transaction); @@ -296,7 +306,9 @@ export const validateTransactionForSigning = ( validateBaseChain(transaction); validateHardfork(transaction); - const formattedTransaction = formatTransaction(transaction as Transaction, ETH_DATA_FORMAT); + const formattedTransaction = formatTransaction(transaction as Transaction, ETH_DATA_FORMAT, { + transactionSchema: options.transactionInfoSchema, + }); validateGas(formattedTransaction); if ( diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index 9ce83aaa941..c255fd6a1ae 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -116,4 +116,8 @@ describe('formatTransaction', () => { }), ); }); + + it.todo('Accepts a custom schema', () => { + // TODO(nico): + }); }); From bc20d2751eb2f3d78ec9074b33209b834b8d911d Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 30 Aug 2024 09:32:05 +0200 Subject: [PATCH 02/16] docs: add entries to changelogs --- CHANGELOG.md | 21 +++++++++++++++++++++ packages/web3-core/CHANGELOG.md | 11 ++++++++--- packages/web3-eth-personal/CHANGELOG.md | 6 +++++- packages/web3-eth/CHANGELOG.md | 12 ++++++++++-- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f2e5ea09b0..88adad56335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2698,3 +2698,24 @@ If there are any bugs, improvements, optimizations or any new feature proposal f - Revert `TransactionFactory.registerTransactionType` if there is a version mistatch between `web3-eth` and `web3-eth-accounts` and fix nextjs problem. (#7216) ## [Unreleased] + +### Added + +#### web3-core + +- Adds a new property (`customTransactionSchema`) to `Web3ConfigOptions` +- Adds a new property (`config`) to `Web3RequestManager` + +#### web3-eth + +- Adds the same `{transactionSchema?: ValidationSchemaInput}` that exists in `formatTransaction` to `validateTransactionForSigning` + +### Changed + +#### web3-eth + +- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction` + +#### web3-eth-personal + +- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction` diff --git a/packages/web3-core/CHANGELOG.md b/packages/web3-core/CHANGELOG.md index 2864dbd4d8d..ca126e01d03 100644 --- a/packages/web3-core/CHANGELOG.md +++ b/packages/web3-core/CHANGELOG.md @@ -195,7 +195,7 @@ Documentation: ### Fixed -- Fix `Web3Config` to properly update within other web3 packages when `setConfig` is used (#6555) +- Fix `Web3Config` to properly update within other web3 packages when `setConfig` is used (#6555) ### Added @@ -205,7 +205,7 @@ Documentation: ### Changed -- Web3config `contractDataInputFill` has been defaulted to `data`, istead of `input`. (#6622) +- Web3config `contractDataInputFill` has been defaulted to `data`, istead of `input`. (#6622) ## [4.4.0] @@ -233,4 +233,9 @@ Documentation: - `setConfig()` fix for `setMaxListenerWarningThreshold` fix (#5079) -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Added + +- Adds a new property (`customTransactionSchema`) to `Web3ConfigOptions` +- Adds a new property (`config`) to `Web3RequestManager` diff --git a/packages/web3-eth-personal/CHANGELOG.md b/packages/web3-eth-personal/CHANGELOG.md index aba75cae036..abc79b58850 100644 --- a/packages/web3-eth-personal/CHANGELOG.md +++ b/packages/web3-eth-personal/CHANGELOG.md @@ -147,4 +147,8 @@ Documentation: - Dependencies updated -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Changed + +- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction` diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 6be4b4b5797..7d1470f593d 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -254,7 +254,7 @@ Documentation: - `sendTransaction` in `rpc_method_wrappers` accepts optional param of `TransactionMiddleware` (#7088) - WebEth has `setTransactionMiddleware` and `getTransactionMiddleware` for automatically passing to `sentTransaction` (#7088) -- `TransactionMiddleware` and `TransactionMiddleware` data types are exported (#7088) +- `TransactionMiddleware` and `TransactionMiddleware` data types are exported (#7088) ## [4.8.1] @@ -269,4 +269,12 @@ Documentation: - Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) - Change method `getTransactionReceipt` to not be casted as `TransactionReceipt` to give proper return type (#7159) -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Changed + +- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction` + +### Added + +- Adds the same `{transactionSchema?: ValidationSchemaInput}` that exists in `formatTransaction` to `validateTransactionForSigning` From fd803282ecce2b77e19e007fba7d625190f613ff Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 30 Aug 2024 17:47:52 +0200 Subject: [PATCH 03/16] tests: initial tests --- packages/web3-core/src/web3_config.ts | 11 ++++++- packages/web3-core/src/web3_context.ts | 3 +- .../web3-core/src/web3_request_manager.ts | 6 ++-- .../web3-core/test/unit/web3_config.test.ts | 1 + .../web3-core/test/unit/web3_context.test.ts | 3 ++ packages/web3-eth/CHANGELOG.md | 2 +- .../test/fixtures/format_transaction.ts | 33 +++++++++++++++++++ .../test/unit/format_transaction.test.ts | 17 ++++++++-- 8 files changed, 67 insertions(+), 9 deletions(-) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index bf944dc9ba5..cfb1939cc09 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -25,7 +25,7 @@ import { } from 'web3-types'; import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; import { isNullish, toHex } from 'web3-utils'; -import { ValidationSchemaInput } from 'web3-validator'; +import { ValidationSchemaInput, Schema } from 'web3-validator'; import { TransactionTypeParser } from './types.js'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context.js'; @@ -523,6 +523,15 @@ export abstract class Web3Config this.config.transactionTypeParser = val; } + public get customTransactionSchema() { + return this.config.customTransactionSchema; + } + + public set customTransactionSchema(schema: ValidationSchemaInput) { + this._triggerConfigChange('customTransactionSchema', schema); + this.config.customTransactionSchema = schema; + } + private _triggerConfigChange( config: K, newValue: Web3ConfigOptions[K], diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index d030b375b9a..f181a28bba2 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -119,7 +119,6 @@ export class Web3Context< isSupportedProvider(providerOrContext as SupportedProviders) ) { this._requestManager = new Web3RequestManager( - {}, providerOrContext as undefined | string | SupportedProviders, ); this._subscriptionManager = new Web3SubscriptionManager( @@ -146,7 +145,7 @@ export class Web3Context< this._requestManager = requestManager ?? new Web3RequestManager( - this.config, + config, provider, config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout, requestManagerMiddleware, diff --git a/packages/web3-core/src/web3_request_manager.ts b/packages/web3-core/src/web3_request_manager.ts index 087d8b3cbd8..70ce2425912 100644 --- a/packages/web3-core/src/web3_request_manager.ts +++ b/packages/web3-core/src/web3_request_manager.ts @@ -75,18 +75,18 @@ export class Web3RequestManager< }> { private _provider?: SupportedProviders; private readonly useRpcCallSpecification?: boolean; - public config: Web3ConfigOptions; + public config: Partial; public middleware?: RequestManagerMiddleware; public constructor( - config: Web3ConfigOptions, provider?: SupportedProviders | string, useRpcCallSpecification?: boolean, requestManagerMiddleware?: RequestManagerMiddleware, + config?: Partial, ) { super(); - this.config = config; + this.config = config ?? {}; if (!isNullish(provider)) { this.setProvider(provider); diff --git a/packages/web3-core/test/unit/web3_config.test.ts b/packages/web3-core/test/unit/web3_config.test.ts index bd250d6386a..7238c1fcb33 100644 --- a/packages/web3-core/test/unit/web3_config.test.ts +++ b/packages/web3-core/test/unit/web3_config.test.ts @@ -49,6 +49,7 @@ const defaultConfig = { defaultReturnFormat: DEFAULT_RETURN_FORMAT, transactionBuilder: undefined, transactionTypeParser: undefined, + customTransactionSchema: undefined, }; const setValue = { string: 'newValue', diff --git a/packages/web3-core/test/unit/web3_context.test.ts b/packages/web3-core/test/unit/web3_context.test.ts index b50c98e8661..6b3d211f2c6 100644 --- a/packages/web3-core/test/unit/web3_context.test.ts +++ b/packages/web3-core/test/unit/web3_context.test.ts @@ -124,6 +124,9 @@ describe('Web3Context', () => { expect(newContext.requestManager).toBeInstanceOf(Web3RequestManager); expect(newContext.config.defaultHardfork).toEqual(config.defaultHardfork); expect(newContext.config.defaultNetworkId).toEqual(config.defaultNetworkId); + expect(newContext.requestManager.config.defaultHardfork).toEqual( + config.defaultHardfork, + ); }); describe('accountsProvider', () => { diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 7d1470f593d..3e3454e63ef 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -267,7 +267,7 @@ Documentation: ### Fixed - Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) -- Change method `getTransactionReceipt` to not be casted as `TransactionReceipt` to give proper return type (#7159) + w ## [Unreleased] diff --git a/packages/web3-eth/test/fixtures/format_transaction.ts b/packages/web3-eth/test/fixtures/format_transaction.ts index dc91dc7c823..5476efa90aa 100644 --- a/packages/web3-eth/test/fixtures/format_transaction.ts +++ b/packages/web3-eth/test/fixtures/format_transaction.ts @@ -210,6 +210,39 @@ export const numbersAsBigIntTransaction: FormatType< s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0', }; +export const customFieldTransaction: FormatType< + Transaction, + { number: FMT_NUMBER.BIGINT; bytes: typeof DEFAULT_RETURN_FORMAT.bytes } +> = { + from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', + to: '0x3535353535353535353535353535353535353535', + value: BigInt(100000000000), + gas: BigInt(21000), + gasPrice: BigInt(20000000000), + type: BigInt(0), + maxFeePerGas: BigInt(78000000000), + maxPriorityFeePerGas: BigInt(1230000000), + data: '0x', + nonce: BigInt(4), + chain: 'mainnet', + hardfork: 'berlin', + chainId: BigInt(1), + common: { + customChain: { + name: 'foo', + networkId: BigInt(4), + chainId: BigInt(66), + }, + baseChain: 'mainnet', + hardfork: 'berlin', + }, + gasLimit: BigInt(21000), + v: BigInt(37), + r: '0x4f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88d', + s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0', + feeCurrency: '0x4242424242424242424242424242424242424242', +}; + const dummyTransaction: Transaction = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index c255fd6a1ae..fab79f4c988 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -25,8 +25,10 @@ import { numbersAsStringTransaction, numbersAsNumberTransaction, bytesAsUint8ArrayTransaction, + customFieldTransaction, } from '../fixtures/format_transaction'; import { objectBigintToString } from '../fixtures/system_test_utils'; +import { transactionSchema } from '../../src'; const transactionsDataForNumberTypes: Record> = { [FMT_NUMBER.BIGINT]: numbersAsBigIntTransaction, @@ -117,7 +119,18 @@ describe('formatTransaction', () => { ); }); - it.todo('Accepts a custom schema', () => { - // TODO(nico): + it('Accepts a custom schema', () => { + expect(formatTransaction(customFieldTransaction).feeCurrency).toBeUndefined(); + expect( + formatTransaction(customFieldTransaction, undefined, { + transactionSchema: { + ...transactionSchema, + properties: { + ...transactionSchema.properties, + feeCurrency: 'address', + }, + }, + }).feeCurrency, + ).not.toBeUndefined(); }); }); From 596224cbe47dff637208bd757c1133681af59b84 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 2 Sep 2024 15:20:19 +0200 Subject: [PATCH 04/16] fix: unused vars --- packages/web3-core/src/web3_config.ts | 6 +++--- packages/web3-core/src/web3_context.ts | 2 +- packages/web3-core/src/web3_request_manager.ts | 2 +- packages/web3-eth/src/utils/format_transaction.ts | 2 +- .../web3-eth/src/utils/prepare_transaction_for_signing.ts | 1 + packages/web3-eth/src/validation.ts | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index cfb1939cc09..f95156aee67 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -25,7 +25,7 @@ import { } from 'web3-types'; import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; import { isNullish, toHex } from 'web3-utils'; -import { ValidationSchemaInput, Schema } from 'web3-validator'; +import { ValidationSchemaInput } from 'web3-validator'; import { TransactionTypeParser } from './types.js'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context.js'; @@ -523,11 +523,11 @@ export abstract class Web3Config this.config.transactionTypeParser = val; } - public get customTransactionSchema() { + public get customTransactionSchema(): ValidationSchemaInput | undefined { return this.config.customTransactionSchema; } - public set customTransactionSchema(schema: ValidationSchemaInput) { + public set customTransactionSchema(schema: ValidationSchemaInput | undefined) { this._triggerConfigChange('customTransactionSchema', schema); this.config.customTransactionSchema = schema; } diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index f181a28bba2..2dd7d5a2433 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -145,10 +145,10 @@ export class Web3Context< this._requestManager = requestManager ?? new Web3RequestManager( - config, provider, config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout, requestManagerMiddleware, + config, ); if (subscriptionManager) { diff --git a/packages/web3-core/src/web3_request_manager.ts b/packages/web3-core/src/web3_request_manager.ts index 70ce2425912..684befe97f7 100644 --- a/packages/web3-core/src/web3_request_manager.ts +++ b/packages/web3-core/src/web3_request_manager.ts @@ -53,7 +53,7 @@ import { } from './utils.js'; import { Web3EventEmitter } from './web3_event_emitter.js'; import { RequestManagerMiddleware } from './types.js'; -import { Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js'; +import { Web3ConfigOptions } from './web3_config.js'; export enum Web3RequestManagerEvent { PROVIDER_CHANGED = 'PROVIDER_CHANGED', diff --git a/packages/web3-eth/src/utils/format_transaction.ts b/packages/web3-eth/src/utils/format_transaction.ts index 5333254db60..44fa58aad07 100644 --- a/packages/web3-eth/src/utils/format_transaction.ts +++ b/packages/web3-eth/src/utils/format_transaction.ts @@ -29,7 +29,7 @@ export function formatTransaction< transaction: TransactionType, returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, options: { - transactionSchema?: ValidationSchemaInput | typeof transactionSchema; + transactionSchema?: ValidationSchemaInput | typeof transactionSchema | undefined; fillInputAndData?: boolean; } = { transactionSchema: transactionInfoSchema, diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index 6178a07d132..cd70e1e265c 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -140,6 +140,7 @@ export const prepareTransactionForSigning = async ( validateTransactionForSigning( formattedTransaction as unknown as FormatType, + undefined, { transactionSchema: web3Context.config.customTransactionSchema, }, diff --git a/packages/web3-eth/src/validation.ts b/packages/web3-eth/src/validation.ts index 1d00e9f3bd6..563b6c9233e 100644 --- a/packages/web3-eth/src/validation.ts +++ b/packages/web3-eth/src/validation.ts @@ -307,7 +307,7 @@ export const validateTransactionForSigning = ( validateHardfork(transaction); const formattedTransaction = formatTransaction(transaction as Transaction, ETH_DATA_FORMAT, { - transactionSchema: options.transactionInfoSchema, + transactionSchema: options.transactionSchema, }); validateGas(formattedTransaction); From 00e231e66d580580c287b0262593b9221116d948 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 2 Sep 2024 15:29:09 +0200 Subject: [PATCH 05/16] tests: lint issues --- packages/web3-eth/src/utils/decode_signed_transaction.ts | 2 +- packages/web3-eth/test/unit/format_transaction.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth/src/utils/decode_signed_transaction.ts b/packages/web3-eth/src/utils/decode_signed_transaction.ts index 3adfb690228..73e38d79871 100644 --- a/packages/web3-eth/src/utils/decode_signed_transaction.ts +++ b/packages/web3-eth/src/utils/decode_signed_transaction.ts @@ -22,9 +22,9 @@ import { } from 'web3-types'; import { bytesToHex, format, hexToBytes, keccak256 } from 'web3-utils'; import { TransactionFactory } from 'web3-eth-accounts'; +import { ValidationSchemaInput } from 'web3-validator'; import { detectRawTransactionType } from './detect_transaction_type.js'; import { formatTransaction } from './format_transaction.js'; -import { ValidationSchemaInput } from 'web3-validator'; /** * Decodes an [RLP](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/#top) encoded transaction. diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index fab79f4c988..7098a27d0ee 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -131,6 +131,6 @@ describe('formatTransaction', () => { }, }, }).feeCurrency, - ).not.toBeUndefined(); + ).toBeDefined(); }); }); From 7361a79be0c43a8987cdd96c2a3a935d0f1e4eec Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 6 Sep 2024 15:47:08 +0200 Subject: [PATCH 06/16] refactor: pr review --- packages/web3-eth/CHANGELOG.md | 2 +- packages/web3-eth/src/rpc_method_wrappers.ts | 12 ++++++------ .../web3-eth/src/utils/decode_signed_transaction.ts | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 3e3454e63ef..7d1470f593d 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -267,7 +267,7 @@ Documentation: ### Fixed - Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) - w +- Change method `getTransactionReceipt` to not be casted as `TransactionReceipt` to give proper return type (#7159) ## [Unreleased] diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 9931c56364f..2fcbf2471e5 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -296,7 +296,7 @@ export async function getBlock( const result = { ...res, transactions: res.transactions ?? [], - }; + } return result; } @@ -530,11 +530,11 @@ export async function getTransactionReceipt( } return isNullish(response) ? response - : format( + : (format( transactionReceiptSchema, response as unknown as TransactionReceipt, returnFormat ?? web3Context.defaultReturnFormat, - ); + )); } /** @@ -579,7 +579,7 @@ export function sendTransaction< | TransactionWithFromAndToLocalWalletIndex, returnFormat: ReturnFormat, options: SendTransactionOptions = { checkRevertBeforeSending: true }, - transactionMiddleware?: TransactionMiddleware, + transactionMiddleware?: TransactionMiddleware ): Web3PromiEvent> { const promiEvent = new Web3PromiEvent>( (resolve, reject) => { @@ -592,9 +592,9 @@ export function sendTransaction< returnFormat, }); - let transaction = { ...transactionObj }; + let transaction = {...transactionObj}; - if (!isNullish(transactionMiddleware)) { + if(!isNullish(transactionMiddleware)){ transaction = await transactionMiddleware.processTransaction(transaction); } diff --git a/packages/web3-eth/src/utils/decode_signed_transaction.ts b/packages/web3-eth/src/utils/decode_signed_transaction.ts index 73e38d79871..2522b25b8e8 100644 --- a/packages/web3-eth/src/utils/decode_signed_transaction.ts +++ b/packages/web3-eth/src/utils/decode_signed_transaction.ts @@ -38,7 +38,6 @@ export function decodeSignedTransaction( returnFormat: ReturnFormat, options: { fillInputAndData?: boolean; transactionSchema?: ValidationSchemaInput } = { fillInputAndData: false, - transactionSchema: undefined, }, ): SignedTransactionInfoAPI { return { From 796660d12228eea1fb6f26ae243584badc5a89f9 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 6 Sep 2024 15:50:25 +0200 Subject: [PATCH 07/16] fix: type errors --- packages/web3-core/src/web3_context.ts | 1 + .../web3-core/src/web3_request_manager.ts | 2 +- .../__snapshots__/web3_context.test.ts.snap | 3 +++ .../test/fixtures/format_transaction.ts | 3 ++- .../test/unit/format_transaction.test.ts | 25 +++++++++++++------ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 2dd7d5a2433..5acbce324f2 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -36,6 +36,7 @@ import { ExtensionObject, RequestManagerMiddleware } from './types.js'; import { Web3BatchRequest } from './web3_batch_request.js'; // eslint-disable-next-line import/no-cycle import { Web3Config, Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js'; +// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; import { Web3SubscriptionConstructor } from './web3_subscriptions.js'; import { Web3SubscriptionManager } from './web3_subscription_manager.js'; diff --git a/packages/web3-core/src/web3_request_manager.ts b/packages/web3-core/src/web3_request_manager.ts index 684befe97f7..2d6386c49ed 100644 --- a/packages/web3-core/src/web3_request_manager.ts +++ b/packages/web3-core/src/web3_request_manager.ts @@ -53,7 +53,7 @@ import { } from './utils.js'; import { Web3EventEmitter } from './web3_event_emitter.js'; import { RequestManagerMiddleware } from './types.js'; -import { Web3ConfigOptions } from './web3_config.js'; +import { type Web3ConfigOptions } from './web3_config.js'; export enum Web3RequestManagerEvent { PROVIDER_CHANGED = 'PROVIDER_CHANGED', diff --git a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap index e01ba14a467..c4a97626642 100644 --- a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap +++ b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap @@ -6,6 +6,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] = "config": { "blockHeaderTimeout": 10, "contractDataInputFill": "data", + "customTransactionSchema": undefined, "defaultAccount": undefined, "defaultBlock": "latest", "defaultChain": "mainnet", @@ -73,6 +74,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] = "clientUrl": "http://test/abc", "httpProviderOptions": undefined, }, + "config": {}, "useRpcCallSpecification": undefined, }, "subscriptionManager": Web3SubscriptionManager { @@ -108,6 +110,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] = "clientUrl": "http://test/abc", "httpProviderOptions": undefined, }, + "config": {}, "useRpcCallSpecification": undefined, }, "tolerateUnlinkedSubscription": false, diff --git a/packages/web3-eth/test/fixtures/format_transaction.ts b/packages/web3-eth/test/fixtures/format_transaction.ts index 5476efa90aa..6ab3fa0a9d2 100644 --- a/packages/web3-eth/test/fixtures/format_transaction.ts +++ b/packages/web3-eth/test/fixtures/format_transaction.ts @@ -210,8 +210,9 @@ export const numbersAsBigIntTransaction: FormatType< s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0', }; +export type CustomFieldTransaction = Transaction & { feeCurrency: `0x${string}` }; export const customFieldTransaction: FormatType< - Transaction, + CustomFieldTransaction, { number: FMT_NUMBER.BIGINT; bytes: typeof DEFAULT_RETURN_FORMAT.bytes } > = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index 7098a27d0ee..2a13c10b561 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -26,6 +26,7 @@ import { numbersAsNumberTransaction, bytesAsUint8ArrayTransaction, customFieldTransaction, + CustomFieldTransaction, } from '../fixtures/format_transaction'; import { objectBigintToString } from '../fixtures/system_test_utils'; import { transactionSchema } from '../../src'; @@ -120,17 +121,25 @@ describe('formatTransaction', () => { }); it('Accepts a custom schema', () => { - expect(formatTransaction(customFieldTransaction).feeCurrency).toBeUndefined(); expect( - formatTransaction(customFieldTransaction, undefined, { - transactionSchema: { - ...transactionSchema, - properties: { - ...transactionSchema.properties, - feeCurrency: 'address', + formatTransaction( + customFieldTransaction, + ).feeCurrency, + ).toBeUndefined(); + expect( + formatTransaction( + customFieldTransaction, + undefined, + { + transactionSchema: { + ...transactionSchema, + properties: { + ...transactionSchema.properties, + feeCurrency: { format: 'address' }, + }, }, }, - }).feeCurrency, + ).feeCurrency, ).toBeDefined(); }); }); From b5d29cf019a7991ca35cec1cdfa1b946a37dd6c6 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 6 Sep 2024 16:09:41 +0200 Subject: [PATCH 08/16] fix: dependency cycle --- packages/web3-core/src/web3_batch_request.ts | 1 + packages/web3-core/src/web3_subscription_manager.ts | 1 + packages/web3-core/src/web3_subscriptions.ts | 1 + 3 files changed, 3 insertions(+) diff --git a/packages/web3-core/src/web3_batch_request.ts b/packages/web3-core/src/web3_batch_request.ts index 10a3eaf4693..24616b4e7c0 100644 --- a/packages/web3-core/src/web3_batch_request.ts +++ b/packages/web3-core/src/web3_batch_request.ts @@ -18,6 +18,7 @@ along with web3.js. If not, see . import { JsonRpcBatchResponse, JsonRpcOptionalRequest, JsonRpcRequest } from 'web3-types'; import { jsonRpc, Web3DeferredPromise } from 'web3-utils'; import { OperationAbortError, OperationTimeoutError, ResponseError } from 'web3-errors'; +// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; export const DEFAULT_BATCH_REQUEST_TIMEOUT = 1000; diff --git a/packages/web3-core/src/web3_subscription_manager.ts b/packages/web3-core/src/web3_subscription_manager.ts index 8e76498e3c2..75f7b76f8bd 100644 --- a/packages/web3-core/src/web3_subscription_manager.ts +++ b/packages/web3-core/src/web3_subscription_manager.ts @@ -29,6 +29,7 @@ import { import { ProviderError, SubscriptionError } from 'web3-errors'; import { isNullish } from 'web3-utils'; import { isSupportSubscriptions } from './utils.js'; +// eslint-disable-next-line import/no-cycle import { Web3RequestManager, Web3RequestManagerEvent } from './web3_request_manager.js'; // eslint-disable-next-line import/no-cycle import { Web3SubscriptionConstructor } from './web3_subscriptions.js'; diff --git a/packages/web3-core/src/web3_subscriptions.ts b/packages/web3-core/src/web3_subscriptions.ts index 21cd4fe21fe..6cb75259c6a 100644 --- a/packages/web3-core/src/web3_subscriptions.ts +++ b/packages/web3-core/src/web3_subscriptions.ts @@ -34,6 +34,7 @@ import { jsonRpc } from 'web3-utils'; // eslint-disable-next-line import/no-cycle import { Web3SubscriptionManager } from './web3_subscription_manager.js'; import { Web3EventEmitter, Web3EventMap } from './web3_event_emitter.js'; +// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; type CommonSubscriptionEvents = { From 2593ba4f0573e57ca64a8d90ea0076185e1b574a Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Wed, 11 Sep 2024 09:40:01 +0200 Subject: [PATCH 09/16] refactor: revert whitespaces changes --- packages/web3-core/src/web3_config.ts | 7 +++---- packages/web3-core/src/web3_context.ts | 8 ++++---- packages/web3-eth/src/rpc_method_wrappers.ts | 16 ++++++---------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index f95156aee67..b81e7117369 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -118,11 +118,10 @@ export abstract class Web3Config for (const key of keys) { this._triggerConfigChange(key, options[key]); - if ( - !isNullish(options[key]) && + if(!isNullish(options[key]) && typeof options[key] === 'number' && - key === 'maxListenersWarningThreshold' - ) { + key === 'maxListenersWarningThreshold' ) + { // additionally set in event emitter this.setMaxListenerWarningThreshold(Number(options[key])); } diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 5acbce324f2..29dab69b78b 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -138,7 +138,7 @@ export class Web3Context< registeredSubscriptions, accountProvider, wallet, - requestManagerMiddleware, + requestManagerMiddleware } = providerOrContext as Web3ContextInitOptions; this.setConfig(config ?? {}); @@ -232,7 +232,7 @@ export class Web3Context< // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context' this[ContextRef.name] = newContextChild; - + return newContextChild; } @@ -372,10 +372,10 @@ export class Web3Context< return true; } - public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware) { + public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware){ this.requestManager.setMiddleware(requestManagerMiddleware); } - + /** * Will return the {@link Web3BatchRequest} constructor. */ diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 2fcbf2471e5..dd4e0362499 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -299,7 +299,7 @@ export async function getBlock( } return result; } - + return res; } @@ -511,22 +511,18 @@ export async function getTransactionReceipt( ); let response; try { - response = await ethRpcMethods.getTransactionReceipt( + response = await ethRpcMethods.getTransactionReceipt( web3Context.requestManager, transactionHashFormatted, ); } catch (error) { // geth indexing error, we poll until transactions stopped indexing - if ( - typeof error === 'object' && - !isNullish(error) && - 'message' in error && - (error as { message: string }).message === 'transaction indexing is in progress' - ) { - console.warn('Transaction indexing is in progress.'); + if (typeof error === 'object' && !isNullish(error) && 'message' in error && (error as { message: string }).message === 'transaction indexing is in progress') { + console.warn('Transaction indexing is in progress.') } else { throw error; } + } return isNullish(response) ? response @@ -593,7 +589,7 @@ export function sendTransaction< }); let transaction = {...transactionObj}; - + if(!isNullish(transactionMiddleware)){ transaction = await transactionMiddleware.processTransaction(transaction); } From b6f459ecabd0351d8735c6039db0632acd9f8a7b Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Wed, 11 Sep 2024 09:58:24 +0200 Subject: [PATCH 10/16] fix: types --- packages/web3-eth/src/utils/format_transaction.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth/src/utils/format_transaction.ts b/packages/web3-eth/src/utils/format_transaction.ts index 44fa58aad07..beaafcf8b19 100644 --- a/packages/web3-eth/src/utils/format_transaction.ts +++ b/packages/web3-eth/src/utils/format_transaction.ts @@ -16,12 +16,17 @@ along with web3.js. If not, see . */ import { Transaction, DataFormat, DEFAULT_RETURN_FORMAT, FormatType } from 'web3-types'; -import { isNullish, ValidationSchemaInput } from 'web3-validator'; +import { isNullish, ValidationSchemaInput, Schema } from 'web3-validator'; import { mergeDeep, format, bytesToHex, toHex } from 'web3-utils'; import { TransactionDataAndInputError } from 'web3-errors'; import { transactionInfoSchema, transactionSchema } from '../schemas.js'; +type TransactionSchema = { + type: 'object'; + properties: typeof transactionSchema['properties'] & Record; +}; + export function formatTransaction< ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, TransactionType extends Transaction = Transaction, @@ -29,7 +34,7 @@ export function formatTransaction< transaction: TransactionType, returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, options: { - transactionSchema?: ValidationSchemaInput | typeof transactionSchema | undefined; + transactionSchema?: ValidationSchemaInput | TransactionSchema | undefined; fillInputAndData?: boolean; } = { transactionSchema: transactionInfoSchema, From 11d8de75cf23864b262400262f443cfc70e9e67d Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Wed, 11 Sep 2024 14:00:31 +0200 Subject: [PATCH 11/16] fix: types --- docs/docs/guides/web3_config/index.md | 4 ++++ packages/web3-eth/src/utils/format_transaction.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/docs/guides/web3_config/index.md b/docs/docs/guides/web3_config/index.md index 42561aedcbc..32f645a465a 100644 --- a/docs/docs/guides/web3_config/index.md +++ b/docs/docs/guides/web3_config/index.md @@ -28,6 +28,7 @@ There is list of configuration params that can be set for modifying behavior of - [defaultCommon](/guides/web3_config/#defaultcommon) - [defaultTransactionType](/guides/web3_config/#defaulttransactiontype) - [defaultMaxPriorityFeePerGas](/guides/web3_config/#defaultmaxpriorityfeepergas) +- [customTransactionSchema](/guides/web3_config/#customTransactionSchema) - [defaultReturnFormat](/guides/web3_config/#defaultreturnformat) ## Global level Config @@ -411,6 +412,9 @@ The `defaultMaxPriorityFeePerGas` option is used to set the [`defaultMaxPriority The default value of `defaultMaxPriorityFeePerGas` is 2500000000 (2.5gwei) in hexstring format. +### [customTransactionSchema](/api/web3-core/class/Web3Config#customTransactionSchema) +The `customTransactionSchema` option is used to allow [`formatTransaction`](/api/web3-eth/function/formatTransaction) to accept a custom schema to validate transactions. A use-case could be: your chain has an extra field in its transactions and you want to write a plugin that makes sending these transactions easier. + ### [defaultReturnFormat](/api/web3-core/class/Web3Config#defaultReturnFormat) The `defaultReturnFormat` option allows users to specify the format in which certain types of data should be returned by default. It is a configuration parameter that can be set at the global level and affects how data is returned across the entire library. ```ts diff --git a/packages/web3-eth/src/utils/format_transaction.ts b/packages/web3-eth/src/utils/format_transaction.ts index beaafcf8b19..05dc47068e3 100644 --- a/packages/web3-eth/src/utils/format_transaction.ts +++ b/packages/web3-eth/src/utils/format_transaction.ts @@ -23,7 +23,7 @@ import { TransactionDataAndInputError } from 'web3-errors'; import { transactionInfoSchema, transactionSchema } from '../schemas.js'; type TransactionSchema = { - type: 'object'; + type: string; properties: typeof transactionSchema['properties'] & Record; }; From 1f8d4160b1397e9771bcdccf2f3f11c2567dfb48 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Fri, 13 Sep 2024 10:07:47 +0200 Subject: [PATCH 12/16] test: fix web3-eth-personal tests --- .../web3-eth-personal/test/unit/eth_personal.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-personal/test/unit/eth_personal.test.ts b/packages/web3-eth-personal/test/unit/eth_personal.test.ts index fc2d545da10..45104d82d2d 100644 --- a/packages/web3-eth-personal/test/unit/eth_personal.test.ts +++ b/packages/web3-eth-personal/test/unit/eth_personal.test.ts @@ -185,7 +185,11 @@ describe('Personal', () => { await personal.sendTransaction(tx, 'password'); expect(eth.formatTransaction).toHaveBeenCalledTimes(1); - expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT); + expect(eth.formatTransaction).toHaveBeenCalledWith( + tx, + ETH_DATA_FORMAT, + expect.anything(), + ); }); }); @@ -215,7 +219,11 @@ describe('Personal', () => { await personal.signTransaction(tx, 'password'); expect(eth.formatTransaction).toHaveBeenCalledTimes(1); - expect(eth.formatTransaction).toHaveBeenCalledWith(tx, ETH_DATA_FORMAT); + expect(eth.formatTransaction).toHaveBeenCalledWith( + tx, + ETH_DATA_FORMAT, + expect.anything(), + ); }); }); From 125bf12f7b5eab62f087fb64ef5ae0862da61bf6 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 16 Sep 2024 16:49:25 +0200 Subject: [PATCH 13/16] refactor: remove config from manager --- packages/web3-core/src/web3_batch_request.ts | 1 - packages/web3-core/src/web3_config.ts | 8 ++++---- packages/web3-core/src/web3_context.ts | 6 ------ packages/web3-core/src/web3_request_manager.ts | 9 --------- packages/web3-core/src/web3_subscription_manager.ts | 1 - packages/web3-core/src/web3_subscriptions.ts | 1 - .../unit/__snapshots__/web3_context.test.ts.snap | 2 -- packages/web3-eth-personal/src/personal.ts | 4 ++-- .../web3-eth-personal/src/rpc_method_wrappers.ts | 10 ++++++---- packages/web3-eth/src/rpc_method_wrappers.ts | 13 +++++++------ packages/web3-eth/src/types.ts | 7 +++++++ .../web3-eth/src/utils/decode_signed_transaction.ts | 4 ++-- packages/web3-eth/src/utils/format_transaction.ts | 12 ++++-------- .../src/utils/prepare_transaction_for_signing.ts | 6 ++++-- packages/web3-eth/src/validation.ts | 4 ++-- .../rpc_method_wrappers/get_transaction.test.ts | 8 ++++++-- 16 files changed, 44 insertions(+), 52 deletions(-) diff --git a/packages/web3-core/src/web3_batch_request.ts b/packages/web3-core/src/web3_batch_request.ts index 24616b4e7c0..10a3eaf4693 100644 --- a/packages/web3-core/src/web3_batch_request.ts +++ b/packages/web3-core/src/web3_batch_request.ts @@ -18,7 +18,6 @@ along with web3.js. If not, see . import { JsonRpcBatchResponse, JsonRpcOptionalRequest, JsonRpcRequest } from 'web3-types'; import { jsonRpc, Web3DeferredPromise } from 'web3-utils'; import { OperationAbortError, OperationTimeoutError, ResponseError } from 'web3-errors'; -// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; export const DEFAULT_BATCH_REQUEST_TIMEOUT = 1000; diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index f95156aee67..274df6d5d6c 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -25,7 +25,7 @@ import { } from 'web3-types'; import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; import { isNullish, toHex } from 'web3-utils'; -import { ValidationSchemaInput } from 'web3-validator'; +import { Schema } from 'web3-validator'; import { TransactionTypeParser } from './types.js'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context.js'; @@ -60,7 +60,7 @@ export interface Web3ConfigOptions { }; transactionBuilder?: TransactionBuilder; transactionTypeParser?: TransactionTypeParser; - customTransactionSchema?: ValidationSchemaInput; + customTransactionSchema?: Record; defaultReturnFormat: DataFormat; } @@ -523,11 +523,11 @@ export abstract class Web3Config this.config.transactionTypeParser = val; } - public get customTransactionSchema(): ValidationSchemaInput | undefined { + public get customTransactionSchema(): Record | undefined { return this.config.customTransactionSchema; } - public set customTransactionSchema(schema: ValidationSchemaInput | undefined) { + public set customTransactionSchema(schema: Record | undefined) { this._triggerConfigChange('customTransactionSchema', schema); this.config.customTransactionSchema = schema; } diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 5acbce324f2..207c4a64574 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -36,7 +36,6 @@ import { ExtensionObject, RequestManagerMiddleware } from './types.js'; import { Web3BatchRequest } from './web3_batch_request.js'; // eslint-disable-next-line import/no-cycle import { Web3Config, Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js'; -// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; import { Web3SubscriptionConstructor } from './web3_subscriptions.js'; import { Web3SubscriptionManager } from './web3_subscription_manager.js'; @@ -149,7 +148,6 @@ export class Web3Context< provider, config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout, requestManagerMiddleware, - config, ); if (subscriptionManager) { @@ -226,8 +224,6 @@ export class Web3Context< this.on(Web3ConfigEvent.CONFIG_CHANGE, event => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment newContextChild.setConfig({ [event.name]: event.newValue }); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - this._requestManager.setConfig({ [event.name]: event.newValue }); }); // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context' @@ -251,8 +247,6 @@ export class Web3Context< parentContext.on(Web3ConfigEvent.CONFIG_CHANGE, event => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment this.setConfig({ [event.name]: event.newValue }); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - this._requestManager.setConfig({ [event.name]: event.newValue }); }); } diff --git a/packages/web3-core/src/web3_request_manager.ts b/packages/web3-core/src/web3_request_manager.ts index 2d6386c49ed..21783229a5f 100644 --- a/packages/web3-core/src/web3_request_manager.ts +++ b/packages/web3-core/src/web3_request_manager.ts @@ -53,7 +53,6 @@ import { } from './utils.js'; import { Web3EventEmitter } from './web3_event_emitter.js'; import { RequestManagerMiddleware } from './types.js'; -import { type Web3ConfigOptions } from './web3_config.js'; export enum Web3RequestManagerEvent { PROVIDER_CHANGED = 'PROVIDER_CHANGED', @@ -75,19 +74,15 @@ export class Web3RequestManager< }> { private _provider?: SupportedProviders; private readonly useRpcCallSpecification?: boolean; - public config: Partial; public middleware?: RequestManagerMiddleware; public constructor( provider?: SupportedProviders | string, useRpcCallSpecification?: boolean, requestManagerMiddleware?: RequestManagerMiddleware, - config?: Partial, ) { super(); - this.config = config ?? {}; - if (!isNullish(provider)) { this.setProvider(provider); } @@ -153,10 +148,6 @@ export class Web3RequestManager< return true; } - public setConfig(options: Partial) { - Object.assign(this.config, options); - } - public setMiddleware(requestManagerMiddleware: RequestManagerMiddleware) { this.middleware = requestManagerMiddleware; } diff --git a/packages/web3-core/src/web3_subscription_manager.ts b/packages/web3-core/src/web3_subscription_manager.ts index 75f7b76f8bd..8e76498e3c2 100644 --- a/packages/web3-core/src/web3_subscription_manager.ts +++ b/packages/web3-core/src/web3_subscription_manager.ts @@ -29,7 +29,6 @@ import { import { ProviderError, SubscriptionError } from 'web3-errors'; import { isNullish } from 'web3-utils'; import { isSupportSubscriptions } from './utils.js'; -// eslint-disable-next-line import/no-cycle import { Web3RequestManager, Web3RequestManagerEvent } from './web3_request_manager.js'; // eslint-disable-next-line import/no-cycle import { Web3SubscriptionConstructor } from './web3_subscriptions.js'; diff --git a/packages/web3-core/src/web3_subscriptions.ts b/packages/web3-core/src/web3_subscriptions.ts index 6cb75259c6a..21cd4fe21fe 100644 --- a/packages/web3-core/src/web3_subscriptions.ts +++ b/packages/web3-core/src/web3_subscriptions.ts @@ -34,7 +34,6 @@ import { jsonRpc } from 'web3-utils'; // eslint-disable-next-line import/no-cycle import { Web3SubscriptionManager } from './web3_subscription_manager.js'; import { Web3EventEmitter, Web3EventMap } from './web3_event_emitter.js'; -// eslint-disable-next-line import/no-cycle import { Web3RequestManager } from './web3_request_manager.js'; type CommonSubscriptionEvents = { diff --git a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap index c4a97626642..7d280405bfb 100644 --- a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap +++ b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap @@ -74,7 +74,6 @@ exports[`Web3Context getContextObject should return correct context object 1`] = "clientUrl": "http://test/abc", "httpProviderOptions": undefined, }, - "config": {}, "useRpcCallSpecification": undefined, }, "subscriptionManager": Web3SubscriptionManager { @@ -110,7 +109,6 @@ exports[`Web3Context getContextObject should return correct context object 1`] = "clientUrl": "http://test/abc", "httpProviderOptions": undefined, }, - "config": {}, "useRpcCallSpecification": undefined, }, "tolerateUnlinkedSubscription": false, diff --git a/packages/web3-eth-personal/src/personal.ts b/packages/web3-eth-personal/src/personal.ts index db8d2656703..012a0738803 100644 --- a/packages/web3-eth-personal/src/personal.ts +++ b/packages/web3-eth-personal/src/personal.ts @@ -159,7 +159,7 @@ export class Personal extends Web3Context { * ``` */ public async sendTransaction(tx: Transaction, passphrase: string) { - return rpcWrappers.sendTransaction(this.requestManager, tx, passphrase); + return rpcWrappers.sendTransaction(this.requestManager, tx, passphrase, this.config); } /** * Signs a transaction. This account needs to be unlocked. @@ -204,7 +204,7 @@ export class Personal extends Web3Context { * ``` */ public async signTransaction(tx: Transaction, passphrase: string) { - return rpcWrappers.signTransaction(this.requestManager, tx, passphrase); + return rpcWrappers.signTransaction(this.requestManager, tx, passphrase, this.config); } /** * Calculates an Ethereum specific signature with: diff --git a/packages/web3-eth-personal/src/rpc_method_wrappers.ts b/packages/web3-eth-personal/src/rpc_method_wrappers.ts index f7e398c5f8e..906ef327da1 100644 --- a/packages/web3-eth-personal/src/rpc_method_wrappers.ts +++ b/packages/web3-eth-personal/src/rpc_method_wrappers.ts @@ -14,9 +14,9 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { Web3RequestManager } from 'web3-core'; +import { Web3RequestManager, Web3ConfigOptions } from 'web3-core'; import { toChecksumAddress, utf8ToHex } from 'web3-utils'; -import { formatTransaction } from 'web3-eth'; +import { formatTransaction, type CustomTransactionSchema } from 'web3-eth'; import { Address, EthPersonalAPI, ETH_DATA_FORMAT, HexString, Transaction } from 'web3-types'; import { validator, isHexStrict } from 'web3-validator'; import { personalRpcMethods } from 'web3-rpc-methods'; @@ -72,9 +72,10 @@ export const sendTransaction = async ( requestManager: Web3RequestManager, tx: Transaction, passphrase: string, + config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: requestManager.config.customTransactionSchema, + transactionSchema: config?.customTransactionSchema as CustomTransactionSchema, }); return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase); @@ -84,9 +85,10 @@ export const signTransaction = async ( requestManager: Web3RequestManager, tx: Transaction, passphrase: string, + config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: requestManager.config.customTransactionSchema, + transactionSchema: config?.customTransactionSchema as CustomTransactionSchema, }); return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase); diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 9931c56364f..ea39e45682b 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -66,6 +66,7 @@ import { SignatureObjectSchema, } from './schemas.js'; import { + type CustomTransactionSchema, SendSignedTransactionEvents, SendSignedTransactionOptions, SendTransactionEvents, @@ -429,7 +430,7 @@ export async function getTransaction( return isNullish(response) ? response : formatTransaction(response, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, fillInputAndData: true, }); } @@ -449,7 +450,7 @@ export async function getPendingTransactions( transaction as unknown as Transaction, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, fillInputAndData: true, }, ), @@ -490,7 +491,7 @@ export async function getTransactionFromBlock( return isNullish(response) ? response : formatTransaction(response, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, fillInputAndData: true, }); } @@ -610,7 +611,7 @@ export function sendTransaction< }, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, }, ); @@ -854,7 +855,7 @@ export async function signTransaction( const response = await ethRpcMethods.signTransaction( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, }), ); // Some clients only return the encoded signed transaction (e.g. Ganache) @@ -870,7 +871,7 @@ export async function signTransaction( returnFormat, ), tx: formatTransaction((response as SignedTransactionInfoAPI).tx, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, fillInputAndData: true, }), }; diff --git a/packages/web3-eth/src/types.ts b/packages/web3-eth/src/types.ts index 9f9de0ec14a..abe0cc9bca3 100644 --- a/packages/web3-eth/src/types.ts +++ b/packages/web3-eth/src/types.ts @@ -37,6 +37,8 @@ import { TransactionWithFromLocalWalletIndex, TransactionWithToLocalWalletIndex, } from 'web3-types'; +import { Schema } from 'web3-validator'; +import { transactionSchema } from './schemas.js'; export type InternalTransaction = FormatType; @@ -105,3 +107,8 @@ export interface TransactionMiddleware { options?: { [key: string]: unknown }, ): Promise; } + +export type CustomTransactionSchema = { + type: string; + properties: typeof transactionSchema['properties'] & Record; +}; diff --git a/packages/web3-eth/src/utils/decode_signed_transaction.ts b/packages/web3-eth/src/utils/decode_signed_transaction.ts index 2522b25b8e8..6a313f9b775 100644 --- a/packages/web3-eth/src/utils/decode_signed_transaction.ts +++ b/packages/web3-eth/src/utils/decode_signed_transaction.ts @@ -22,9 +22,9 @@ import { } from 'web3-types'; import { bytesToHex, format, hexToBytes, keccak256 } from 'web3-utils'; import { TransactionFactory } from 'web3-eth-accounts'; -import { ValidationSchemaInput } from 'web3-validator'; import { detectRawTransactionType } from './detect_transaction_type.js'; import { formatTransaction } from './format_transaction.js'; +import { type CustomTransactionSchema } from '../types.js'; /** * Decodes an [RLP](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/#top) encoded transaction. @@ -36,7 +36,7 @@ import { formatTransaction } from './format_transaction.js'; export function decodeSignedTransaction( encodedSignedTransaction: HexStringBytes, returnFormat: ReturnFormat, - options: { fillInputAndData?: boolean; transactionSchema?: ValidationSchemaInput } = { + options: { fillInputAndData?: boolean; transactionSchema?: CustomTransactionSchema } = { fillInputAndData: false, }, ): SignedTransactionInfoAPI { diff --git a/packages/web3-eth/src/utils/format_transaction.ts b/packages/web3-eth/src/utils/format_transaction.ts index 05dc47068e3..53d29659bbe 100644 --- a/packages/web3-eth/src/utils/format_transaction.ts +++ b/packages/web3-eth/src/utils/format_transaction.ts @@ -16,16 +16,12 @@ along with web3.js. If not, see . */ import { Transaction, DataFormat, DEFAULT_RETURN_FORMAT, FormatType } from 'web3-types'; -import { isNullish, ValidationSchemaInput, Schema } from 'web3-validator'; +import { isNullish, ValidationSchemaInput } from 'web3-validator'; import { mergeDeep, format, bytesToHex, toHex } from 'web3-utils'; import { TransactionDataAndInputError } from 'web3-errors'; -import { transactionInfoSchema, transactionSchema } from '../schemas.js'; - -type TransactionSchema = { - type: string; - properties: typeof transactionSchema['properties'] & Record; -}; +import { transactionInfoSchema } from '../schemas.js'; +import { type CustomTransactionSchema } from '../types.js'; export function formatTransaction< ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, @@ -34,7 +30,7 @@ export function formatTransaction< transaction: TransactionType, returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, options: { - transactionSchema?: ValidationSchemaInput | TransactionSchema | undefined; + transactionSchema?: ValidationSchemaInput | CustomTransactionSchema | undefined; fillInputAndData?: boolean; } = { transactionSchema: transactionInfoSchema, diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index cd70e1e265c..ae27dddaa1f 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -33,6 +33,7 @@ import { isNullish } from 'web3-validator'; import { validateTransactionForSigning } from '../validation.js'; import { formatTransaction } from './format_transaction.js'; import { transactionBuilder } from './transaction_builder.js'; +import { type CustomTransactionSchema } from '../types.js'; const getEthereumjsTxDataFromTransaction = ( transaction: FormatType, @@ -135,14 +136,15 @@ export const prepareTransactionForSigning = async ( fillGasLimit, })) as unknown as PopulatedUnsignedTransaction; const formattedTransaction = formatTransaction(populatedTransaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, }) as unknown as FormatType; validateTransactionForSigning( formattedTransaction as unknown as FormatType, undefined, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config + .customTransactionSchema as CustomTransactionSchema, }, ); diff --git a/packages/web3-eth/src/validation.ts b/packages/web3-eth/src/validation.ts index 563b6c9233e..4aabf31b4e7 100644 --- a/packages/web3-eth/src/validation.ts +++ b/packages/web3-eth/src/validation.ts @@ -55,7 +55,7 @@ import { UnsupportedFeeMarketError, } from 'web3-errors'; import { formatTransaction } from './utils/format_transaction.js'; -import { InternalTransaction } from './types.js'; +import { CustomTransactionSchema, InternalTransaction } from './types.js'; export function isBaseTransaction(value: BaseTransactionAPI): boolean { if (!isNullish(value.to) && !isAddress(value.to)) return false; @@ -290,7 +290,7 @@ export const validateTransactionForSigning = ( transaction: InternalTransaction, overrideMethod?: (transaction: InternalTransaction) => void, options: { - transactionSchema?: ValidationSchemaInput; + transactionSchema?: CustomTransactionSchema; } = { transactionSchema: undefined }, ) => { if (!isNullish(overrideMethod)) { diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/get_transaction.test.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/get_transaction.test.ts index 225a9e106ef..cb1315c56b1 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/get_transaction.test.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/get_transaction.test.ts @@ -21,7 +21,11 @@ import { ethRpcMethods } from 'web3-rpc-methods'; import { getTransaction } from '../../../src/rpc_method_wrappers'; import { mockRpcResponse, testData } from './fixtures/get_transaction'; -import { formatTransaction, transactionInfoSchema } from '../../../src'; +import { + type CustomTransactionSchema, + formatTransaction, + transactionInfoSchema, +} from '../../../src'; jest.mock('web3-rpc-methods'); @@ -57,7 +61,7 @@ describe('getTransaction', () => { const expectedFormattedResult = formatTransaction( mockRpcResponse, expectedReturnFormat, - { transactionSchema: transactionInfoSchema }, + { transactionSchema: transactionInfoSchema as CustomTransactionSchema }, ); (ethRpcMethods.getTransactionByHash as jest.Mock).mockResolvedValueOnce( mockRpcResponse, From 0348f44dc3f46a332e0dd4369268acfbeb0ae3fb Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 16 Sep 2024 17:22:59 +0200 Subject: [PATCH 14/16] fix: types --- packages/web3-eth/src/rpc_method_wrappers.ts | 18 +++++++++--------- .../utils/prepare_transaction_for_signing.ts | 4 ++-- packages/web3-eth/src/validation.ts | 1 - 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index ea39e45682b..aec142ec741 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -430,7 +430,7 @@ export async function getTransaction( return isNullish(response) ? response : formatTransaction(response, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, fillInputAndData: true, }); } @@ -450,7 +450,7 @@ export async function getPendingTransactions( transaction as unknown as Transaction, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, fillInputAndData: true, }, ), @@ -491,7 +491,7 @@ export async function getTransactionFromBlock( return isNullish(response) ? response : formatTransaction(response, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, fillInputAndData: true, }); } @@ -611,7 +611,7 @@ export function sendTransaction< }, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }, ); @@ -855,7 +855,7 @@ export async function signTransaction( const response = await ethRpcMethods.signTransaction( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }), ); // Some clients only return the encoded signed transaction (e.g. Ganache) @@ -871,7 +871,7 @@ export async function signTransaction( returnFormat, ), tx: formatTransaction((response as SignedTransactionInfoAPI).tx, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, fillInputAndData: true, }), }; @@ -896,7 +896,7 @@ export async function call( const response = await ethRpcMethods.call( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }), blockNumberFormatted, ); @@ -916,7 +916,7 @@ export async function estimateGas( returnFormat: ReturnFormat, ) { const transactionFormatted = formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }); const blockNumberFormatted = isBlockTag(blockNumber as string) ? (blockNumber as BlockTag) @@ -1089,7 +1089,7 @@ export async function createAccessList( const response = (await ethRpcMethods.createAccessList( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }), blockNumberFormatted, )) as unknown as AccessListResult; diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index ae27dddaa1f..e47a18dad39 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -136,7 +136,7 @@ export const prepareTransactionForSigning = async ( fillGasLimit, })) as unknown as PopulatedUnsignedTransaction; const formattedTransaction = formatTransaction(populatedTransaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, }) as unknown as FormatType; validateTransactionForSigning( @@ -144,7 +144,7 @@ export const prepareTransactionForSigning = async ( undefined, { transactionSchema: web3Context.config - .customTransactionSchema as CustomTransactionSchema, + .customTransactionSchema as unknown as CustomTransactionSchema, }, ); diff --git a/packages/web3-eth/src/validation.ts b/packages/web3-eth/src/validation.ts index 4aabf31b4e7..b318e16dd41 100644 --- a/packages/web3-eth/src/validation.ts +++ b/packages/web3-eth/src/validation.ts @@ -28,7 +28,6 @@ import { ETH_DATA_FORMAT, } from 'web3-types'; import { - ValidationSchemaInput, isAddress, isHexStrict, isHexString32Bytes, From e2caa166b778c3bcc1acf060b513cdf3379c1509 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 16 Sep 2024 21:16:43 +0200 Subject: [PATCH 15/16] fix: build issue --- packages/web3-eth-personal/src/rpc_method_wrappers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-personal/src/rpc_method_wrappers.ts b/packages/web3-eth-personal/src/rpc_method_wrappers.ts index 906ef327da1..9ad816bbc8d 100644 --- a/packages/web3-eth-personal/src/rpc_method_wrappers.ts +++ b/packages/web3-eth-personal/src/rpc_method_wrappers.ts @@ -75,7 +75,7 @@ export const sendTransaction = async ( config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: config?.customTransactionSchema as CustomTransactionSchema, + transactionSchema: config?.customTransactionSchema as unknown as CustomTransactionSchema, }); return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase); @@ -88,7 +88,7 @@ export const signTransaction = async ( config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: config?.customTransactionSchema as CustomTransactionSchema, + transactionSchema: config?.customTransactionSchema as unknown as CustomTransactionSchema, }); return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase); From 5c8f35e0775c275dd0b7a45fde6b7140e9e2a881 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Tue, 17 Sep 2024 08:58:17 +0200 Subject: [PATCH 16/16] fix: CustomTransactionSchema type --- packages/web3-core/src/types.ts | 6 ++++++ packages/web3-core/src/web3_config.ts | 9 ++++----- .../web3-core/test/unit/web3_context.test.ts | 3 --- .../src/rpc_method_wrappers.ts | 6 +++--- packages/web3-eth/src/rpc_method_wrappers.ts | 19 +++++++++---------- packages/web3-eth/src/types.ts | 3 +-- .../utils/prepare_transaction_for_signing.ts | 6 ++---- .../test/unit/format_transaction.test.ts | 2 +- 8 files changed, 26 insertions(+), 28 deletions(-) diff --git a/packages/web3-core/src/types.ts b/packages/web3-core/src/types.ts index b2b655b351a..b6b7d402584 100644 --- a/packages/web3-core/src/types.ts +++ b/packages/web3-core/src/types.ts @@ -23,6 +23,7 @@ import { Web3APIMethod, Web3APIReturnType, } from 'web3-types'; +import { Schema } from 'web3-validator'; export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined; @@ -50,3 +51,8 @@ export interface RequestManagerMiddleware { options?: { [key: string]: unknown }, ): Promise>; } + +export type CustomTransactionSchema = { + type: string; + properties: Record; +}; diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index 274df6d5d6c..ee397ea9daf 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -25,8 +25,7 @@ import { } from 'web3-types'; import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; import { isNullish, toHex } from 'web3-utils'; -import { Schema } from 'web3-validator'; -import { TransactionTypeParser } from './types.js'; +import { CustomTransactionSchema, TransactionTypeParser } from './types.js'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context.js'; import { Web3EventEmitter } from './web3_event_emitter.js'; @@ -60,7 +59,7 @@ export interface Web3ConfigOptions { }; transactionBuilder?: TransactionBuilder; transactionTypeParser?: TransactionTypeParser; - customTransactionSchema?: Record; + customTransactionSchema?: CustomTransactionSchema; defaultReturnFormat: DataFormat; } @@ -523,11 +522,11 @@ export abstract class Web3Config this.config.transactionTypeParser = val; } - public get customTransactionSchema(): Record | undefined { + public get customTransactionSchema(): CustomTransactionSchema | undefined { return this.config.customTransactionSchema; } - public set customTransactionSchema(schema: Record | undefined) { + public set customTransactionSchema(schema: CustomTransactionSchema | undefined) { this._triggerConfigChange('customTransactionSchema', schema); this.config.customTransactionSchema = schema; } diff --git a/packages/web3-core/test/unit/web3_context.test.ts b/packages/web3-core/test/unit/web3_context.test.ts index 6b3d211f2c6..b50c98e8661 100644 --- a/packages/web3-core/test/unit/web3_context.test.ts +++ b/packages/web3-core/test/unit/web3_context.test.ts @@ -124,9 +124,6 @@ describe('Web3Context', () => { expect(newContext.requestManager).toBeInstanceOf(Web3RequestManager); expect(newContext.config.defaultHardfork).toEqual(config.defaultHardfork); expect(newContext.config.defaultNetworkId).toEqual(config.defaultNetworkId); - expect(newContext.requestManager.config.defaultHardfork).toEqual( - config.defaultHardfork, - ); }); describe('accountsProvider', () => { diff --git a/packages/web3-eth-personal/src/rpc_method_wrappers.ts b/packages/web3-eth-personal/src/rpc_method_wrappers.ts index 9ad816bbc8d..451b15074d9 100644 --- a/packages/web3-eth-personal/src/rpc_method_wrappers.ts +++ b/packages/web3-eth-personal/src/rpc_method_wrappers.ts @@ -16,7 +16,7 @@ along with web3.js. If not, see . */ import { Web3RequestManager, Web3ConfigOptions } from 'web3-core'; import { toChecksumAddress, utf8ToHex } from 'web3-utils'; -import { formatTransaction, type CustomTransactionSchema } from 'web3-eth'; +import { formatTransaction } from 'web3-eth'; import { Address, EthPersonalAPI, ETH_DATA_FORMAT, HexString, Transaction } from 'web3-types'; import { validator, isHexStrict } from 'web3-validator'; import { personalRpcMethods } from 'web3-rpc-methods'; @@ -75,7 +75,7 @@ export const sendTransaction = async ( config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: config?.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: config?.customTransactionSchema, }); return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase); @@ -88,7 +88,7 @@ export const signTransaction = async ( config?: Web3ConfigOptions, ) => { const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, { - transactionSchema: config?.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: config?.customTransactionSchema, }); return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase); diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index aec142ec741..9931c56364f 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -66,7 +66,6 @@ import { SignatureObjectSchema, } from './schemas.js'; import { - type CustomTransactionSchema, SendSignedTransactionEvents, SendSignedTransactionOptions, SendTransactionEvents, @@ -430,7 +429,7 @@ export async function getTransaction( return isNullish(response) ? response : formatTransaction(response, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }); } @@ -450,7 +449,7 @@ export async function getPendingTransactions( transaction as unknown as Transaction, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }, ), @@ -491,7 +490,7 @@ export async function getTransactionFromBlock( return isNullish(response) ? response : formatTransaction(response, returnFormat ?? web3Context.defaultReturnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }); } @@ -611,7 +610,7 @@ export function sendTransaction< }, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }, ); @@ -855,7 +854,7 @@ export async function signTransaction( const response = await ethRpcMethods.signTransaction( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }), ); // Some clients only return the encoded signed transaction (e.g. Ganache) @@ -871,7 +870,7 @@ export async function signTransaction( returnFormat, ), tx: formatTransaction((response as SignedTransactionInfoAPI).tx, returnFormat, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, fillInputAndData: true, }), }; @@ -896,7 +895,7 @@ export async function call( const response = await ethRpcMethods.call( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }), blockNumberFormatted, ); @@ -916,7 +915,7 @@ export async function estimateGas( returnFormat: ReturnFormat, ) { const transactionFormatted = formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }); const blockNumberFormatted = isBlockTag(blockNumber as string) ? (blockNumber as BlockTag) @@ -1089,7 +1088,7 @@ export async function createAccessList( const response = (await ethRpcMethods.createAccessList( web3Context.requestManager, formatTransaction(transaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }), blockNumberFormatted, )) as unknown as AccessListResult; diff --git a/packages/web3-eth/src/types.ts b/packages/web3-eth/src/types.ts index abe0cc9bca3..cc3c86cd2ee 100644 --- a/packages/web3-eth/src/types.ts +++ b/packages/web3-eth/src/types.ts @@ -38,7 +38,6 @@ import { TransactionWithToLocalWalletIndex, } from 'web3-types'; import { Schema } from 'web3-validator'; -import { transactionSchema } from './schemas.js'; export type InternalTransaction = FormatType; @@ -110,5 +109,5 @@ export interface TransactionMiddleware { export type CustomTransactionSchema = { type: string; - properties: typeof transactionSchema['properties'] & Record; + properties: Record; }; diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index e47a18dad39..cd70e1e265c 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -33,7 +33,6 @@ import { isNullish } from 'web3-validator'; import { validateTransactionForSigning } from '../validation.js'; import { formatTransaction } from './format_transaction.js'; import { transactionBuilder } from './transaction_builder.js'; -import { type CustomTransactionSchema } from '../types.js'; const getEthereumjsTxDataFromTransaction = ( transaction: FormatType, @@ -136,15 +135,14 @@ export const prepareTransactionForSigning = async ( fillGasLimit, })) as unknown as PopulatedUnsignedTransaction; const formattedTransaction = formatTransaction(populatedTransaction, ETH_DATA_FORMAT, { - transactionSchema: web3Context.config.customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }) as unknown as FormatType; validateTransactionForSigning( formattedTransaction as unknown as FormatType, undefined, { - transactionSchema: web3Context.config - .customTransactionSchema as unknown as CustomTransactionSchema, + transactionSchema: web3Context.config.customTransactionSchema, }, ); diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index 2a13c10b561..d3c86c077bd 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -132,7 +132,7 @@ describe('formatTransaction', () => { undefined, { transactionSchema: { - ...transactionSchema, + type: 'object', properties: { ...transactionSchema.properties, feeCurrency: { format: 'address' },