22
loading...
This website collects cookies to deliver better user experience
Write Contract
tab.new
keyword. new
keyword works similarly to javascript in that it creates a new instance. In this case, a new deployed contract instance.ApetasticERC20Factory
, you can see that we first need to:ApetasticERC20.sol
.new
keyword in a function using the name of the contract to deploy a new instance. allTokens
array. ApetasticERC20 token
is also more than just an address. It also represents an ApetasticERC20
interface which means uint256 tokenTotalSupply = token.totalSupply()
would also be possible. This allows your factory contract to call methods on the newly deployed contract for configuration or other purposes./* from ApetasticERC20Factory contract */
import "./ApetasticERC20.sol";
/// @notice Create a new ERC-20 contract
/// @dev Tokens created have 18 decimals which means add 18 zeros the integer supply: 000000000000000000
/// @param name The name of the token
/// @param symbol The symbol of the token
/// @param supply The totalSupply of the token
function createToken(string memory name, string memory symbol, uint256 supply) external returns (address) {
ApetasticERC20 token = new ApetasticERC20(name, symbol, msg.sender, supply);
allTokens.push(address(token));
emit TokenCreated(address(token), supply);
return address(token);
}
ApetasticERC20
contract is an extension of OpenZeppelin's ERC20
implementation. This was done so the proper parameters could be set in the constructor
of the token. You could take a similar approach and add a bit more functionality if needed. _mint
function and sending it to the mintTo
address. msg.sender
would actually send the tokens to the factory which could lock them. To avoid this, the factory passes its msg.sender
(the address that initiated the creation in this case) into ApetasticERC20
so that the tokens are sent to the correct address./* from ApetasticERC20 contract */
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/// @notice Create an ERC20 token with adjustable initial parameters
contract ApetasticERC20 is ERC20 {
/// @param name The name of the token
/// @param symbol The symbol of the token
/// @param mintTo The address that the initial supply should be sent to
/// @param supply The totalSupply of the token
constructor(
string memory name,
string memory symbol,
address mintTo,
uint256 supply
) ERC20(name, symbol) {
_mint(mintTo, supply);
}
}