Solidity ERC-20 – Best Practice to Create Custom ERC20 Token

contract-developmenterc-20soliditytokens

I want to custom ERC20 tokens based on user input.

I can achieve this in two ways

  1. create smart contract that will create new instance of token contracts with details provided by client.

    Doing so, the creator of contract is marked as the parent contract and I'll have to transfer the custom tokens from parent contract to the msg.sender after token creation. How do I make the child contract know that the creator is tx.origin? Can I use delegatecall to achieve this? If so how?

  2. use ethers.js to create token. This will remove the problems as mentioned 1. but i'll need to find a way to keep track of the tokens created (make another transaction to store the data)

What is the best way to do this?

Best Answer

You don't need to use tx.origin or delegatecall(will not work).

Just change the constructor of the child contract by adding a new parameter e.g.

contract ChildContract {
    ...
    constructor(...., address ownerOfChild)...
    ...
}

And in the Factory contract (Parent) use msg.sender e.g.

import `.....ChildContract.sol`

contract Factory {

   function deployChildContract(.....) external {
      ChildContract childContract = new ChildContract(......, msg.sender)
   }
}

And now you have the message sender in your child contract and you can use it as you like.

tx.origin could do the same but it has security risks . Therefore it is better to pass the message sender as a parameter


The benefits of your (2) solution is that you can update and change future contracts.

You don't need 2 transactions, you could deploy 1 Register Smart Contract that store the information you need and in the constructor of your custome ERC20 contract, call the register Smart contract to register the token.

Related Topic