Skip to content

Commit 5417f71

Browse files
Alexdkurukula
authored andcommitted
fix web3config setconfig (web3#6555)
* add web3config * fix config to emit properly * fix lint * fix testcases and change emit for contracts * fix lint * remove unneccesary code * update tests * remove test * update changelog and format * update changelog * fix contracts creation in web3 package * fix linter * format * format * add validation * add test and address feedback * update changelog * update tests * update * fix tests * update test * update time * add promises to fix parallel tests * revert
1 parent b4c3196 commit 5417f71

File tree

15 files changed

+388
-65
lines changed

15 files changed

+388
-65
lines changed

packages/web3-core/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ Documentation:
193193

194194
## [4.3.1]
195195

196+
### Fixed
197+
198+
- Fix `Web3Config` to properly update within other web3 packages when `setConfig` is used (#6555)
199+
196200
### Added
197201

198202
- Added `isMetaMaskProvider` function to check if provider is metamask (#6534)
199203

200-
## [Unreleased]
204+
## [Unreleased]

packages/web3-core/src/web3_config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ export abstract class Web3Config
9797

9898
public constructor(options?: Partial<Web3ConfigOptions>) {
9999
super();
100-
101100
this.setConfig(options ?? {});
102101
}
103102

104103
public setConfig(options: Partial<Web3ConfigOptions>) {
105104
// TODO: Improve and add key check
105+
const keys = Object.keys(options) as (keyof Web3ConfigOptions)[];
106+
for (const key of keys) {
107+
this._triggerConfigChange(key, options[key])
108+
}
106109
Object.assign(this.config, options);
107110
}
108111

packages/web3-core/src/web3_context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class Web3Context<
108108
| Web3ContextInitOptions<API, RegisteredSubs>,
109109
) {
110110
super();
111-
111+
112112
// If "providerOrContext" is provided as "string" or an objects matching "SupportedProviders" interface
113113
if (
114114
isNullish(providerOrContext) ||

packages/web3-eth-contract/src/contract.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ export class Contract<Abi extends ContractAbi>
241241
* RPC provider when using contract methods.
242242
* Default is `input`
243243
*/
244-
private readonly _dataInputFill?: 'data' | 'input' | 'both';
245244

246245
private context?: Web3Context;
247246
/**
@@ -369,17 +368,11 @@ export class Contract<Abi extends ContractAbi>
369368
: isDataFormat(optionsOrContextOrReturnFormat)
370369
? optionsOrContextOrReturnFormat
371370
: returnFormat ?? DEFAULT_RETURN_FORMAT;
372-
373371
const address =
374372
typeof addressOrOptionsOrContext === 'string' ? addressOrOptionsOrContext : undefined;
375-
376-
if (this.config.contractDataInputFill === 'both') {
377-
this._dataInputFill = this.config.contractDataInputFill;
378-
} else {
379-
this._dataInputFill =
373+
this.config.contractDataInputFill =
380374
(options as ContractInitOptions)?.dataInputFill ??
381375
this.config.contractDataInputFill;
382-
}
383376
this._parseAndSetJsonInterface(jsonInterface, returnDataFormat);
384377

385378
if (!isNullish(address)) {
@@ -409,6 +402,13 @@ export class Contract<Abi extends ContractAbi>
409402
set: (value: ContractAbi) => this._parseAndSetJsonInterface(value, returnDataFormat),
410403
get: () => this._jsonInterface,
411404
});
405+
406+
if (contractContext instanceof Web3Context) {
407+
contractContext.on(Web3ConfigEvent.CONFIG_CHANGE, event => {
408+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
409+
this.setConfig({ [event.name]: event.newValue });
410+
});
411+
}
412412
}
413413

414414
/**
@@ -501,7 +501,7 @@ export class Contract<Abi extends ContractAbi>
501501
data: this.options.data,
502502
provider: this.currentProvider,
503503
syncWithContext: this.syncWithContext,
504-
dataInputFill: this._dataInputFill,
504+
dataInputFill: this.config.contractDataInputFill,
505505
},
506506
this.getContextObject(),
507507
);
@@ -516,7 +516,7 @@ export class Contract<Abi extends ContractAbi>
516516
data: this.options.data,
517517
provider: this.currentProvider,
518518
syncWithContext: this.syncWithContext,
519-
dataInputFill: this._dataInputFill,
519+
dataInputFill: this.config.contractDataInputFill,
520520
},
521521
this.getContextObject(),
522522
);
@@ -1014,7 +1014,7 @@ export class Contract<Abi extends ContractAbi>
10141014
params,
10151015
options: {
10161016
...options,
1017-
dataInputFill: this._dataInputFill,
1017+
dataInputFill: this.config.contractDataInputFill,
10181018
},
10191019
contractOptions: {
10201020
...this.options,
@@ -1088,7 +1088,7 @@ export class Contract<Abi extends ContractAbi>
10881088
checkRevertBeforeSending: false,
10891089
contractAbi: this._jsonInterface,
10901090
});
1091-
1091+
10921092
// eslint-disable-next-line no-void
10931093
void transactionToSend.on('error', (error: unknown) => {
10941094
if (error instanceof ContractExecutionError) {

packages/web3-eth-contract/src/utils.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ import {
2525
Address,
2626
NonPayableCallOptions,
2727
PayableCallOptions,
28-
ContractInitOptions,
2928
ContractOptions,
3029
} from 'web3-types';
31-
import { isNullish, mergeDeep } from 'web3-utils';
30+
import { isNullish, mergeDeep, isContractInitOptions } from 'web3-utils';
3231
import { encodeMethodABI } from './encoding.js';
3332
import { Web3ContractContext } from './types.js';
3433

@@ -165,24 +164,11 @@ export const getEstimateGasParams = ({
165164
return txParams as TransactionWithSenderAPI;
166165
};
167166

168-
export const isContractInitOptions = (options: unknown): options is ContractInitOptions =>
169-
typeof options === 'object' &&
170-
!isNullish(options) &&
171-
[
172-
'input',
173-
'data',
174-
'from',
175-
'gas',
176-
'gasPrice',
177-
'gasLimit',
178-
'address',
179-
'jsonInterface',
180-
'syncWithContext',
181-
'dataInputFill',
182-
].some(key => key in options);
167+
export { isContractInitOptions } from 'web3-utils';
183168

184169
export const isWeb3ContractContext = (options: unknown): options is Web3ContractContext =>
185-
typeof options === 'object' && !isNullish(options) && !isContractInitOptions(options);
170+
typeof options === 'object' && !isNullish(options) &&
171+
Object.keys(options).length !== 0 && !isContractInitOptions(options);
186172

187173
export const getCreateAccessListParams = ({
188174
abi,

packages/web3-eth/src/rpc_method_wrappers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export function sendTransaction<
507507
},
508508
ETH_DATA_FORMAT,
509509
);
510-
510+
511511
try {
512512
transactionFormatted = await sendTxHelper.populateGasPrice({
513513
transaction,

packages/web3-eth/src/utils/prepare_transaction_for_signing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export const prepareTransactionForSigning = async (
141141
validateTransactionForSigning(
142142
formattedTransaction as unknown as FormatType<Transaction, typeof ETH_DATA_FORMAT>,
143143
);
144-
144+
145145
return TransactionFactory.fromTxData(
146146
getEthereumjsTxDataFromTransaction(formattedTransaction),
147147
getEthereumjsTransactionOptions(formattedTransaction, web3Context),

packages/web3-eth/src/web3_eth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
9595
super({
9696
...(providerOrContext as Web3ContextInitOptions),
9797
registeredSubscriptions,
98-
});
98+
});
9999
}
100100

101101
/**

packages/web3-utils/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,8 @@ Documentation:
169169
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
170170
- Fixed bug in chunks processing logic (#6496)
171171

172-
## [Unreleased]
172+
## [Unreleased]
173+
174+
### Added
175+
176+
- Add `isContractInitOptions` method (#6455)

packages/web3-utils/src/validation.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
isTopicInBloom as isTopicInBloomValidator,
3131
isUserEthereumAddressInBloom as isUserEthereumAddressInBloomValidator,
3232
} from 'web3-validator';
33-
import { BlockNumberOrTag, BlockTags } from 'web3-types';
33+
import { BlockNumberOrTag, BlockTags, ContractInitOptions } from 'web3-types';
3434

3535
/**
3636
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
@@ -169,4 +169,22 @@ export const compareBlockNumbers = (blockA: BlockNumberOrTag, blockB: BlockNumbe
169169
return 1;
170170
};
171171

172+
173+
export const isContractInitOptions = (options: unknown): options is ContractInitOptions =>
174+
typeof options === 'object' &&
175+
!isNullishValidator(options) &&
176+
Object.keys(options).length !== 0 &&
177+
[
178+
'input',
179+
'data',
180+
'from',
181+
'gas',
182+
'gasPrice',
183+
'gasLimit',
184+
'address',
185+
'jsonInterface',
186+
'syncWithContext',
187+
'dataInputFill',
188+
].some(key => key in options);
189+
172190
export const isNullish = isNullishValidator;

0 commit comments

Comments
 (0)