diff --git a/CHANGELOG.md b/CHANGELOG.md index bc91c060afe..a68b7ca5203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/web3-eth-contract/CHANGELOG.md b/packages/web3-eth-contract/CHANGELOG.md index 3062d11eb5b..fec6f069c9d 100644 --- a/packages/web3-eth-contract/CHANGELOG.md +++ b/packages/web3-eth-contract/CHANGELOG.md @@ -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) diff --git a/packages/web3-eth-contract/src/contract.ts b/packages/web3-eth-contract/src/contract.ts index 6f27d4fb4cc..7fe4e966aa8 100644 --- a/packages/web3-eth-contract/src/contract.ts +++ b/packages/web3-eth-contract/src/contract.ts @@ -347,27 +347,38 @@ export class Contract 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, }); diff --git a/packages/web3-eth-contract/test/unit/contract.test.ts b/packages/web3-eth-contract/test/unit/contract.test.ts index f1ce2cd11ec..dc03e26d5fd 100644 --- a/packages/web3-eth-contract/test/unit/contract.test.ts +++ b/packages/web3-eth-contract/test/unit/contract.test.ts @@ -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'); @@ -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', () => {