Last active
August 15, 2022 11:46
-
-
Save einnar82/c031de19ee80b9709c2425fda37fc796 to your computer and use it in GitHub Desktop.
ERC20 Token standard
This file contains hidden or 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 | |
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); | |
} |
This file contains hidden or 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 | |
pragma solidity ^0.8.0; | |
import "./IERC20.sol"; | |
contract OToken is IERC20 { | |
uint256 private _totalSupply; | |
mapping(address => uint256) private _balances; | |
mapping(address => mapping(address => uint256)) private _allowances; | |
string public name; | |
string public symbol; | |
constructor(string memory name_, string memory symbol_, uint256 totalSupply_) { | |
name = name_; | |
symbol = symbol_; | |
_balances[msg.sender] = totalSupply_; | |
_totalSupply = totalSupply_; | |
} | |
/** | |
* @dev Returns the amount of tokens in existence. | |
* Should return total circulating supply. | |
*/ | |
function totalSupply() public view override returns (uint256) { | |
return _totalSupply; | |
} | |
/** | |
* @dev Returns the amount of tokens owned by `account`. | |
*/ | |
function balanceOf(address account) public view override returns (uint256) { | |
return _balances[account]; | |
} | |
/** | |
* @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) public view override returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
/** | |
* @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) public override returns (bool) { | |
// place validations; | |
// 1. check if recipient is not address 0 | |
// 2. Check balance if sufficient. | |
// 3. Update _balances. | |
// 4. Emit Transfer event. | |
require(recipient != address(0), "invalid transfer to zero address."); | |
require(amount <= _balances[msg.sender], "insufficient balance."); | |
// update balance | |
// 1. Update sender balance. - subtract | |
// 2. Update recipient balance - add | |
_balances[msg.sender] -= amount; | |
_balances[recipient] += amount; | |
emit Transfer(msg.sender, recipient, amount); | |
return true; | |
} | |
/** | |
* @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) public override returns (bool) { | |
// validations | |
// 1. spender != address(0) check address is address 0 | |
// 2. update balances | |
// 3. emit Approval event | |
require(spender != address(0), "invalid approval to zero address."); | |
// override previous allowance | |
_allowances[msg.sender][spender] = amount; | |
emit Approval(msg.sender, spender, amount); | |
return true; | |
} | |
/** | |
* @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. | |
* msg.sender = spender | |
* sender = owner | |
*/ | |
function transferFrom( | |
address sender, | |
address recipient, | |
uint256 amount | |
) public override returns (bool) { | |
// validations | |
// 1. check sender if address 0 | |
// 2. check recipient if address 0 | |
// 3. check allowance | |
// 4. check balance of owner | |
// 5. update balances (sender, recipient) | |
// 6. update allowance of owner spender | |
// 7. emit transfer and approval events | |
require(sender != address(0), "invalid transfer from zero address."); | |
require(recipient != address(0), "invalid transfer to zero address."); | |
require(_allowances[sender][msg.sender] >= amount, "insufficient allowance."); | |
require(_balances[sender] >= amount, "insufficient balance."); | |
_balances[sender] -= amount; | |
_balances[recipient] -= amount; | |
uint256 updatedAllowance = _allowances[sender][msg.sender] - amount; | |
_allowances[sender][msg.sender] = updatedAllowance; | |
emit Transfer(sender, recipient, amount); | |
emit Approval(sender, msg.sender, amount); | |
return true; | |
} | |
// transfer from needs to call approve | |
// but if transfer only, no need. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment