76 lines
2.9 KiB
Solidity
76 lines
2.9 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "../interfaces/IERC20.sol";
|
|
import "../libraries/SafeMath.sol";
|
|
|
|
contract ERC20Token is IERC20 {
|
|
using SafeMath for uint256;
|
|
|
|
// The name of the token
|
|
string public name;
|
|
// The symbol of the token
|
|
string public symbol;
|
|
// The number of decimals for the token
|
|
uint8 public decimals;
|
|
// Total supply of tokens
|
|
uint256 private _totalSupply;
|
|
|
|
// Mapping from account addresses to their token balance
|
|
mapping(address => uint256) private _balances;
|
|
// Mapping from account addresses to a mapping of allowed spender addresses and their allowances
|
|
mapping(address => mapping(address => uint256)) private _allowances;
|
|
|
|
// Event emitted when tokens are transferred
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
// Event emitted when an approval is made
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
|
|
constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 initialSupply) {
|
|
name = _name;
|
|
symbol = _symbol;
|
|
decimals = _decimals;
|
|
_totalSupply = initialSupply * 10 ** uint256(decimals);
|
|
_balances[msg.sender] = _totalSupply;
|
|
emit Transfer(address(0), msg.sender, _totalSupply);
|
|
}
|
|
|
|
function totalSupply() public view override returns (uint256) {
|
|
return _totalSupply;
|
|
}
|
|
|
|
function balanceOf(address account) public view override returns (uint256) {
|
|
return _balances[account];
|
|
}
|
|
|
|
function transfer(address recipient, uint256 amount) public override returns (bool) {
|
|
require(recipient != address(0), "ERC20: transfer to the zero address");
|
|
require(_balances[msg.sender] >= amount, "ERC20: transfer amount exceeds balance");
|
|
|
|
_balances[msg.sender] = _balances[msg.sender].sub(amount);
|
|
_balances[recipient] = _balances[recipient].add(amount);
|
|
|
|
emit Transfer(msg.sender, recipient, amount);
|
|
return true;
|
|
}
|
|
|
|
function allowance(address owner, address spender) public view override returns (uint256) {
|
|
return _allowances[owner][spender];
|
|
}
|
|
|
|
function approve(address spender, uint256 amount) public override returns (bool) {
|
|
require(spender != address(0), "ERC20: approve to the zero address");
|
|
|
|
_allowances[msg.sender][spender] = amount;
|
|
emit Approval(msg.sender, spender, amount);
|
|
return true;
|
|
}
|
|
|
|
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
|
|
require(sender != address(0), "ERC20: transfer from the zero address");
|
|
require(recipient != address(0), "ERC20: transfer to the zero address");
|
|
require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");
|
|
require(_allowances[sender][msg.sender] >= amount, "ERC20: transfer amount exceeds allowance");
|
|
}
|
|
}
|