Migration
執行 Truffle 腳本的問題:‘Exchange’ 即智能合約 (SC) 尚未部署到檢測到的網路
我正在關注提供的教程: Truffle 腳本範例
但我已經添加了 ganache、編譯和遷移。在遷移中,我收到以下錯誤:$ truffle exec ts2.js Using network ‘ganache’。
Error: Exchange has not been deployed to detected network (network/artifact mismatch) at Object.checkNetworkArtifactMatch (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/contract/lib/utils/index.js:245:1) at Function.deployed (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/contract/lib/contract/constructorMethods.js:85:1) at process._tickCallback (internal/process/next_tick.js:68:7)
我的松露 2_deploy_contracts.js 是:
const Exchange = artifacts.require("Exchange"); module.exports = function(deployer) { deployer.deploy(Exchange); };
教程中提供了 Exchange 合約和腳本,我將腳本命名為 ts2,複製如下:
// Contracts const Exchange = artifacts.require("Exchange") // Utils const ether = (n) => { return new web3.utils.BN( web3.utils.toWei(n.toString(), 'ether') ) } module.exports = async function(callback) { try { // Fetch accounts from wallet - these are unlocked const accounts = await web3.eth.getAccounts() // Fetch the deployed exchange const exchange = await Exchange.deployed() console.log('Exchange fetched', exchange.address) // Set up exchange users const user1 = accounts[0] // User 1 Deposits Ether amount = 2 await exchange.depositEther({ from: user1, value: ether(amount) }) console.log(`Deposited ${amount} Ether from ${user1}`) //... } catch(error) { console.log(error) } callback() }
有人請指導我。
祖爾菲。
本教程適用於我這個
truffle-config.js
文件:module.exports = { networks: { development: { host: "127.0.0.1", // Localhost (default: none) port: 8545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) }, }, // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 }, // Configure your compilers compilers: { solc: { version: "0.8.4", // Fetch exact version from solc-bin (default: truffle's version) } } };
確保使用正確部署智能合約
truffle migrate --reset
。然後,我能夠成功執行
truffle exec ts2.js
。