gpt4 book ai didi

Cannot connect to MetaMask after upgrading web3 from 1.x to 4.x due to "setProvider is not a function" error(将Web3从1.x升级到4.x后,由于“setProvider不是一个函数”错误,无法连接到MetaMask.)

转载 作者:bug小助手 更新时间:2023-10-24 19:05:03 24 4
gpt4 key购买 nike



The following code worked fine until I decided to upgrade the two following dependencies from 1.7.5 versions to the latest:

以下代码运行良好,直到我决定将以下两个依赖项从1.7.5版本升级到最新版本:


"web3": "^4.1.1",
"web3-eth-contract": "^4.0.5"

After the update, when I use my app to connect to metamask I get TypeError:

更新后,当我使用我的应用程序连接到元掩码时,我收到了TypeError:


"web3_eth_contract__WEBPACK_IMPORTED_MODULE_0__.default.setProvider is not a function"

The line where the error points to:
Web3EthContract.setProvider(ethereum);

错误所指向的行:Web3ethContract.setProvider(Etherum);


import Web3EthContract from "web3-eth-contract";
import Web3 from "web3";
import MyContractABI from "../../contracts/myContractABI.json";
import { fetchData } from "../data/dataActions";

const connectRequest = () => {
return {
type: "CONNECTION_REQUEST",
};
};

const connectSuccess = (payload) => {
return {
type: "CONNECTION_SUCCESS",
payload: payload,
};
};

const connectFailed = (payload) => {
return {
type: "CONNECTION_FAILED",
payload: payload,
};
};

const updateAccountRequest = (payload) => {
return {
type: "UPDATE_ACCOUNT",
payload: payload,
};
};

export const connect = () => {
return async (dispatch) => {
dispatch(connectRequest());
const { ethereum } = window;
const metamaskIsInstalled = ethereum && ethereum.isMetaMask;
await window.ethereum.request({
method: "eth_requestAccounts",
});
if (metamaskIsInstalled) {
// In the next line the code shows the following error:
// TypeError: web3_eth_contract__WEBPACK_IMPORTED_MODULE_0__.default.setProvider is not a function
Web3EthContract.setProvider(ethereum);
let web3 = new Web3(ethereum);
try {
const accounts = await ethereum.request({
method: "eth_requestAccounts",
});
const networkId = await ethereum.request({
method: "net_version",
});
if (networkId == 679420) { // this is a local ganache testing net ID
const myContract = new Web3EthContract( MyContractABI, "0x3d7Eec9C41c7A99489fD27e6B087f9C827b16d3F");
dispatch(
connectSuccess({
account: accounts[0],
_myContract: myContract,
web3: web3,
})
);
ethereum.on("accountsChanged", (accounts) => {
window.location.reload();
});
ethereum.on("chainChanged", () => {
window.location.reload();
});
} else {
dispatch(connectFailed("Change Metamask Network to Polygon and Connect again!"));
}
} catch (err) {
dispatch(connectFailed("Could not connect to Polygon Network. Check MetaMask settings."));
console.log(err);
}
} else {
dispatch(connectFailed("Please install Metamask to your browser extensions, and try again!"));
}
};
};
export const updateAccount = (account) => {
return async (dispatch) => {
dispatch(updateAccountRequest({ account: account }));
dispatch(fetchData(account));
};
};


I tried looking at the latest official syntax here:
"How to use the web3-eth-contract.setProvider function in web3-eth-contract"
snyk.io/advisor/npm-package/web3-eth-contract

我试着在这里查看了最新的官方语法:“How to Use the web3-ETH-Contact t.setProvider Function in web3-ETH-Contact”snyk.io/Advisor/NPM-Package/Web3-ETH-Contact


I also looked at the official breaking changes list here:
docs.web3js.org/guides/web3_upgrade_guide

我还查看了官方的突破性更改列表:docs.web3js.org/Guide/web3_upgrade_Guide


I cannot see what I am doing wrong, the latest examples seem to use the exact same syntax, but honestly I do not understand web3 code well enough yet. If I revert to 1.75 versions of the above dependencies, my dapp connects to metamask and works fine.
I have no specific reason to want to upgrade from version 1.75, I simply wanted to perhaps futureproof my dapp.

我看不出我做错了什么,最新的例子似乎使用了完全相同的语法,但老实说,我还不够好地理解Web3代码。如果我恢复到上述依赖项的1.75版本,我的DAPP将连接到元掩码,并且工作正常。我没有特别的理由想要从1.75版本升级,我只是想也许将来证明我的DAPP。


更多回答
优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com