[英]Contract has not been deployed to detected network (network/artifact mismatch)
I'm having some issues determining and connecting with the right MetaMask network.我在确定和连接正确的 MetaMask 网络时遇到了一些问题。
In Ganache, my RPC server is 127.0.0.1.7545 and the network id is 5777. However, when I try to create a custom RPC in MetaMask with this info, I get the following error:在 Ganache 中,我的 RPC 服务器是 127.0.0.1.7545,网络 ID 是 5777。但是,当我尝试使用此信息在 MetaMask 中创建自定义 RPC 时,出现以下错误:
The endpoint returned a different chain ID: 1337
This is my truffle-config.js:这是我的 truffle-config.js:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*" // Match any network id
},
develop: {
port: 8545
}
}
};
I'm hoping this would match to any network id as I've specified, but the console shows the following error:我希望这与我指定的任何网络 ID 匹配,但控制台显示以下错误:
Contract has not been deployed to detected network (network/artifact mismatch)
I've already tried truffle migrate --reset
, without success.我已经尝试过
truffle migrate --reset
,但没有成功。 I've also tried creating an explicit network for testrpc in truffle-config.js - that didn't work either.我还尝试在 truffle-config.js 中为 testrpc 创建一个显式网络 - 这也不起作用。
Any help would be much appreciated!任何帮助将非常感激!
You are seeing that error because your contract is deployed to Ganache but you are connected to a different network.您看到该错误是因为您的合约已部署到 Ganache,但您连接到不同的网络。
The code that you are writing to load the contract should be inside try/catch
block.您为加载合约而编写的代码应该在
try/catch
块内。
inside loading contract logic:内部加载合约逻辑:
export const loadContract = async (name, provider) => {
// Load the contract
// set the provider
let deployedContract = null;
try {
// Get the contract
deployedContract = await _contract.deployed();
} catch {
console.error("You are connected to the wrong network");
}
return deployedContract;
};
In the component that you are using loadContract
, call it inside useEffect
.在您使用
loadContract
的组件中,在useEffect
内部调用它。
useEffect(() => {
// Detect Provider
if (provider) {
// contract should be loaded when provider exists
const contract = await loadContract("ContractName", provider);
rLoaded: true,
// Add More logic
} else {
console.error("Please, install Metamask.");
}
};
}, []);
Now you need to make sure if you are not connected to Ganache, disable the button, so your app won't crash.现在您需要确保如果您没有连接到 Ganache,请禁用该按钮,这样您的应用程序就不会崩溃。 for this create a state variable
为此创建一个状态变量
// You probably already have logic to get account and contract
const canConnectToContract = account && contract;
now write a proper ui:现在写一个合适的用户界面:
{!canConnectToContract && (
<h2>Connect to Ganache</h2>
)}
<button
disabled={!canConnectToContract}
>
Donate 1 Ethreum
</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.