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

Nikos/5603/some default properties cannot set from contract class #5756

Merged
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9a55515
Create getters & setters for static defaults properties
nikoulai Jan 13, 2023
5b076e3
Add web3config static variable to initialize web3Context config
nikoulai Jan 13, 2023
67db632
prioritize instance's value
nikoulai Jan 13, 2023
d824c0b
Update tests
nikoulai Jan 13, 2023
c39de80
Enable optional static variables
nikoulai Jan 16, 2023
3af00ea
Keep only contractWeb3Config for static defaults
nikoulai Jan 24, 2023
e89e5dc
Formatting
nikoulai Jan 24, 2023
682932d
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Jan 24, 2023
f409094
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Jan 24, 2023
ca39898
Enable contract instances to listen on global config event changes
nikoulai Jan 27, 2023
56005cb
Add tests
nikoulai Jan 27, 2023
e341b1c
Remove comment
nikoulai Jan 27, 2023
241a520
Fix global defaults on deployed smart contracts
nikoulai Jan 27, 2023
e8a4aeb
Fix clone function
nikoulai Jan 30, 2023
e1d35e0
Fix defaultAccount inheritance from class level during contract deplo…
nikoulai Jan 30, 2023
977854b
Update tests
nikoulai Jan 30, 2023
e81f725
Update test
nikoulai Jan 30, 2023
6b464e0
Update test
nikoulai Jan 30, 2023
4213f9d
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Jan 30, 2023
5fc372c
Delete unused test
nikoulai Jan 30, 2023
24a9513
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Jan 30, 2023
9c147f5
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Feb 1, 2023
85929ef
Subscribe for global config changes in contract class
nikoulai Feb 1, 2023
3945f44
Remove static contract defaults
nikoulai Feb 1, 2023
ae4dca4
Remove tests using static contract defaults
nikoulai Feb 1, 2023
b05402b
Fix failing test
nikoulai Feb 1, 2023
1a834f2
Fix global defaults on smart contracts
nikoulai Feb 2, 2023
47eb05d
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Feb 2, 2023
2682573
Address feedback
nikoulai Feb 2, 2023
f7647da
Remove unused import
nikoulai Feb 2, 2023
a610501
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Feb 2, 2023
f2d1b58
fix
nikoulai Feb 2, 2023
54dc58e
Remove formatting error
nikoulai Feb 3, 2023
d8469bd
move syncWithContext in contract init options
nikoulai Feb 3, 2023
ca19503
move syncWithContext in contract init options
nikoulai Feb 3, 2023
d550ef8
Fix and test
nikoulai Feb 3, 2023
e0daec2
Update changelog
nikoulai Feb 3, 2023
a6c6f2d
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
nikoulai Feb 3, 2023
0815578
Merge branch '4.x' into nikos/5603/some-default-properties-cannot-set…
spacesailor24 Feb 7, 2023
d5483a0
Update packages/web3-eth-contract/CHANGELOG.md
spacesailor24 Feb 7, 2023
d8c8250
Update packages/web3-eth-contract/CHANGELOG.md
spacesailor24 Feb 7, 2023
2ca8725
Remove comment
nikoulai Feb 7, 2023
6c75f77
Merge branch 'nikos/5603/some-default-properties-cannot-set-from-Cont…
nikoulai Feb 7, 2023
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
7 changes: 6 additions & 1 deletion packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,13 @@ const transactionHash = receipt.transactionHash;

## [Unreleased]

### Changed
### Fixed

