Created
September 3, 2021 16:33
-
-
Save Ro5s/f1c35b0a6fb995bdfb859559022a5915 to your computer and use it in GitHub Desktop.
permissioned factory for erc20 tokens with an admin set on construction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// SPDX-License-Identifier: GPL-3.0-or-later | |
pragma solidity ^0.8.0; | |
/// @notice Basic ERC20 implementation. | |
contract Token { | |
string public name; | |
string public symbol; | |
uint8 constant public decimals = 18; | |
uint public totalSupply; | |
mapping(address => uint) public balanceOf; | |
mapping(address => mapping(address => uint)) public allowance; | |
event Transfer(address indexed from, address indexed to, uint amount); | |
event Approval(address indexed owner, address indexed spender, uint amount); | |
constructor(address owner, string memory _name, string memory _symbol, uint _totalSupply) { | |
name = _name; | |
symbol = _symbol; | |
totalSupply = _totalSupply; | |
balanceOf[owner] = _totalSupply; | |
emit Transfer(address(0), owner, _totalSupply); | |
} | |
function approve(address to, uint amount) external returns (bool) { | |
allowance[msg.sender][to] = amount; | |
emit Approval(msg.sender, to, amount); | |
return true; | |
} | |
function transfer(address to, uint amount) external returns (bool) { | |
balanceOf[msg.sender] -= amount; | |
balanceOf[to] += amount; | |
emit Transfer(msg.sender, to, amount); | |
return true; | |
} | |
function transferFrom(address from, address to, uint amount) external returns (bool) { | |
if (allowance[from][msg.sender] != type(uint).max) | |
allowance[from][msg.sender] -= amount; | |
balanceOf[from] -= amount; | |
balanceOf[to] += amount; | |
emit Transfer(from, to, amount); | |
return true; | |
} | |
} | |
contract Factory { | |
address immutable admin; | |
event DeployToken(Token indexed newToken); | |
constructor(address _admin) { | |
admin = _admin; | |
} | |
function deployToken(address owner, string memory _name, string memory _symbol, uint _totalSupply) external returns (Token newToken) { | |
require(admin == msg.sender, "NOT_ADMIN"); | |
newToken = new Token(owner, _name, _symbol, _totalSupply); | |
emit DeployToken(newToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment