Cómo crear un contrato múltiple con múltiples instancias

Me gustaría saber cómo crear (a través de otro contrato o algo más) varias instancias del mismo contrato con diferentes parámetros.

Por ejemplo, cree varios ERC20 diferentes bajo demanda con diferentes parámetros, valores, ...

¿También sería posible crear otros tipos de contrato como ERC721, con el mismo principio?

Respuestas (2)

Podrías crear un contrato de fábrica:

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);
}

El patrón de fábrica también es una forma de hacer esto. veamos el siguiente ejemplo:

/**
 * 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()genere siempre la nueva dirección del contrato, es decir, despliegue el contrato en la nueva dirección del contrato. pero la estructura lógica sigue siendo la misma que la del contrato TitanToken.
¡Esto puede ayudarte!