- Fix contract defaults (#5756)

### Fixed

- Fix contract defaults (#5756)
spacesailor24 marked this conversation as resolved.
Show resolved Hide resolved
- Update imports statements for objects that was moved between web3 packages (#5771)

### Added
Expand Down
242 changes: 40 additions & 202 deletions packages/web3-eth-contract/src/contract.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 { Web3Context, Web3EventEmitter, Web3PromiEvent } from 'web3-core';
import { Web3Context, Web3EventEmitter, Web3PromiEvent, Web3ConfigEvent } from 'web3-core';
import { ContractExecutionError, SubscriptionError, Web3ContractError } from 'web3-errors';
import {
createAccessList,
Expand Down Expand Up @@ -57,7 +57,6 @@ import {
HexString,
LogsInput,
Mutable,
Common,
} from 'web3-types';
import {
DataFormat,
Expand Down Expand Up @@ -213,69 +212,9 @@ export class Contract<Abi extends ContractAbi>
public readonly options: ContractOptions;

/**
* Can be used to set {@link Contract.defaultAccount} for all contracts.
* Set to true if you want contracts' defaults to sync with global defaults.
*/
public static defaultAccount?: HexString;

/**
* Can be used to set {@link Contract.defaultBlock} for all contracts.
*/
public static defaultBlock?: BlockNumberOrTag;

/**
* Can be used to set {@link Contract.defaultHardfork} for all contracts.
*/
public static defaultHardfork?: string;

/**
* Can be used to set {@link Contract.defaultCommon} for all contracts.
*/
public static defaultCommon?: Common;

/**
* Can be used to set {@link Contract.transactionSendTimeout} for all contracts.
*/
public static transactionSendTimeout?: number;

/**
* Can be used to set {@link Contract.transactionBlockTimeout} for all contracts.
*/
public static transactionBlockTimeout?: number;

/**
* Can be used to set {@link Contract.transactionConfirmationBlocks} for all contracts.
*/
public static transactionConfirmationBlocks?: number;

/**
* Can be used to set {@link Contract.transactionPollingInterval} for all contracts.
*/
public static transactionPollingInterval?: number;

/**
* Can be used to set {@link Contract.transactionPollingTimeout} for all contracts.
*/
public static transactionPollingTimeout?: number;

/**
* Can be used to set {@link Contract.transactionReceiptPollingInterval} for all contracts.
*/
public static transactionReceiptPollingInterval?: number;

/**
* Can be used to set {@link Contract.transactionConfirmationPollingInterval} for all contracts.
*/
public static transactionConfirmationPollingInterval?: number;

/**
* Can be used to set {@link Contract.blockHeaderTimeout} for all contracts.
*/
public static blockHeaderTimeout?: number;

/**
* Can be used to set {@link Contract.handleRevert} for all contracts.
*/
public static handleRevert?: boolean;
public syncWithContext = false;

private _errorsInterface!: AbiErrorFragment[];
private _jsonInterface!: ContractAbiWithSignature;
Expand All @@ -292,6 +231,7 @@ export class Contract<Abi extends ContractAbi>
private _methods!: ContractMethodsInterface<Abi>;
private _events!: ContractEventsInterface<Abi>;

private context?: Web3Context;
/**
* Creates a new contract instance with all its methods and events defined in its {@doclink glossary/json_interface | json interface} object.
*
Expand Down Expand Up @@ -420,6 +360,8 @@ export class Contract<Abi extends ContractAbi>
data: options?.data,
};

this.syncWithContext = (options as ContractInitOptions)?.syncWithContext ?? false;

Object.defineProperty(this.options, 'address', {
set: (value: Address) => this._parseAndSetAddress(value, returnDataFormat),
get: () => this._address,
Expand All @@ -431,131 +373,6 @@ export class Contract<Abi extends ContractAbi>
});
}

public get defaultAccount() {
return (this.constructor as typeof Contract).defaultAccount ?? super.defaultAccount;
}

public set defaultAccount(value: Address | undefined) {
super.defaultAccount = value;
}

public get defaultBlock() {
return (this.constructor as typeof Contract).defaultBlock ?? super.defaultBlock;
}

public set defaultBlock(value: BlockNumberOrTag) {
super.defaultBlock = value;
}

public get defaultHardfork() {
return (this.constructor as typeof Contract).defaultHardfork ?? super.defaultHardfork;
}

public set defaultHardfork(value: string) {
super.defaultHardfork = value;
}

public get defaultCommon(): Common | undefined {
return (this.constructor as typeof Contract).defaultCommon ?? super.defaultCommon;
}

public set defaultCommon(value: Common | undefined) {
super.defaultCommon = value;
}

public get transactionSendTimeout() {
return (
(this.constructor as typeof Contract).transactionSendTimeout ??
super.transactionSendTimeout
);
}

public set transactionSendTimeout(value: number) {
super.transactionSendTimeout = value;
}

public get transactionBlockTimeout() {
return (
(this.constructor as typeof Contract).transactionBlockTimeout ??
super.transactionBlockTimeout
);
}

public set transactionBlockTimeout(value: number) {
super.transactionBlockTimeout = value;
}

public get transactionConfirmationBlocks() {
return (
(this.constructor as typeof Contract).transactionConfirmationBlocks ??
super.transactionConfirmationBlocks
);
}

public set transactionConfirmationBlocks(value: number) {
super.transactionConfirmationBlocks = value;
}

public get transactionPollingInterval() {
return (
(this.constructor as typeof Contract).transactionPollingInterval ??
super.transactionPollingInterval
);
}

public set transactionPollingInterval(value: number) {
super.transactionPollingInterval = value;
}

public get transactionPollingTimeout() {
return (
(this.constructor as typeof Contract).transactionPollingTimeout ??
super.transactionPollingTimeout
);
}

public set transactionPollingTimeout(value: number) {
super.transactionPollingTimeout = value;
}

public get transactionReceiptPollingInterval() {
return (
(this.constructor as typeof Contract).transactionReceiptPollingInterval ??
super.transactionReceiptPollingInterval
);
}

public set transactionReceiptPollingInterval(value: number | undefined) {
super.transactionReceiptPollingInterval = value;
}

public get transactionConfirmationPollingInterval() {
return (
(this.constructor as typeof Contract).transactionConfirmationPollingInterval ??
super.transactionConfirmationPollingInterval
);
}

public set transactionConfirmationPollingInterval(value: number | undefined) {
super.transactionConfirmationPollingInterval = value;
}

public get blockHeaderTimeout() {
return (this.constructor as typeof Contract).blockHeaderTimeout ?? super.blockHeaderTimeout;
}

public set blockHeaderTimeout(value: number) {
super.blockHeaderTimeout = value;
}

public get handleRevert() {
return (this.constructor as typeof Contract).handleRevert ?? super.handleRevert;
}

public set handleRevert(value: boolean) {
super.handleRevert = value;
}

/**
* Subscribe to an event.
*
Expand Down Expand Up @@ -633,8 +450,10 @@ export class Contract<Abi extends ContractAbi>
* ```
*/
public clone() {
let newContract: Contract<any>;

if (this.options.address) {
return new Contract<Abi>(
newContract = new Contract<Abi>(
[...this._jsonInterface, ...this._errorsInterface] as unknown as Abi,
this.options.address,
{
Expand All @@ -644,23 +463,28 @@ export class Contract<Abi extends ContractAbi>
from: this.options.from,
data: this.options.data,
provider: this.currentProvider,
syncWithContext: this.syncWithContext,
},
this.getContextObject(),
);
} else {
newContract = new Contract<Abi>(
[...this._jsonInterface, ...this._errorsInterface] as unknown as Abi,
{
gas: this.options.gas,
gasPrice: this.options.gasPrice,
gasLimit: this.options.gasLimit,
from: this.options.from,
data: this.options.data,
provider: this.currentProvider,
syncWithContext: this.syncWithContext,
},
this.getContextObject(),
);
}
if (this.context) newContract.subscribeToContextEvents(this.context);

return new Contract<Abi>(
[...this._jsonInterface, ...this._errorsInterface] as unknown as Abi,
{
gas: this.options.gas,
gasPrice: this.options.gasPrice,
gasLimit: this.options.gasLimit,
from: this.options.from,
data: this.options.data,
provider: this.currentProvider,
},
this.getContextObject(),
);
return newContract;
}

/**
Expand Down Expand Up @@ -772,6 +596,7 @@ export class Contract<Abi extends ContractAbi>
// modifiedOptions.to = '0x0000000000000000000000000000000000000000';
delete modifiedOptions.to;

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._contractMethodDeploySend(
abi as AbiFunctionFragment,
args as unknown[],
Expand Down Expand Up @@ -1259,6 +1084,7 @@ export class Contract<Abi extends ContractAbi>
}

const newContract = this.clone();
// newContract.subscribeToContextEvents(this.context as Web3Context);
nikoulai marked this conversation as resolved.
Show resolved Hide resolved

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
newContract.options.address = receipt.contractAddress;
Expand Down Expand Up @@ -1330,4 +1156,16 @@ export class Contract<Abi extends ContractAbi>
return sub;
};
}

protected subscribeToContextEvents<T extends Web3Context>(context: T): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const contractThis = this;
this.context = context;

if (contractThis.syncWithContext) {
context.on(Web3ConfigEvent.CONFIG_CHANGE, event => {
contractThis.setConfig({ [event.name]: event.newValue });
});
}
}
}
4 changes: 4 additions & 0 deletions packages/web3-eth-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export interface ContractInitOptions {
readonly data?: Bytes;
readonly gasLimit?: Uint;
readonly provider?: SupportedProviders<EthExecutionAPI> | string;
/**
* If `true`, the defaults of the contract instance will be updated automatically based on the changes of the context used to instantiate the contract.
*/
readonly syncWithContext?: boolean;
}

export interface NonPayableCallOptions {
Expand Down
16 changes: 12 additions & 4 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PayableCallOptions,
ContractOptions,
Web3ContractContext,
ContractInitOptions,
} from './types';

export const getSendTxParams = ({
Expand Down Expand Up @@ -138,12 +139,19 @@ export const getEstimateGasParams = ({
return txParams as TransactionWithSenderAPI;
};

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

export const isWeb3ContractContext = (options: unknown): options is Web3ContractContext =>
typeof options === 'object' && !isNullish(options) && !isContractInitOptions(options);
Expand Down
Loading