0

I want to create a contract where the coins that are minted initially are given to the contract, and then later I'll create a function on how to distribute them. But I was wondering if it's possible to give the tokens to the contract in the constructor? or do you first have to mint the coins in an EOA address?

Code sample:

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";


contract Test is ERC20, Ownable {

    uint256 private constant INITIAL_SUPPLY = 1000000000 * 10**18;

    constructor() ERC20("Test", "TEST") {
        _mint(address(this), INITIAL_SUPPLY);
    }
  
    function transferTest(address _to, uint256 _amount) public onlyOwner {
        _transfer(address(this), _to, _amount);
    }
}

Is this possible?

3
  • 1
    Yes, you can mint them to the contract. Jun 16, 2023 at 5:58
  • @ZartajAfser ahh great, thank you for confirming. Would you know where in the Ethereum documentation it would confirm this? I'm also trying to understand how the backend of the EVM works. I assume the docs should talk about this or at least something tangential to this Jun 17, 2023 at 4:07
  • There is no specific point where this is confirmed, as it is not a big case to be discussed in the docs. There are multiple resources for EVM. You can check this out. zaryabs.com/ethereum-virtual-machine-article-series Jun 17, 2023 at 16:57

1 Answer 1

0

Well it's your contract, your code. It does whatever you write there. This includes minting the tokens to the contract address itself, if you wish.

2
  • So by doing the above code of minting the tokens to the constructor is fine? I was worried that maybe the address of the contract is done at the end of the construction process or something, that could lead the funds to be lost, or maybe some other weird behavior. I did test out the code in Remix and saw that it seemed to work fine, HOWEVER I didn't want my decision to be based on just anecdotal testing, wanted maybe a confirmation in the documentation or at least other people here giving me some feedback as to how it works with the Ethereum Virtual Machine, what's allowed and what isn't. Jun 17, 2023 at 4:03
  • It's a valid concern. Contract address is available already in the constructor. If, for some impossible reason, it wasn't, your code would simply revert. Jun 19, 2023 at 7:50

Your Answer

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

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