Deploy ERC20 contract

Hello again @abcoathup ,

First thanks for show me the code to help me understand

I have a problem , i am learning in the docs how to use openzeppelin, in this case when i run this code

pragma solidity ^0.5.0;

//import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

//import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20Detailed.sol";



/**

 * @title SimpleToken

 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.

 * Note they can later distribute these tokens as they wish using `transfer` and other

 * `ERC20` functions.

 */

contract SimpleToken is ERC20, ERC20Detailed {



    /**

     * @dev Constructor that gives msg.sender all of existing tokens.

     */

    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {

        _mint(msg.sender, 10000 * (10 ** uint256(decimals())));

    }

}

when i compile , i have this error

 ✖ Compiling contracts with solc 0.5.16 (commit.9c3226ce)
Compilation errors: 
contracts/SimpleToken.sol:35:27: TypeError: Wrong argument count for modifier invocation: 3 arguments given but expected 0.
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {

i dont know what i am doing wrong but if i run the code with this imports

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

compile fine , but the problem back when i try to create using npx oz create and select the contract , the error says

Nothing to compile, all contracts are up to date.
? Pick a contract to instantiate SimpleToken
? Pick a network development
- Contract SimpleToken or an ancestor has a constructor. Change it to an initializer function. See https://docs.openzeppelin.com/upgrades/2.6//writing-upgradeable#initializers.
- Contract SimpleToken imports token/ERC20/ERC20.sol, GSN/Context.sol, token/ERC20/IERC20.sol, math/SafeMath.sol, token/ERC20/ERC20Detailed.sol from @openzeppelin/contracts. Use @openzeppelin/contracts-ethereum-package instead. See https://docs.openzeppelin.com/cli/2.6/dependencies#linking-the-contracts-ethereum-package.
One or more contracts have validation errors. Please review the items listed above and fix them, or run this command again with the --force option.

i go to the link and i use this imports

import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20Detailed.sol";

but the first error back again , i dont know how to solved , i think the first error is the most nearby to the solution , but i dont know what i need to do exacly :confused:

1 Like

Hi @kypanz,

I’m sorry that you are having issues.

The OpenZeppelin CLI used to be only for creating upgradeable contracts. (OpenZeppelin CLI now in 2.8 Release Candidate supports deploying non-upgradeable contracts)

To create an upgradeable version of the SimpleToken we need to use an initializer rather than a constructor. (see Writing Upgradeable Contracts)

I previously gave you an example of a non-upgradeable (regular) version of the SimpleToken.

In our project we link OpenZeppelin Contracts Ethereum Package and install @openzeppelin/upgrades

openzeppelin link @openzeppelin/contracts-ethereum-package
npm install @openzeppelin/upgrades

SimpleToken.sol

Upgradeable SimpleToken

pragma solidity ^0.5.0;

import "@openzeppelin/upgrades/contracts/Initializable.sol";

import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the sender.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is Initializable, ERC20, ERC20Detailed {

    /**
     * @dev initialize that gives msg.sender all of existing tokens.
     */
    function initialize(address sender) public initializer {
        ERC20Detailed.initialize("Token", "TKN", 18);
        _mint(sender, 1000000 * (10 ** uint256(decimals())));
    }
}

Create SimpleToken

Get an account to mint the tokens to.

$ oz accounts
? Pick a network development
Accounts for dev-1573097445291:
Default: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
All:
- 0: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1

Create the contract, call initialize with the account to hold the tokens.

$ oz create
✓ Compiled contracts with solc 0.5.12 (commit.7709ece9)
? Pick a contract to instantiate SimpleToken
? Pick a network development
All contracts are up to date
? Do you want to call a function on the instance after creating it? Yes
? Select which function * initialize(sender: address)
? sender (address): 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
✓ Instance created at 0xCeeFD27e0542aFA926B87d23936c79c276A48277
0xCeeFD27e0542aFA926B87d23936c79c276A48277

Interact

$ oz call
? Pick a network development
? Pick an instance SimpleToken at 0xCeeFD27e0542aFA926B87d23936c79c276A48277
? Select which function name()
✓ Method 'name()' returned: Token
Token

Hi @kypanz,

Have you been able to deploy?

1 Like

Yes Works fine, and i learn mocha , assert and chai too , thanks a lot :grinning: :heart:

1 Like