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

Remove erroneous set provider code for Contract constructor #5669

Merged
merged 8 commits into from
Dec 6, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,7 @@ should use 4.0.1-alpha.0 for testing.
#### web3-eth-accounts

- These types were moved from `web3-eth-accounts` to `web3-types` package: Cipher, CipherOptions, ScryptParams, PBKDF2SHA256Params, KeyStore (#5581 )

#### web3-eth-contract

- Erroneous set `provider` logic in Contract constructor (#5669)
spacesailor24 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ const transactionHash = receipt.transactionHash;

- Emit past contract events based on `fromBlock` when passed to `contract.events.someEventName` (#5201)
- Use different types for `ContractOptions` -> `jsonInterface` setter and getter (#5474)

### Removed

- Erroneous set `provider` logic in Contract constructor (#5669)
spacesailor24 marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 9 additions & 11 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,15 @@ export class Contract<Abi extends ContractAbi>
? optionsOrContextOrReturnFormat
: contextOrReturnFormat),
provider:
typeof addressOrOptionsOrContext !== 'string'
? addressOrOptionsOrContext?.provider ??
// eslint-disable-next-line no-nested-ternary
(typeof optionsOrContextOrReturnFormat === 'object' &&
'provider' in optionsOrContextOrReturnFormat
? optionsOrContextOrReturnFormat.provider
: typeof contextOrReturnFormat === 'object' &&
'provider' in contextOrReturnFormat
? contextOrReturnFormat?.provider
: Contract.givenProvider)
: undefined,
(addressOrOptionsOrContext as Web3ContractContext)?.provider ??
// eslint-disable-next-line no-nested-ternary
(typeof optionsOrContextOrReturnFormat === 'object' &&
'provider' in optionsOrContextOrReturnFormat
? optionsOrContextOrReturnFormat.provider
: typeof contextOrReturnFormat === 'object' &&
'provider' in contextOrReturnFormat
? contextOrReturnFormat?.provider
: Contract.givenProvider),
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
registeredSubscriptions: contractSubscriptions,
});

Expand Down
12 changes: 12 additions & 0 deletions packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ describe('Contract', () => {

expect(contract).toBeInstanceOf(Contract);
});

it('should set the provider upon instantiation', () => {
const provider = 'http://127.0.0.1:8545';
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
const contract = new Contract([], '', {
provider,
});

expect(contract.provider).toEqual({
clientUrl: provider,
httpProviderOptions: undefined,
});
});
});

describe('Contract functions and defaults', () => {
Expand Down