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 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ should use 4.0.1-alpha.0 for testing.

- Emit past contract events based on `fromBlock` when passed to `contract.events.someEventName` (#5201)
- Use different types for `ContractOptions` -> `jsonInterface` setter and getter (#5474)
- An issue within the `Contract` constructor where `provider` wasn't being set when provided within the `optionsOrContextOrReturnFormat` argument (#5669)

#### web3-types

Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,4 @@ 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)
- An issue within the `Contract` constructor where `provider` wasn't being set when provided within the `optionsOrContextOrReturnFormat` argument (#5669)
51 changes: 31 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,38 @@ 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 (
typeof optionsOrContextOrReturnFormat === 'object' &&
'provider' in optionsOrContextOrReturnFormat
) {
provider = optionsOrContextOrReturnFormat.provider;
} else if (
typeof contextOrReturnFormat === 'object' &&
'provider' in contextOrReturnFormat
) {
provider = contextOrReturnFormat.provider;
} else {
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