Skip to content

Commit

Permalink
tests: initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Aug 30, 2024
1 parent bc20d27 commit 8abb6e7
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
11 changes: 10 additions & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<K extends keyof Web3ConfigOptions>(
config: K,
newValue: Web3ConfigOptions[K],
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class Web3Context<
isSupportedProvider(providerOrContext as SupportedProviders<API>)
) {
this._requestManager = new Web3RequestManager<API>(
{},
providerOrContext as undefined | string | SupportedProviders<API>,
);
this._subscriptionManager = new Web3SubscriptionManager(
Expand All @@ -146,7 +145,7 @@ export class Web3Context<
this._requestManager =
requestManager ??
new Web3RequestManager<API>(
this.config,
config,
provider,
config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout,
requestManagerMiddleware,
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ export class Web3RequestManager<
}> {
private _provider?: SupportedProviders<API>;
private readonly useRpcCallSpecification?: boolean;
public config: Web3ConfigOptions;
public config: Partial<Web3ConfigOptions>;
public middleware?: RequestManagerMiddleware<API>;

public constructor(
config: Web3ConfigOptions,
provider?: SupportedProviders<API> | string,
useRpcCallSpecification?: boolean,
requestManagerMiddleware?: RequestManagerMiddleware<API>,
config?: Partial<Web3ConfigOptions>,
) {
super();

this.config = config;
this.config = config ?? {};

if (!isNullish(provider)) {
this.setProvider(provider);
Expand Down
1 change: 1 addition & 0 deletions packages/web3-core/test/unit/web3_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const defaultConfig = {
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
transactionBuilder: undefined,
transactionTypeParser: undefined,
customTransactionSchema: undefined,
};
const setValue = {
string: 'newValue',
Expand Down
3 changes: 3 additions & 0 deletions packages/web3-core/test/unit/web3_context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
33 changes: 33 additions & 0 deletions packages/web3-eth/test/fixtures/format_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 15 additions & 2 deletions packages/web3-eth/test/unit/format_transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, Record<string, unknown>> = {
[FMT_NUMBER.BIGINT]: numbersAsBigIntTransaction,
Expand Down Expand Up @@ -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',
},
},
}),
);
});
});

0 comments on commit 8abb6e7

Please sign in to comment.