0

Background: I'm using an opezeppellin Erc20 contract. but i want to use it for minimal proxy pattern(eip-1167). so for that reason i cannot use constructor and have to use the initialize method. Now I'm unsure how to translate this constructor into initialize method.

constructor(uint256 initial_supply) ERC20("Solderse", "SOLD") {
        _mint(msg.sender, initial_supply * 10 ** uint256(decimals()));
    }

so I opt out to hack around which is as follows.

string private _myName;
string private _mySymbol;
bool private _initialized;

constructor() ERC20("", "") {
    _initialized = false;
}

function initialize(string memory _name, string memory _symbol, uint256 initial_supply) external {
    require(!_initialized, "Already initialized"); // Ensure it's not initialized yet
    _myName = _name;
    _mySymbol = _symbol;
    _initialized = true;
    _mint(msg.sender, initial_supply * 10 ** uint256(decimals()));
}

Here I'm initializing my child contract with blank ERC20 constructor to silent off the errors and using an initialize method where I'm setting a name and symbol.

Now another issue arises as in Erc20 implementation the name and symbol are private variables so I cannot change them so I opt in with another hack which is to create two override public function who can change the name and symbol so I made these two functions.

function name() public view override returns (string memory) {
    return _myName;
}

function symbol() public view override returns (string memory) {
    return _mySymbol;
}

And I can write tests to check the name and symbol are returning the values correctly but these hacks are not what I'm looking for as far as i know ERC20 storage values have not changed at any case and i find it not very much sophisticated. so basically my question is

Can we override the private variables from child Contract or How can I implement a basic er20 in minimal proxy pattern?

2 Answers 2

0

I think you should be able to use https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/master/contracts/token/ERC20/ERC20Upgradeable.sol together with the minimal proxy

0

You can use Openzeppelin's upgradeable set of contracts for this.

https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable

The trick here is, each upgradeable contract that you inherit dont have a constructor either, but an _init function (and also an __init_unchained)

So, for an ERC20, inside your inititialize() function you should have __ERC20_init(name, symbol)

You can see an example for an ERC20 in this article

https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable

Hope it helps

Your Answer

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

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