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。
更多回答
我是一名优秀的程序员,十分优秀!