So erstellen Sie einen Mehrfachvertrag mit mehreren Instanzen

Ich würde gerne wissen, wie man (über einen anderen Vertrag oder etwas anderes) mehrere Instanzen desselben Vertrags mit unterschiedlichen Parametern erstellt.

Erstellen Sie zum Beispiel bei Bedarf mehrere verschiedene ERC20 mit unterschiedlichen Parametern, Werten, ...

Wäre es auch möglich, andere Vertragstypen wie ERC721 mit demselben Prinzip zu erstellen?

Antworten (2)

Sie könnten einen Fabrikvertrag erstellen:

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

Fabrikmuster ist auch eine Möglichkeit, dies zu tun. sehen wir uns das folgende Beispiel an:

/**
 * 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()Generieren Sie immer die neue Adresse des Vertrags, dh stellen Sie den Vertrag unter der neuen Vertragsadresse bereit. aber die logische Struktur bleibt die gleiche wie beim TitanToken-Vertrag.
Das kann Ihnen helfen!