Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 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
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

- Remove erroneous `addressOrOptionsOrContext !== 'string'` check in Contract constructor (#5669)
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

- Remove erroneous `addressOrOptionsOrContext !== 'string'` check in Contract constructor (#5669)
53 changes: 33 additions & 20 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,27 +347,40 @@ export class Contract<Abi extends ContractAbi>
contextOrReturnFormat?: Web3ContractContext | DataFormat,
returnFormat?: DataFormat,
) {
let contractContext;
if (isWeb3ContractContext(addressOrOptionsOrContext)) {
contractContext = addressOrOptionsOrContext;
} else if (isWeb3ContractContext(optionsOrContextOrReturnFormat)) {
contractContext = optionsOrContextOrReturnFormat;
} else {
contractContext = contextOrReturnFormat;
}

let provider;
if (
typeof addressOrOptionsOrContext === 'object' &&
'provider' in addressOrOptionsOrContext
) {
provider = addressOrOptionsOrContext.provider;
} else if (
provider === undefined &&
typeof optionsOrContextOrReturnFormat === 'object' &&
'provider' in optionsOrContextOrReturnFormat
) {
provider = optionsOrContextOrReturnFormat.provider;
} else if (
provider === undefined &&
typeof contextOrReturnFormat === 'object' &&
'provider' in contextOrReturnFormat
) {
provider = contextOrReturnFormat.provider;
} else if (provider === undefined && Contract.givenProvider !== undefined) {
provider = Contract.givenProvider;
}

super({
// Due to abide by the rule that super must be first call in constructor
// Have to do this complex ternary conditions
// eslint-disable-next-line no-nested-ternary
...(isWeb3ContractContext(addressOrOptionsOrContext)
? addressOrOptionsOrContext
: isWeb3ContractContext(optionsOrContextOrReturnFormat)
? 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,
...contractContext,
provider,
registeredSubscriptions: contractSubscriptions,
});

Expand Down
13 changes: 13 additions & 0 deletions packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Contract } from '../../src';
import { sampleStorageContractABI } from '../fixtures/storage';
import { GreeterAbi, GreeterBytecode } from '../shared_fixtures/build/Greeter';
import { AllGetPastEventsData, getLogsData, getPastEventsData } from '../fixtures/unitTestFixtures';
import { getSystemTestProvider } from '../fixtures/system_test_utils';

jest.mock('web3-eth');

Expand Down Expand Up @@ -72,6 +73,18 @@ describe('Contract', () => {

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

it('should set the provider upon instantiation', () => {
const provider = getSystemTestProvider();
const contract = new Contract([], '', {
provider,
});

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

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