Cannot upgrade ERC20

Hi I write a new ERC20 contract by openzeppelin-contracts-ethereum-package,
and I run oz deploy first .for example in my code I changed thet Token name from "Crypto" to "Crypto1" but when I run oz upgrade and call name() it returns "Crypto",

This is my code:

    // SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;`Preformatted text`


import '@openzeppelin/contracts-ethereum-package/contracts/Initializable.sol';
import '@openzeppelin/contracts-ethereum-package/contracts/presets/ERC20PresetMinterPauser.sol';
import '@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol';
contract CryptomindToken is Initializable, ERC20PresetMinterPauserUpgradeSafe   {

 uint256 private _totalSupply;
  function initialize() public initializer {
    ERC20PresetMinterPauserUpgradeSafe.initialize("Crypto", "CR");
      _setupDecimals(8);
	   _totalSupply = totalSupply();
	   if(_totalSupply ==0){
	       _mint(msg.sender, 2000000000 * (10 ** 8));
	   }
	   
		transfer(0x9C593f37322Adc092aa8996B14C217b4e1fC4c84,200);

  }

  function  inc(address recipient) public returns (bool){
       	transfer(recipient,200);
		return true;
    }

}

Also, when I added a new function it didn't work.

For example, I add this function:

  function  incMore(address recipient) public returns (bool){
       	transfer(recipient,300);
		return true;
    }

this error appears:

npx oz send-tx
? Pick a network development
? Pick an instance CryptomindToken at 0x4d7251741cd0bc241e32A362328DB00eee859aFa
? Select which function incMore(recipient: address)
? recipient: address: 0x7D1b02681995408bC896AE1CfF622F43037A2B16
× Calling: 'incMore' with:

  • recipient (address): "0x7D1b02681995408bC896AE1CfF622F43037A2B16"
    Error while trying to send transaction to 0x4d7251741cd0bc241e32A362328DB00eee859aFa. Error: Error: Returned error: VM Exception while processing transaction: revert
1 Like

Hi @Mehran_Hafizi,

Welcome to the community :wave:

I noticed a few things about your contract:

You are doing a check on the total supply in the initialize function before any minting, so this will always be zero as you are using the initializer modifier so this function is only ever called once. (similar to a constructor)

You are doing transfer inside your initialize, so this requires that your token contract actually has tokens which it doesn't. I assume you want to do a _mint instead:

_From: https://docs.openzeppelin.com/contracts/3.x/api/token/erc20#IERC20-transfer-address-uint256-_
Moves amount tokens from the caller’s account to recipient .

In your inc function you are doing a transfer which is trying to transfer 200 tokens from the contract (which has zero) to the recipient.

When you add incMore it also does a transfer which fails (which is why it is reverting.

  function  incMore(address recipient) public returns (bool){
       	transfer(recipient,300);
		return true;
    }

I suggest having a look at: Example on how to use ERC20 token in another contract


As an aside, OpenZeppelin Upgrades Plugins for Buidler and Truffle was recently released. You could use either of the plugins to deploy upgradeable contracts: https://docs.openzeppelin.com/upgrades-plugins/1.x/

1 Like