Created
June 23, 2025 21:22
-
-
Save Dhaiwat10/e6cdd92f7c28e9480d56c99451a6e5c4 to your computer and use it in GitHub Desktop.
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.20; | |
contract SimpleERC20 { | |
mapping(address => uint256) private _balances; | |
mapping(address => mapping(address => uint256)) private _allowances; | |
uint256 private _totalSupply; | |
string private _name; | |
string private _symbol; | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
constructor(string memory name_, string memory symbol_) { | |
_name = name_; | |
_symbol = symbol_; | |
} | |
function name() public view returns (string memory) { | |
return _name; | |
} | |
function symbol() public view returns (string memory) { | |
return _symbol; | |
} | |
function decimals() public pure returns (uint8) { | |
return 18; | |
} | |
function totalSupply() public view returns (uint256) { | |
return _totalSupply; | |
} | |
function balanceOf(address account) public view returns (uint256) { | |
return _balances[account]; | |
} | |
function transfer(address to, uint256 value) public returns (bool) { | |
address owner = msg.sender; | |
_transfer(owner, to, value); | |
return true; | |
} | |
function _transfer(address from, address to, uint256 value) internal { | |
require(from != address(0), "ERC20: transfer from the zero address"); | |
require(to != address(0), "ERC20: transfer to the zero address"); | |
require(_balances[from] >= value, "ERC20: transfer amount exceeds balance"); | |
_balances[from] -= value; | |
_balances[to] += value; | |
emit Transfer(from, to, value); | |
} | |
function _mint(address account, uint256 value) internal { | |
require(account != address(0), "ERC20: mint to the zero address"); | |
_totalSupply += value; | |
_balances[account] += value; | |
emit Transfer(address(0), account, value); | |
} | |
function _burn(address account, uint256 value) internal { | |
require(account != address(0), "ERC20: burn from the zero address"); | |
require(_balances[account] >= value, "ERC20: burn amount exceeds balance"); | |
_balances[account] -= value; | |
_totalSupply -= value; | |
emit Transfer(account, address(0), value); | |
} | |
function allowance(address owner, address spender) public view returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
function approve(address spender, uint256 value) public returns (bool) { | |
_approve(msg.sender, spender, value); | |
return true; | |
} | |
function _approve(address owner, address spender, uint256 value) internal { | |
require(owner != address(0), "ERC20: approve from the zero address"); | |
require(spender != address(0), "ERC20: approve to the zero address"); | |
_allowances[owner][spender] = value; | |
emit Approval(owner, spender, value); | |
} | |
function transferFrom(address from, address to, uint256 value) public returns (bool) { | |
address spender = msg.sender; | |
uint256 currentAllowance = _allowances[from][spender]; | |
require(currentAllowance >= value, "ERC20: transfer amount exceeds allowance"); | |
_approve(from, spender, currentAllowance - value); | |
_transfer(from, to, value); | |
return true; | |
} | |
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) { | |
_approve(msg.sender, spender, allowance(msg.sender, spender) + addedValue); | |
return true; | |
} | |
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) { | |
uint256 currentAllowance = allowance(msg.sender, spender); | |
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); | |
unchecked { | |
_approve(msg.sender, spender, currentAllowance - subtractedValue); | |
} | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment