![](/img/trans.png)
[英]Token.networks[networkId] returns undefined in web3
[英]Ropsten Networks is Undefined
我正在嘗試將 Metamask 上的 Ropsten TestNet 連接到我的項目。 但是我有一個問題,我已經好幾天沒能解決了。 我在要重定向的頁面上做了如下定義。
當我希望它是一個錯誤時,我希望它給我一個信息。 我現在得到的正是那個錯誤。 '確保你在正確的網絡上。 將網絡設置為 Ropsten 測試網絡'
publicdashboard.jsx
useEffect(() => {
const init = async () => {
try {
const web3 = await getWeb3();
const accounts = await web3.eth.getAccounts();
const networkId = await web3.eth.net.getId();
const deployedNetwork = Project.networks[networkId];
if(deployedNetwork === undefined)
throw new Error('Make sure you are on the corrent network. Set the network to Ropsten Test Network');
const contract = new web3.eth.Contract(
Project.abi,
deployedNetwork.address,
);
setWeb3(web3);
setAccounts(accounts);
setContract(contract);
} catch (error) {
window.alert(error);
history.push("/dashboard");
}
}
init();
if (isReady()) {
window.ethereum.on('accountsChanged', accounts => {
setAccounts(accounts);
});
}
}, [history]);
這是我的 truffle-config.js 文件。 一切似乎都很正常。 我不明白為什么它沒有連接。 你能幫我解決這個問題嗎?
const path = require("path");
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
module.exports = {
contracts_build_directory: path.join(__dirname, "src/contracts"),
networks: {
ropsten: {
provider: () => new HDWalletProvider(mnemonic, 'https://ropsten.infura.io/v3/08ac778579d74dbaa8d2e3d02e5c0092'),
network_id: 3, // Ropsten's id
gas: 5500000, // Ropsten has a lower block limit than mainnet
confirmations: 2, // # of confs to wait between deployments. (default: 0)
timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
skipDryRun: false // Skip dry run before migrations? (default: false for public nets )
},
},
mocha: {
// timeout: 100000
},
compilers: {
solc: {
//version: "0.8.13", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
}
},
}
};
我還在終端上寫了這段代碼“truffle migrate --network ropsten”。 我沒有看到任何錯誤。
以供將來參考,請勿發布以 08ac7785 開頭的 api 密鑰...這意味着任何人都可以使用它,如果可能,請刪除 api 密鑰。 您的Project
命名空間似乎也無效。 比使用 HDWalletProvider 更好的新解決方案是使用新的 truffle truffle dashboard
命令連接到連接到您的硬件錢包的 metamask
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.