简体   繁体   中英

VUEX & WEB3: Duplicated method constructor. This method is defined as RPC call and as Object method

Having the following issue when deploying my web3 inside of VueX:

TypeError: Duplicated method constructor. This method is defined as RPC call and as Object method.

I have deduced that the issue is in the declaration of web3 in setupWeb3 Action (see below).

I am assuming it is some typing issue?

Any thoughts would be much appreciated.

Edit:

I am declaring a global namespace in my main.ts as follows:

declare global {
  interface Window {
      ethereum:any;
      web3:any;
  }
}

Here are my Actions:

export const actions: ActionTree<Network, RootState> = {
    setupWeb3(context: ActionContext<Network, RootState>) {
        let web3;
        if (window.ethereum) {
            web3 = new Web3(window.ethereum);
            window.ethereum.enable().then(enabled => console.log(enabled));
        } else if (window.web3) {
            web3 = new Web3(window.web3.currentProvider);
        } else {
            // TODO better handle of metamask
            window.alert(
                'Non-Ethereum browser detected. You should consider trying MetaMask!',
            );
        }
        context.commit('SET_WEB3', web3);
    },
    async getNetworkData(context: ActionContext<Network, RootState>) {
        const { Web3 } = context.getters;
        const network = await Web3.eth.net.getNetworkType();
        const networkId = await Web3.eth.net.getId();
        const currentBlock = await Web3.eth.getBlockNumber();
        context.commit('SET_NETWORK_DATA', {
            network,
            networkId,
            currentBlock,
        });
    },
    getAddress(context: ActionContext<Network, RootState>) {
        const { Web3 } = context.getters;
        Web3.eth.getAccounts().then((account: string[]) => 
            context.commit("SET_ADDRESS", account[0]));
    },
    bootstrapContracts(context: ActionContext<Network, RootState>) {
        const setupWeb3 = context.dispatch('setupWeb3');
        const network   = context.dispatch('getNetworkData');
        const address   = context.dispatch('getAddress');
        Promise.all([setupWeb3, network, address,])
            .then(() => {
                context.dispatch('setupLootControls');
            });
    },
};

Turns out that I was on a beta release and by upgrading to

"web3": "^1.2.9"

Fixed the error for me. In case anyone else has the same issue: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM