-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtokenbuilder.sol
234 lines (196 loc) · 7.29 KB
/
tokenbuilder.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
SPDX-License-Identifier: CC-BY-4.0
(c) Desenvolvido por Jeff Prestes
This work is licensed under a Creative Commons Attribution 4.0 International License.
*/
pragma solidity 0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/// @title Manages the contract owner
contract Owned {
address payable contractOwner;
constructor() {
contractOwner = payable(msg.sender);
}
function whoIsTheOwner() public view returns(address) {
return contractOwner;
}
function changeOwner(address payable _newOwner) public {
require(contractOwner == msg.sender);
contractOwner = _newOwner;
}
}
/// @title Mortal allows the owner to kill the contract
contract Mortal is Owned {
function kill() public {
require(msg.sender==contractOwner, "Only owner can destroy the contract");
selfdestruct(contractOwner);
}
}
/// @title A Token Builder
/// @notice Use this contract to create another token
contract TokenBuilder {
/// @notice Alerts when new token was created
event NewTokenCreated(address newTokenAddress, string tokenName);
/// @notice Creates a new Token
function newToken(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _initialSupply)
external
{
TokenERC20 token = new TokenERC20(_name, _symbol, _decimals, _initialSupply, payable(msg.sender));
require(address(token) != address(0), "Token was not created");
emit NewTokenCreated(address(token), token.name());
}
}
/// @title ERC-20 Token template
/// @dev It is used work as template to create new ERC-20 token by Token Builder
contract TokenERC20 is IERC20, Mortal {
string private myName;
string private mySymbol;
uint256 private myTotalSupply;
uint8 public decimals;
mapping (address=>uint256) balances;
mapping (address=>mapping (address=>uint256)) ownerAllowances;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _initialSupply,
address payable _contractOwner)
{
require(_initialSupply<=999999999999, "High amount");
myName = _name;
mySymbol = _symbol;
decimals = _decimals;
changeOwner(_contractOwner);
_mint(_contractOwner, (_initialSupply * (10 ** decimals)));
}
function name() public view returns(string memory) {
return myName;
}
function symbol() public view returns(string memory) {
return mySymbol;
}
function totalSupply() public override view returns(uint256) {
return myTotalSupply;
}
function balanceOf(address tokenOwner) public override view returns(uint256) {
return balances[tokenOwner];
}
function allowance(address tokenOwner, address spender) public override view returns(uint256) {
return ownerAllowances[tokenOwner][spender];
}
function transfer(address to, uint256 amount) public override hasEnoughBalance(msg.sender, amount) tokenAmountValid(amount) returns(bool) {
balances[msg.sender] = balances[msg.sender] - amount;
balances[to] = balances[to] + amount;
emit Transfer(msg.sender, to, amount);
return true;
}
function approve(address spender, uint limit) public override returns(bool) {
ownerAllowances[msg.sender][spender] = limit;
emit Approval(msg.sender, spender, limit);
return true;
}
function transferFrom(address from, address to, uint256 amount) public override
hasEnoughBalance(from, amount) isAllowed(msg.sender, from, amount) tokenAmountValid(amount)
returns(bool) {
balances[from] = balances[from] - amount;
balances[to] += amount;
ownerAllowances[from][msg.sender] = amount;
emit Transfer(from, to, amount);
return true;
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
myTotalSupply = myTotalSupply + amount;
balances[account] = balances[account] + amount;
emit Transfer(address(0), account, amount);
}
function purchase() public payable {
require(msg.value >= 1 ether);
transfer(msg.sender, 100);
contractOwner.transfer(msg.value);
}
modifier hasEnoughBalance(address owner, uint amount) {
uint balance;
balance = balances[owner];
require (balance >= amount);
_;
}
modifier isAllowed(address spender, address tokenOwner, uint amount) {
require (amount <= ownerAllowances[tokenOwner][spender]);
_;
}
modifier tokenAmountValid(uint256 amount) {
require(amount > 0);
require(amount <= myTotalSupply);
_;
}
}