0

I am following the tutorial provided at: Example of Truffle script

But I have added the ganache, compilation and migrate. In migration I am getting the following error: $ 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)

My truffle 2_deploy_contracts.js is:

const Exchange = artifacts.require("Exchange");

module.exports = function(deployer) {
deployer.deploy(Exchange);
};

The Exchange contract and the script is provided in the tutorial, I have named the script as ts2 and is copied below:

// 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()
}

Somebody please guide me.

Zulfi.

1 Answer 1

1

The tutorial works for me with this truffle-config.js file :

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)
    }
  }
};

Make sure to correctly deploy the smart contract using truffle migrate --reset.

Then, I was able to successfully ran truffle exec ts2.js.

1
  • 1
    Thanks, it worked. I got the following output: $ truffle exec ts2.js Using network 'development'. Exchange fetched 0x498Ffe59f98b40Ebc4593ce169b0B55545d6Fa07 Deposited 2 Ether from 0x794b13e9b81102957CB0F519b9D4c3496B045B67
    – zak100
    Jun 2, 2021 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.