Branch data Line data Source code
1 : : // SPDX-License-Identifier: MIT
2 : : // OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/ERC20.sol)
3 : : pragma solidity ^0.8.10;
4 : :
5 : : import "../interfaces/IERC20.sol";
6 : :
7 : : /**
8 : : * @dev Provides information about the current execution context, including the
9 : : * sender of the transaction and its data. While these are generally available
10 : : * via msg.sender and msg.data, they should not be accessed in such a direct
11 : : * manner, since when dealing with meta-transactions the account sending and
12 : : * paying for execution may not be the actual sender (as far as an application
13 : : * is concerned).
14 : : *
15 : : * This contract is only required for intermediate, library-like contracts.
16 : : */
17 : : abstract contract Context {
18 : : function _msgSender() internal view virtual returns (address) {
19 : 93 : return msg.sender;
20 : : }
21 : :
22 : : function _msgData() internal view virtual returns (bytes calldata) {
23 : 0 : return msg.data;
24 : : }
25 : : }
26 : :
27 : : /**
28 : : * @dev Implementation of the {IERC20} interface.
29 : : *
30 : : * This implementation is agnostic to the way tokens are created. This means
31 : : * that a supply mechanism has to be added in a derived contract using {_mint}.
32 : : * For a generic mechanism see {ERC20PresetMinterPauser}.
33 : : *
34 : : * TIP: For a detailed writeup see our guide
35 : : * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
36 : : * to implement supply mechanisms].
37 : : *
38 : : * The default value of {decimals} is 18. To change this, you should override
39 : : * this function so it returns a different value.
40 : : *
41 : : * We have followed general OpenZeppelin Contracts guidelines: functions revert
42 : : * instead returning `false` on failure. This behavior is nonetheless
43 : : * conventional and does not conflict with the expectations of ERC20
44 : : * applications.
45 : : *
46 : : * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
47 : : * This allows applications to reconstruct the allowance for all accounts just
48 : : * by listening to said events. Other implementations of the EIP may not emit
49 : : * these events, as it isn't required by the specification.
50 : : *
51 : : * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
52 : : * functions have been added to mitigate the well-known issues around setting
53 : : * allowances. See {IERC20-approve}.
54 : : */
55 : : contract PurchaseToken is Context, IERC20, IERC20Metadata {
56 : : mapping(address => uint256) private _balances;
57 : :
58 : : mapping(address => mapping(address => uint256)) private _allowances;
59 : :
60 : : uint256 private _totalSupply;
61 : :
62 : : string private _name;
63 : : string private _symbol;
64 : :
65 : : /**
66 : : * @dev Sets the values for {name} and {symbol}.
67 : : *
68 : : * All two of these values are immutable: they can only be set once during
69 : : * construction.
70 : : */
71 : : constructor() {
72 : : _name = "PurchaseToken";
73 : : _symbol = "PT";
74 : : }
75 : :
76 : : /**
77 : : * @dev Returns the name of the token.
78 : : */
79 : : function name() public view virtual override returns (string memory) {
80 : 1 : return _name;
81 : : }
82 : :
83 : : /**
84 : : * @dev Returns the symbol of the token, usually a shorter version of the
85 : : * name.
86 : : */
87 : : function symbol() public view virtual override returns (string memory) {
88 : 1 : return _symbol;
89 : : }
90 : :
91 : : /**
92 : : * @dev Returns the number of decimals used to get its user representation.
93 : : * For example, if `decimals` equals `18`, a balance of `5e18` tokens should
94 : : * be displayed to a user as `5.00` (`5e18 / 10 ** 18`).
95 : : *
96 : : * Tokens usually opt for a value of 18, imitating the relationship between
97 : : * Ether and Wei. This is the default value returned by this function, unless
98 : : * it's overridden.
99 : : *
100 : : */
101 : : function decimals() public view virtual override returns (uint8) {
102 : 1 : return 18;
103 : : }
104 : :
105 : : /**
106 : : * @dev See {IERC20-totalSupply}.
107 : : */
108 : : function totalSupply() public view virtual override returns (uint256) {
109 : 4 : return _totalSupply;
110 : : }
111 : :
112 : : /**
113 : : * @dev See {IERC20-balanceOf}.
114 : : */
115 : : function balanceOf(address account)
116 : : public
117 : : view
118 : : virtual
119 : : override
120 : : returns (uint256)
121 : : {
122 : 12 : return _balances[account];
123 : : }
124 : :
125 : : /**
126 : : * @dev This allows users to mint ERC20 tokens by sending ETH to this contract.
127 : : * The amount of ERC20 tokens minted will be 100 times the amount of ETH sent.
128 : : */
129 : : function mint() external payable {
130 : 52 : uint256 amountToMint = msg.value * 100;
131 : 52 : _mint(msg.sender, amountToMint);
132 : : }
133 : :
134 : : /**
135 : : * @dev See {IERC20-transfer}.
136 : : *
137 : : * Requirements:
138 : : *
139 : : * - `to` cannot be the zero address.
140 : : * - the caller must have a balance of at least `amount`.
141 : : */
142 : : function transfer(address to, uint256 amount)
143 : : public
144 : : virtual
145 : : override
146 : : returns (bool)
147 : : {
148 : 2 : address owner = _msgSender();
149 : 2 : _transfer(owner, to, amount);
150 : 1 : return true;
151 : : }
152 : :
153 : : /**
154 : : * @dev See {IERC20-allowance}.
155 : : */
156 : : function allowance(address owner, address spender)
157 : : public
158 : : view
159 : : virtual
160 : : override
161 : : returns (uint256)
162 : : {
163 : 88 : return _allowances[owner][spender];
164 : : }
165 : :
166 : : /**
167 : : * @dev See {IERC20-approve}.
168 : : *
169 : : * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
170 : : * `transferFrom`. This is semantically equivalent to an infinite approval.
171 : : *
172 : : * Requirements:
173 : : *
174 : : * - `spender` cannot be the zero address.
175 : : */
176 : : function approve(address spender, uint256 amount)
177 : : public
178 : : virtual
179 : : override
180 : : returns (bool)
181 : : {
182 : 48 : address owner = _msgSender();
183 : 48 : _approve(owner, spender, amount);
184 : 48 : return true;
185 : : }
186 : :
187 : : /**
188 : : * @dev See {IERC20-transferFrom}.
189 : : *
190 : : * Emits an {Approval} event indicating the updated allowance. This is not
191 : : * required by the EIP. See the note at the beginning of {ERC20}.
192 : : *
193 : : * NOTE: Does not update the allowance if the current allowance
194 : : * is the maximum `uint256`.
195 : : *
196 : : * Requirements:
197 : : *
198 : : * - `from` and `to` cannot be the zero address.
199 : : * - `from` must have a balance of at least `amount`.
200 : : * - the caller must have allowance for ``from``'s tokens of at least
201 : : * `amount`.
202 : : */
203 : : function transferFrom(
204 : : address from,
205 : : address to,
206 : : uint256 amount
207 : : ) public virtual override returns (bool) {
208 : 43 : address spender = _msgSender();
209 : 43 : _spendAllowance(from, spender, amount);
210 : 42 : _transfer(from, to, amount);
211 : 40 : return true;
212 : : }
213 : :
214 : : /**
215 : : * @dev Atomically increases the allowance granted to `spender` by the caller.
216 : : *
217 : : * This is an alternative to {approve} that can be used as a mitigation for
218 : : * problems described in {IERC20-approve}.
219 : : *
220 : : * Emits an {Approval} event indicating the updated allowance.
221 : : *
222 : : * Requirements:
223 : : *
224 : : * - `spender` cannot be the zero address.
225 : : */
226 : : function increaseAllowance(address spender, uint256 addedValue)
227 : : public
228 : : virtual
229 : : returns (bool)
230 : : {
231 : 0 : address owner = _msgSender();
232 : 0 : _approve(owner, spender, allowance(owner, spender) + addedValue);
233 : 0 : return true;
234 : : }
235 : :
236 : : /**
237 : : * @dev Atomically decreases the allowance granted to `spender` by the caller.
238 : : *
239 : : * This is an alternative to {approve} that can be used as a mitigation for
240 : : * problems described in {IERC20-approve}.
241 : : *
242 : : * Emits an {Approval} event indicating the updated allowance.
243 : : *
244 : : * Requirements:
245 : : *
246 : : * - `spender` cannot be the zero address.
247 : : * - `spender` must have allowance for the caller of at least
248 : : * `subtractedValue`.
249 : : */
250 : : function decreaseAllowance(address spender, uint256 subtractedValue)
251 : : public
252 : : virtual
253 : : returns (bool)
254 : : {
255 : 0 : address owner = _msgSender();
256 : 0 : uint256 currentAllowance = allowance(owner, spender);
257 [ # # ]: 0 : require(
258 : : currentAllowance >= subtractedValue,
259 : : "ERC20: decreased allowance below zero"
260 : : );
261 : : unchecked {
262 : 0 : _approve(owner, spender, currentAllowance - subtractedValue);
263 : : }
264 : :
265 : 0 : return true;
266 : : }
267 : :
268 : : /**
269 : : * @dev Moves `amount` of tokens from `from` to `to`.
270 : : *
271 : : * This internal function is equivalent to {transfer}, and can be used to
272 : : * e.g. implement automatic token fees, slashing mechanisms, etc.
273 : : *
274 : : * Emits a {Transfer} event.
275 : : *
276 : : * Requirements:
277 : : *
278 : : * - `from` cannot be the zero address.
279 : : * - `to` cannot be the zero address.
280 : : * - `from` must have a balance of at least `amount`.
281 : : */
282 : : function _transfer(
283 : : address from,
284 : : address to,
285 : : uint256 amount
286 : : ) internal virtual {
287 [ # + ]: 44 : require(from != address(0), "ERC20: transfer from the zero address");
288 [ # + ]: 44 : require(to != address(0), "ERC20: transfer to the zero address");
289 : :
290 : 44 : _beforeTokenTransfer(from, to, amount);
291 : :
292 : 44 : uint256 fromBalance = _balances[from];
293 [ + + ]: 44 : require(
294 : : fromBalance >= amount,
295 : : "ERC20: transfer amount exceeds balance"
296 : : );
297 : : unchecked {
298 : 41 : _balances[from] = fromBalance - amount;
299 : : // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
300 : : // decrementing then incrementing.
301 : 41 : _balances[to] += amount;
302 : : }
303 : :
304 : 41 : emit Transfer(from, to, amount);
305 : :
306 : 41 : _afterTokenTransfer(from, to, amount);
307 : : }
308 : :
309 : : /** @dev Creates `amount` tokens and assigns them to `account`, increasing
310 : : * the total supply.
311 : : *
312 : : * Emits a {Transfer} event with `from` set to the zero address.
313 : : *
314 : : * Requirements:
315 : : *
316 : : * - `account` cannot be the zero address.
317 : : */
318 : : function _mint(address account, uint256 amount) internal virtual {
319 [ + + ]: 52 : require(account != address(0), "ERC20: mint to the zero address");
320 : :
321 : 51 : _beforeTokenTransfer(address(0), account, amount);
322 : :
323 : 51 : _totalSupply += amount;
324 : : unchecked {
325 : : // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
326 : 51 : _balances[account] += amount;
327 : : }
328 : 51 : emit Transfer(address(0), account, amount);
329 : :
330 : 51 : _afterTokenTransfer(address(0), account, amount);
331 : : }
332 : :
333 : : /**
334 : : * @dev Destroys `amount` tokens from `account`, reducing the
335 : : * total supply.
336 : : *
337 : : * Emits a {Transfer} event with `to` set to the zero address.
338 : : *
339 : : * Requirements:
340 : : *
341 : : * - `account` cannot be the zero address.
342 : : * - `account` must have at least `amount` tokens.
343 : : */
344 : : function _burn(address account, uint256 amount) internal virtual {
345 [ # # ]: 0 : require(account != address(0), "ERC20: burn from the zero address");
346 : :
347 : 0 : _beforeTokenTransfer(account, address(0), amount);
348 : :
349 : 0 : uint256 accountBalance = _balances[account];
350 [ # # ]: 0 : require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
351 : : unchecked {
352 : 0 : _balances[account] = accountBalance - amount;
353 : : // Overflow not possible: amount <= accountBalance <= totalSupply.
354 : 0 : _totalSupply -= amount;
355 : : }
356 : :
357 : 0 : emit Transfer(account, address(0), amount);
358 : :
359 : 0 : _afterTokenTransfer(account, address(0), amount);
360 : : }
361 : :
362 : : /**
363 : : * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
364 : : *
365 : : * This internal function is equivalent to `approve`, and can be used to
366 : : * e.g. set automatic allowances for certain subsystems, etc.
367 : : *
368 : : * Emits an {Approval} event.
369 : : *
370 : : * Requirements:
371 : : *
372 : : * - `owner` cannot be the zero address.
373 : : * - `spender` cannot be the zero address.
374 : : */
375 : : function _approve(
376 : : address owner,
377 : : address spender,
378 : : uint256 amount
379 : : ) internal virtual {
380 [ # + ]: 90 : require(owner != address(0), "ERC20: approve from the zero address");
381 [ # + ]: 90 : require(spender != address(0), "ERC20: approve to the zero address");
382 : :
383 : 90 : _allowances[owner][spender] = amount;
384 : 90 : emit Approval(owner, spender, amount);
385 : : }
386 : :
387 : : /**
388 : : * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
389 : : *
390 : : * Does not update the allowance amount in case of infinite allowance.
391 : : * Revert if not enough allowance is available.
392 : : *
393 : : * Might emit an {Approval} event.
394 : : */
395 : : function _spendAllowance(
396 : : address owner,
397 : : address spender,
398 : : uint256 amount
399 : : ) internal virtual {
400 : 43 : uint256 currentAllowance = allowance(owner, spender);
401 [ + + ]: 43 : if (currentAllowance != type(uint256).max) {
402 [ + + ]: 43 : require(
403 : : currentAllowance >= amount,
404 : : "ERC20: insufficient allowance"
405 : : );
406 : : unchecked {
407 : 42 : _approve(owner, spender, currentAllowance - amount);
408 : : }
409 : : }
410 : : }
411 : :
412 : : /**
413 : : * @dev Hook that is called before any transfer of tokens. This includes
414 : : * minting and burning.
415 : : *
416 : : * Calling conditions:
417 : : *
418 : : * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
419 : : * will be transferred to `to`.
420 : : * - when `from` is zero, `amount` tokens will be minted for `to`.
421 : : * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
422 : : * - `from` and `to` are never both zero.
423 : : *
424 : : * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
425 : : */
426 : : function _beforeTokenTransfer(
427 : : address from,
428 : : address to,
429 : : uint256 amount
430 : : ) internal virtual {}
431 : :
432 : : /**
433 : : * @dev Hook that is called after any transfer of tokens. This includes
434 : : * minting and burning.
435 : : *
436 : : * Calling conditions:
437 : : *
438 : : * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
439 : : * has been transferred to `to`.
440 : : * - when `from` is zero, `amount` tokens have been minted for `to`.
441 : : * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
442 : : * - `from` and `to` are never both zero.
443 : : *
444 : : * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
445 : : */
446 : : function _afterTokenTransfer(
447 : : address from,
448 : : address to,
449 : : uint256 amount
450 : : ) internal virtual {}
451 : : }
|