Having a little issue. I have deployed a Mock ERC20 contract on development network like so:

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

module.exports = async function(deployer, network, accounts) {
    if (network === "development") {
        console.log("Deploying MockDAI on Development at: ", accounts[5]);
        await deployer.deploy(ERC20Mock, accounts[5], 100*(10**6), {from: accounts[5]});

And trying to interact with truffle test:

contract("Erc20Teller", function([_, adminRole, user ]) {
  let daiAddress;
  beforeEach(async function() {
    daiAddress = (await web3.eth.getAccounts())[5];
    this.daiToken = await ERC20Mock.at(daiAddress);

However, I am receiving Error: Cannot create instance of ERC20Mock; no code at address 0x7F39C89Ed1954AB82361C23f6c02b420DF65b3D0 even though the same address is clearly being used in my migrations and in the unit test. What could the problem be?

  • The fact that you're deploying your contract using account 0x7F39C89Ed1954AB82361C23f6c02b420DF65b3D0 doesn't mean that your contract will be deployed to this address. In fact, it will NOT be deployed to this address, hence the error. – goodvibration Feb 20 '19 at 14:35
  • @goodvibration, I see! How would I find where the contract is deployed to? – isaacsultan Feb 20 '19 at 14:39
  • Side note: The second argument to Truffle's contract function is an array of all available accounts. So you may start your test with contract("Erc20Teller", function(accounts) {, and then simply use accounts[5] instead of (await web3.eth.getAccounts())[5]. – goodvibration Feb 20 '19 at 14:39
  • I don't rely on Truffle migration infrastructure, so I'm not quite sure. But you can simply mask your migration altogether, for example, by changing if (network === "development") to if (network === "production"), and then deploy the contract in your test instead, via this.daiToken = await artifacts.require("ERC20Mock").new();. – goodvibration Feb 20 '19 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.