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..8267868c616 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', + }, + }, + }), + ); }); });