Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix web3config setconfig #6555

Merged
merged 29 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ Documentation:

## [4.3.1]

### Fixed

- Fix `Web3Config` to properly update within other web3 packages when `setConfig` is used (#6555)

### Added

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

## [Unreleased]
## [Unreleased]
5 changes: 4 additions & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ export abstract class Web3Config

public constructor(options?: Partial<Web3ConfigOptions>) {
super();

this.setConfig(options ?? {});
}

public setConfig(options: Partial<Web3ConfigOptions>) {
// TODO: Improve and add key check
const keys = Object.keys(options) as (keyof Web3ConfigOptions)[];
for (const key of keys) {
this._triggerConfigChange(key, options[key])
}
Comment on lines +105 to +108
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you, please, mention this in the CHANGELOG.md.

Object.assign(this.config, options);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class Web3Context<
| Web3ContextInitOptions<API, RegisteredSubs>,
) {
super();

// If "providerOrContext" is provided as "string" or an objects matching "SupportedProviders" interface
if (
isNullish(providerOrContext) ||
Expand Down
24 changes: 12 additions & 12 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ export class Contract<Abi extends ContractAbi>
* RPC provider when using contract methods.
* Default is `input`
*/
private readonly _dataInputFill?: 'data' | 'input' | 'both';

private context?: Web3Context;
/**
Expand Down Expand Up @@ -369,17 +368,11 @@ export class Contract<Abi extends ContractAbi>
: isDataFormat(optionsOrContextOrReturnFormat)
? optionsOrContextOrReturnFormat
: returnFormat ?? DEFAULT_RETURN_FORMAT;

const address =
typeof addressOrOptionsOrContext === 'string' ? addressOrOptionsOrContext : undefined;

if (this.config.contractDataInputFill === 'both') {
this._dataInputFill = this.config.contractDataInputFill;
} else {
this._dataInputFill =
this.config.contractDataInputFill =
(options as ContractInitOptions)?.dataInputFill ??
this.config.contractDataInputFill;
}
this._parseAndSetJsonInterface(jsonInterface, returnDataFormat);

if (!isNullish(address)) {
Expand Down Expand Up @@ -409,6 +402,13 @@ export class Contract<Abi extends ContractAbi>
set: (value: ContractAbi) => this._parseAndSetJsonInterface(value, returnDataFormat),
get: () => this._jsonInterface,
});

if (contractContext instanceof Web3Context) {
contractContext.on(Web3ConfigEvent.CONFIG_CHANGE, event => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.setConfig({ [event.name]: event.newValue });
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
});
}
Comment on lines +406 to +411
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you, please, update the CHANGELOG.md?

}

/**
Expand Down Expand Up @@ -501,7 +501,7 @@ export class Contract<Abi extends ContractAbi>
data: this.options.data,
provider: this.currentProvider,
syncWithContext: this.syncWithContext,
dataInputFill: this._dataInputFill,
dataInputFill: this.config.contractDataInputFill,
},
this.getContextObject(),
);
Expand All @@ -516,7 +516,7 @@ export class Contract<Abi extends ContractAbi>
data: this.options.data,
provider: this.currentProvider,
syncWithContext: this.syncWithContext,
dataInputFill: this._dataInputFill,
dataInputFill: this.config.contractDataInputFill,
},
this.getContextObject(),
);
Expand Down Expand Up @@ -1014,7 +1014,7 @@ export class Contract<Abi extends ContractAbi>
params,
options: {
...options,
dataInputFill: this._dataInputFill,
dataInputFill: this.config.contractDataInputFill,
},
contractOptions: {
...this.options,
Expand Down Expand Up @@ -1088,7 +1088,7 @@ export class Contract<Abi extends ContractAbi>
checkRevertBeforeSending: false,
contractAbi: this._jsonInterface,
});

// eslint-disable-next-line no-void
void transactionToSend.on('error', (error: unknown) => {
if (error instanceof ContractExecutionError) {
Expand Down
22 changes: 4 additions & 18 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import {
Address,
NonPayableCallOptions,
PayableCallOptions,
ContractInitOptions,
ContractOptions,
} from 'web3-types';
import { isNullish, mergeDeep } from 'web3-utils';
import { isNullish, mergeDeep, isContractInitOptions } from 'web3-utils';
import { encodeMethodABI } from './encoding.js';
import { Web3ContractContext } from './types.js';

Expand Down Expand Up @@ -165,24 +164,11 @@ export const getEstimateGasParams = ({
return txParams as TransactionWithSenderAPI;
};

export const isContractInitOptions = (options: unknown): options is ContractInitOptions =>
typeof options === 'object' &&
!isNullish(options) &&
[
'input',
'data',
'from',
'gas',
'gasPrice',
'gasLimit',
'address',
'jsonInterface',
'syncWithContext',
'dataInputFill',
].some(key => key in options);
export { isContractInitOptions } from 'web3-utils';

export const isWeb3ContractContext = (options: unknown): options is Web3ContractContext =>
typeof options === 'object' && !isNullish(options) && !isContractInitOptions(options);
typeof options === 'object' && !isNullish(options) &&
Object.keys(options).length !== 0 && !isContractInitOptions(options);

export const getCreateAccessListParams = ({
abi,
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ export function sendTransaction<
},
ETH_DATA_FORMAT,
);

luu-alex marked this conversation as resolved.
Show resolved Hide resolved
try {
transactionFormatted = await sendTxHelper.populateGasPrice({
transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const prepareTransactionForSigning = async (
validateTransactionForSigning(
formattedTransaction as unknown as FormatType<Transaction, typeof ETH_DATA_FORMAT>,
);

return TransactionFactory.fromTxData(
getEthereumjsTxDataFromTransaction(formattedTransaction),
getEthereumjsTransactionOptions(formattedTransaction, web3Context),
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/web3_eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
super({
...(providerOrContext as Web3ContextInitOptions),
registeredSubscriptions,
});
});
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,8 @@ Documentation:
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
- Fixed bug in chunks processing logic (#6496)

## [Unreleased]
## [Unreleased]

### Added

- Add `isContractInitOptions` method (#6455)
20 changes: 19 additions & 1 deletion packages/web3-utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
isTopicInBloom as isTopicInBloomValidator,
isUserEthereumAddressInBloom as isUserEthereumAddressInBloomValidator,
} from 'web3-validator';
import { BlockNumberOrTag, BlockTags } from 'web3-types';
import { BlockNumberOrTag, BlockTags, ContractInitOptions } from 'web3-types';

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


export const isContractInitOptions = (options: unknown): options is ContractInitOptions =>
typeof options === 'object' &&
!isNullishValidator(options) &&
Object.keys(options).length !== 0 &&
[
'input',
'data',
'from',
'gas',
'gasPrice',
'gasLimit',
'address',
'jsonInterface',
'syncWithContext',
'dataInputFill',
].some(key => key in options);

export const isNullish = isNullishValidator;
17 changes: 16 additions & 1 deletion packages/web3-utils/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Numbers } from 'web3-types';
import { Numbers, ContractInitOptions } from 'web3-types';
import { InvalidBlockError } from 'web3-errors';

export const compareBlockNumbersValidData: [[Numbers, Numbers], number][] = [
Expand Down Expand Up @@ -77,3 +77,18 @@ export const isBloomValidData: [any, true][] = [
true,
],
];

export const isContractInitValidData: ContractInitOptions[] = [
{dataInputFill: "data"},
{syncWithContext: true},
{gas: "100000",
syncWithContext: true,
dataInputFill: "data",
},
];

export const isContractInitInvalidData: unknown[] = [
"",
12,
{}
];
20 changes: 19 additions & 1 deletion packages/web3-utils/test/unit/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
/* eslint-disable jest/no-conditional-expect */

import { InvalidBlockError } from 'web3-errors';
import { compareBlockNumbers } from '../../src/validation';
import { compareBlockNumbers, isContractInitOptions } from '../../src/validation';
import {
compareBlockNumbersInvalidData,
compareBlockNumbersValidData,
isContractInitValidData,
isContractInitInvalidData
} from '../fixtures/validation';

describe('validation', () => {
Expand All @@ -37,4 +39,20 @@ describe('validation', () => {
},
);
});
describe('isContractInit', () => {
describe('should return true', () => {
it.each([...isContractInitValidData])(
'%s', (input) => {
expect(isContractInitOptions(input)).toBe(true);
}
)
});
describe('should return false', () => {
it.each([...isContractInitInvalidData])(
'%s', (input) => {
expect(isContractInitOptions(input)).toBe(false);
}
)
});
});
});
Loading
Loading