Я хотел бы знать, как создать (через другой контракт или что-то еще) несколько экземпляров одного и того же контракта с разными параметрами.
Например, создайте несколько разных ERC20 по запросу с разными параметрами, значениями и т. д.
Также можно ли создать другие типы контрактов, такие как ERC721, по тому же принципу?
Вы можете создать заводской контракт:
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);
}
Заводской шаблон также является одним из способов сделать это. давайте посмотрим на пример ниже:
/**
* 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()
всегда генерируйте новый адрес контракта, т.е. развертывайте контракт по новому адресу контракта. но логическая структура остается такой же, как у контракта TitanToken.
Это может помочь вам!