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

setProvider "does not exist on type 'typeof import(..." in typescript. #4770

Closed
1 task done
bsor-dev opened this issue Feb 13, 2022 · 3 comments · Fixed by #4822
Closed
1 task done

setProvider "does not exist on type 'typeof import(..." in typescript. #4770

bsor-dev opened this issue Feb 13, 2022 · 3 comments · Fixed by #4822
Labels
1.x 1.0 related issues Bug Addressing a bug

Comments

@bsor-dev
Copy link

bsor-dev commented Feb 13, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Can't call Web3EthContract.setProvider(ethereum)

function doesn't exist in the index.d.ts

/*
    This file is part of web3.js.
    web3.js is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    web3.js is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
    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/>.
*/
/**
 * @file index.d.ts
 * @author Josh Stevens <joshstevens19@hotmail.co.uk>
 * @date 2018
 */

import BN = require('bn.js');
import {Common, PromiEvent, provider, hardfork, chain, BlockNumber, PastLogsOptions, LogsOptions} from 'web3-core';
import {AbiItem} from 'web3-utils';

// TODO: Add generic type!
export class Contract {
    constructor(
        jsonInterface: AbiItem[],
        address?: string,
        options?: ContractOptions
    );

    private _address: string;
    private _jsonInterface: AbiItem[];
    defaultAccount: string | null;
    defaultBlock: BlockNumber;
    defaultCommon: Common;
    defaultHardfork: hardfork;
    defaultChain: chain;
    transactionPollingTimeout: number;
    transactionConfirmationBlocks: number;
    transactionBlockTimeout: number;
    handleRevert: boolean;

    options: Options;

    clone(): Contract;

    deploy(options: DeployOptions): ContractSendMethod;

    methods: any;

    once(
        event: string,
        callback: (error: Error, event: EventData) => void
    ): void;
    once(
        event: string,
        options: EventOptions,
        callback: (error: Error, event: EventData) => void
    ): void;

    events: any;

    getPastEvents(event: string): Promise<EventData[]>;
    getPastEvents(
        event: string,
        options: PastEventOptions,
        callback: (error: Error, event: EventData) => void
    ): Promise<EventData[]>;
    getPastEvents(event: string, options: PastEventOptions): Promise<EventData[]>;
    getPastEvents(
        event: string,
        callback: (error: Error, event: EventData) => void
    ): Promise<EventData[]>;
}

export interface Options extends ContractOptions {
    address: string;
    jsonInterface: AbiItem[];
}

export interface DeployOptions {
    data: string;
    arguments?: any[];
}

export interface ContractSendMethod {
    send(
        options: SendOptions,
        callback?: (err: Error, transactionHash: string) => void
    ): PromiEvent<Contract>;

    call(
        options?: CallOptions,
        callback?: (err: Error, result: any) => void
    ): Promise<any>;

    estimateGas(
        options: EstimateGasOptions,
        callback?: (err: Error, gas: number) => void
    ): Promise<number>;

    estimateGas(callback: (err: Error, gas: number) => void): Promise<number>;

    estimateGas(
        options: EstimateGasOptions,
        callback: (err: Error, gas: number) => void
    ): Promise<number>;

    estimateGas(options: EstimateGasOptions): Promise<number>;

    estimateGas(): Promise<number>;

    encodeABI(): string;
}

export interface CallOptions {
    from?: string;
    gasPrice?: string;
    gas?: number;
}

export interface SendOptions {
    from: string;
    gasPrice?: string;
    gas?: number;
    value?: number | string | BN;
    nonce?: number;
}

export interface EstimateGasOptions {
    from?: string;
    gas?: number;
    value?: number | string | BN;
}

export interface ContractOptions {
    // Sender to use for contract calls
    from?: string;
    // Gas price to use for contract calls
    gasPrice?: string;
    // Gas to use for contract calls
    gas?: number;
    // Contract code
    data?: string;
}

export interface PastEventOptions extends PastLogsOptions {
    filter?: Filter;
}

export interface EventOptions extends LogsOptions {
    filter?: Filter;
}

export interface Filter {
    [key: string]: number | string | string[] | number[];
}

export interface EventData {
    returnValues: {
        [key: string]: any;
    };
    raw: {
        data: string;
        topics: string[];
    };
    event: string;
    signature: string;
    logIndex: number;
    transactionIndex: number;
    transactionHash: string;
    blockHash: string;
    blockNumber: number;
    address: string;
}

Expected Behavior

I want to use setProvider

Steps to Reproduce


import Web3 from 'web3'
import Web3EthContract from 'web3-eth-contract'


const { ethereum } = window as any
    const metamaskIsInstalled = ethereum && ethereum.isMetaMask
    if (metamaskIsInstalled) {
      Web3EthContract.setProvider(ethereum)
      const web3 = new Web3(ethereum)

     ....


Web3.js Version

1.7.0

Environment

  • Operating System:
  • Browser:
  • Node.js Version:
  • NPM Version:

Anything Else?

No response

@bsor-dev bsor-dev added the Bug Addressing a bug label Feb 13, 2022
@jdevcs jdevcs added the 1.x 1.0 related issues label Feb 14, 2022
@jdevcs
Copy link
Contributor

jdevcs commented Feb 14, 2022

Hi @rosnaib11, This functionality is not exposed for contracts package in 1.x , you should use it with main Web3 or web3.eth.setProvider(myProvider)

@jdevcs jdevcs closed this as completed Feb 14, 2022
@sdesalas
Copy link

sdesalas commented Mar 4, 2022

Hiya @jdevcs any reason why its not exposed?

Seems to be used in the documentation for web3.eth.Contract:

var Contract = require('web3-eth-contract');

// set provider for all later instances to use
Contract.setProvider('ws://localhost:8546');

var contract = new Contract(jsonInterface, address);

contract.methods.somFunc().send({from: ....})
.on('receipt', function(){
    ...
});

@sdesalas
Copy link

sdesalas commented Mar 4, 2022

☝️ Added PR if you want to merge. Otherwise feel free to close.

jdevcs pushed a commit that referenced this issue May 4, 2022
…4822)

* Exposing web3.eth.Contract.setProvider() as per public documentation

* Additional TS defs for web3.eth.Contract.setProvider() as suggested (#4770)

* Additional TS defs for web3.eth.Contract.setProvider()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants