2

I would like to know how to create (via another contract or something else) several instances of the same contract with different parameters.

For example create several different ERC20 on demand with different parameters, values, ...

Also would it be possible to create other types of contract like ERC721, with the same principle?

4

You could create a factory contract:

pragma solidity ^0.4.24;

// Import and then rename the OpenZeppelin contract stubs for ERC20 and ERC721 contract
import {StandardToken as StandardERC20} from "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
import {ERC721Token as StandardERC721} from "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC721/ERC721Token.sol";

contract ERC20Token is StandardERC20 {
    string public name;
    string public symbol;
    uint8 public decimals;

    constructor(string _name, string _symbol, uint8 _decimals) public {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;
    }
}

contract ERC721Token is StandardERC721 {
    constructor(string _name, string _symbol)
    public
    StandardERC721(_name, _symbol)
    {
    }
}

contract TokenFactory {
    /* @dev Creates a new ERC20Token with the given name, symbol and number of decimals.
        Logs an event with the address of the token and its parameters
    */
    function newERC20(string _name, string _symbol, uint8 _decimals) public {
        emit ERC20Created(new ERC20Token(_name, _symbol, _decimals), _name, _symbol, _decimals);
    }

    /* @dev Creates a new ERC721Token with the given name and symbol.            
        Logs an event with the address of the token and its parameters               
    */
    function newERC721(string _name, string _symbol) public {
        emit ERC721Created(new ERC721Token(_name, _symbol), _name, _symbol);
    }

    event ERC20Created(ERC20Token indexed tokenAddress, string indexed name, string indexed symbol, uint8 decimals);
    event ERC721Created(ERC721Token tokenAddress, string name, string symbol);
}
1

Factory pattern is also one way to do this. lets see the example below:

/**
 * Constrctor function
 *
 * Initializes contract with initial supply tokens to the creator of the contract
 */
function TitanToken(uint256 _initialSupply, bytes32 _tokenName, bytes32 _tokenSymbol) public {
    totalSupply = _initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
    name = _tokenName;                                   // Set the name for display purposes
    symbol = _tokenSymbol;                               // Set the symbol for display purposes
}    

 import "./TitanToken.sol";

  //Generating new Token . 
    function newToken(uint256 _initialSupply, bytes32 _name, bytes32 _symbol) 
    public
    returns(address, bytes32){
        TitanToken T = new TitanToken(_initialSupply,_name,_symbol);
        return (T, _name);
}

newToken() always generate the new address of the contract i.e deploy the contract in new contract address. but the logical structure remain same as the TitanToken contract.
This may help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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