Cannot read property 'address' of undefined deploying ERC20 example from documentation

Hello OpenZeppelin Community,

I am new to blockchain development and now trying out to use the ERC20 token contract. When executing the OpenZeppeling ERC20 example from the documentation (https://docs.openzeppelin.com/contracts/3.x/erc20) i run into the following issue once I execute “truffle migrate”:

TypeError: Cannot read property 'address' of undefined
    at Contract (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\index.js:31:1)
    at TruffleContract (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:205:1)
    at module.exports (C:\Users\kyril\Desktop\ethereum\GoldToken\migrations\2_deploy_contracts.js:4:19)
    at Migration._load (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:54:1)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at Migration.run (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:171:1)
    at Object.runMigrations (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:150:1)
    at Object.runFrom (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1)
    at Object.runAll (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1)
    at Object.run (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1)
    at runMigrations (C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:269:1)
    at C:\Users\kyril\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:231:1

:computer: Environment
Truffle v5.1.54 (core: 5.1.54)
Solidity - 0.7.5 (solc-js)
Node v12.19.0
Web3.js v1.2.9

Before migrating the contract i have created a one-klick blockchain in ganache on the localhost, which i have set also in the truffle-config.js.

This is the contract, that im using:

    // contracts/GLDToken.sol
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.7.5;
    import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
    contract GLDToken is ERC20 {
        constructor(uint256 initialSupply) public ERC20("Gold", "GLD") {
            _mint(msg.sender, initialSupply);
        }
    }

And my 2_deploy_contracts.js looks like this:

var SimpleToken = artifacts.require("SimpleToken");
module.exports = function(deployer) {
    deployer.deploy(SimpleToken(10000));
};

I would be very grateful for any help!

Best Regards,
Kyrill

1 Like

It seems like you do not provide a valid account to deploy the contract, so what is your truffle-config.js

2 Likes

This is the truffle-config.js i used:

module.exports = {
  networks: {
    development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 7545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)  
    //  from: 0x379eB8217Ad3FdfD4999E487Eaff82cf28044c8e
    },
  },
  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },
  // Configure your compilers
  compilers: {
    solc: {
      version: "0.7.5",    // Fetch exact version from solc-bin (default: truffle's version)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
       optimizer: {
         enabled: false,
         runs: 200
       }
     }
    }
  }
}

When adding a "from: " to the development network configuration I get the following error:
Error: while migrating Migrations: Returned error: sender account not recognized

1 Like

Okay, so have you started your local node, such as ganache-cli --port=7545 --gasLimit=8000000 --accounts=10 --defaultBalanceEther=10000?

2 Likes

I have the Ganache UI running, which should be doing the same in the background. But after trying with creating ganache in the cli, i got the exact same error.

When removing the constructor’s parameters and calling the _mint function with the initialsupply as a uint from the solidity smart contract, it is working. I really cant understand, why the address undefined error comes up when using a constructor with parameters.

1 Like

I finally got it!! It was a syntax mistake with calling the constructor from truffle in 2_deploy_contracts.js.
So the deployer only accepts SimpleToken,1000 in its parameters for the constructor instead of taking SimpleToken(1000), as I have been used to initialize constructors from other programming languages.

So the 2_deploy_contracts.js looks like this:

var SimpleToken = artifacts.require("SimpleToken");
module.exports = function(deployer) {
  deployer.deploy(SimpleToken, 1000);
};

Anyway, thank you for your help Skyge!

2 Likes