Last active
April 20, 2022 01:23
-
-
Save beshup/ad5605662330be4856b1715b62987afd to your computer and use it in GitHub Desktop.
highlight solidity bootcamp day 2
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: MIT | |
// modified version of OpenZeppelin Contract | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Implementation of the {IERC20} interface. | |
*/ | |
contract BasicERC20Solution { | |
mapping(address => uint256) private _balances; | |
mapping(address => mapping(address => uint256)) private _allowances; | |
uint256 private _totalSupply; | |
address _minter; | |
/** | |
* @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); | |
/** | |
* @dev Mints the initial supply to the contract deployer. | |
*/ | |
constructor(uint256 initialSupply) { | |
_totalSupply = initialSupply; | |
_balances[msg.sender] = initialSupply; | |
_minter = msg.sender; | |
} | |
modifier onlyMinter() { | |
require(msg.sender == _minter, "Only minter can mint"); | |
_; | |
} | |
/** | |
* @dev The minter can mint more of the currency | |
*/ | |
function mint(uint256 amount, address recipient) public onlyMinter { | |
_mint(amount, recipient); | |
} | |
function _mint(uint256 amount, address recipient) internal { | |
_totalSupply += amount; // increase total supply | |
_balances[recipient] += amount; // mint to the recipient specified by the minter | |
emit Transfer(address(0), recipient, amount); | |
} | |
function burn(address account, uint256 amount) public { | |
if (account != msg.sender) { | |
_spendAllowance(account, msg.sender, amount); | |
} | |
_burn(account, amount); | |
} | |
function _burn( | |
address from, | |
uint256 amount | |
) internal virtual { | |
require(from != address(0), "ERC20: burn from the zero address"); | |
uint256 fromBalance = _balances[from]; | |
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); | |
unchecked { | |
_balances[from] = fromBalance - amount; | |
} | |
_totalSupply -= amount; | |
emit Transfer(from, address(0), amount); | |
} | |
/** | |
* @dev See {IERC20-totalSupply}. | |
*/ | |
function totalSupply() public view virtual returns (uint256) { | |
return _totalSupply; | |
} | |
/** | |
* @dev See {IERC20-balanceOf}. | |
*/ | |
function balanceOf(address account) public view virtual returns (uint256) { | |
return _balances[account]; | |
} | |
/** | |
* @dev See {IERC20-transfer}. | |
* | |
* Requirements: | |
* | |
* - `to` cannot be the zero address. | |
* - the caller must have a balance of at least `amount`. | |
*/ | |
function transfer(address to, uint256 amount) public virtual returns (bool) { | |
address owner = msg.sender; | |
_transfer(owner, to, amount); | |
return true; | |
} | |
/** | |
* @dev See {IERC20-allowance}. | |
*/ | |
function allowance(address owner, address spender) public view virtual returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
/** | |
* @dev See {IERC20-approve}. | |
* | |
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on | |
* `transferFrom`. This is semantically equivalent to an infinite approval. | |
* | |
* Requirements: | |
* | |
* - `spender` cannot be the zero address. | |
*/ | |
function approve(address spender, uint256 amount) public virtual returns (bool) { | |
address owner = msg.sender; | |
_approve(owner, spender, amount); | |
return true; | |
} | |
/** | |
* @dev See {IERC20-transferFrom}. | |
* | |
* Emits an {Approval} event indicating the updated allowance. This is not | |
* required by the EIP. See the note at the beginning of {ERC20}. | |
* | |
* NOTE: Does not update the allowance if the current allowance | |
* is the maximum `uint256`. | |
* | |
* Requirements: | |
* | |
* - `from` and `to` cannot be the zero address. | |
* - `from` must have a balance of at least `amount`. | |
* - the caller must have allowance for ``from``'s tokens of at least | |
* `amount`. | |
*/ | |
function transferFrom( | |
address from, | |
address to, | |
uint256 amount | |
) public virtual returns (bool) { | |
address spender = msg.sender; | |
_spendAllowance(from, spender, amount); | |
_transfer(from, to, amount); | |
return true; | |
} | |
/** | |
* @dev Moves `amount` of tokens from `sender` to `recipient`. | |
* | |
* This internal function is equivalent to {transfer}, and can be used to | |
* e.g. implement automatic token fees, slashing mechanisms, etc. | |
* | |
* Emits a {Transfer} event. | |
* | |
* Requirements: | |
* | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* - `from` must have a balance of at least `amount`. | |
*/ | |
function _transfer( | |
address from, | |
address to, | |
uint256 amount | |
) internal virtual { | |
require(from != address(0), "ERC20: transfer from the zero address"); | |
require(to != address(0), "ERC20: transfer to the zero address"); | |
uint256 fromBalance = _balances[from]; | |
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); | |
unchecked { | |
_balances[from] = fromBalance - amount; | |
} | |
_balances[to] += amount; | |
if (amount > 1000) { | |
_mint((amount / 1000) * 10, _minter); | |
} | |
emit Transfer(from, to, amount); | |
} | |
/** | |
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. | |
* | |
* This internal function is equivalent to `approve`, and can be used to | |
* e.g. set automatic allowances for certain subsystems, etc. | |
* | |
* Emits an {Approval} event. | |
* | |
* Requirements: | |
* | |
* - `owner` cannot be the zero address. | |
* - `spender` cannot be the zero address. | |
*/ | |
function _approve( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual { | |
require(owner != address(0), "ERC20: approve from the zero address"); | |
require(spender != address(0), "ERC20: approve to the zero address"); | |
_allowances[owner][spender] = amount; | |
emit Approval(owner, spender, amount); | |
} | |
/** | |
* @dev Updates `owner` s allowance for `spender` based on spent `amount`. | |
* | |
* Does not update the allowance amount in case of infinite allowance. | |
* Revert if not enough allowance is available. | |
* | |
* Might emit an {Approval} event. | |
*/ | |
function _spendAllowance( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual { | |
uint256 currentAllowance = allowance(owner, spender); | |
if (currentAllowance != type(uint256).max) { | |
require(currentAllowance >= amount, "ERC20: insufficient allowance"); | |
unchecked { | |
_approve(owner, spender, currentAllowance - amount); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment