|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; |
| 6 | + |
| 7 | +contract MockERC20 is IERC20, IERC20Metadata { |
| 8 | + string private _name; |
| 9 | + string private _symbol; |
| 10 | + uint8 private _decimals; |
| 11 | + |
| 12 | + mapping(address => uint256) private _balances; |
| 13 | + mapping(address => mapping(address => uint256)) private _allowances; |
| 14 | + |
| 15 | + uint256 private _totalSupply; |
| 16 | + |
| 17 | + constructor() { |
| 18 | + _name = "Mock ERC20"; |
| 19 | + _symbol = "MOCK"; |
| 20 | + _decimals = 18; |
| 21 | + } |
| 22 | + |
| 23 | + function name() public view override returns (string memory) { |
| 24 | + return _name; |
| 25 | + } |
| 26 | + |
| 27 | + function symbol() public view override returns (string memory) { |
| 28 | + return _symbol; |
| 29 | + } |
| 30 | + |
| 31 | + function decimals() public view override returns (uint8) { |
| 32 | + return _decimals; |
| 33 | + } |
| 34 | + |
| 35 | + function totalSupply() public view override returns (uint256) { |
| 36 | + return _totalSupply; |
| 37 | + } |
| 38 | + |
| 39 | + function balanceOf(address account) public view override returns (uint256) { |
| 40 | + return _balances[account]; |
| 41 | + } |
| 42 | + |
| 43 | + function transfer( |
| 44 | + address to, |
| 45 | + uint256 amount |
| 46 | + ) public override returns (bool) { |
| 47 | + address owner = msg.sender; |
| 48 | + _transfer(owner, to, amount); |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + function allowance( |
| 53 | + address owner, |
| 54 | + address spender |
| 55 | + ) public view override returns (uint256) { |
| 56 | + return _allowances[owner][spender]; |
| 57 | + } |
| 58 | + |
| 59 | + function approve( |
| 60 | + address spender, |
| 61 | + uint256 amount |
| 62 | + ) public override returns (bool) { |
| 63 | + address owner = msg.sender; |
| 64 | + _approve(owner, spender, amount); |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + function transferFrom( |
| 69 | + address from, |
| 70 | + address to, |
| 71 | + uint256 amount |
| 72 | + ) public override returns (bool) { |
| 73 | + address spender = msg.sender; |
| 74 | + _spendAllowance(from, spender, amount); |
| 75 | + _transfer(from, to, amount); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + function increaseAllowance( |
| 80 | + address spender, |
| 81 | + uint256 addedValue |
| 82 | + ) public returns (bool) { |
| 83 | + address owner = msg.sender; |
| 84 | + _approve(owner, spender, allowance(owner, spender) + addedValue); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + function decreaseAllowance( |
| 89 | + address spender, |
| 90 | + uint256 subtractedValue |
| 91 | + ) public returns (bool) { |
| 92 | + address owner = msg.sender; |
| 93 | + uint256 currentAllowance = allowance(owner, spender); |
| 94 | + require( |
| 95 | + currentAllowance >= subtractedValue, |
| 96 | + "ERC20: decreased allowance below zero" |
| 97 | + ); |
| 98 | + unchecked { |
| 99 | + _approve(owner, spender, currentAllowance - subtractedValue); |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + function mint(address account, uint256 amount) public { |
| 105 | + require(account != address(0), "ERC20: mint to the zero address"); |
| 106 | + _totalSupply += amount; |
| 107 | + _balances[account] += amount; |
| 108 | + emit Transfer(address(0), account, amount); |
| 109 | + } |
| 110 | + |
| 111 | + function burn(address account, uint256 amount) public { |
| 112 | + require(account != address(0), "ERC20: burn from the zero address"); |
| 113 | + uint256 accountBalance = _balances[account]; |
| 114 | + require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); |
| 115 | + unchecked { |
| 116 | + _balances[account] = accountBalance - amount; |
| 117 | + } |
| 118 | + _totalSupply -= amount; |
| 119 | + emit Transfer(account, address(0), amount); |
| 120 | + } |
| 121 | + function burnFrom(address account, uint256 amount) public { |
| 122 | + require(account != address(0), "ERC20: burn from the zero address"); |
| 123 | + uint256 accountBalance = _balances[account]; |
| 124 | + require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); |
| 125 | + unchecked { |
| 126 | + _balances[account] = accountBalance - amount; |
| 127 | + } |
| 128 | + _totalSupply -= amount; |
| 129 | + emit Transfer(account, address(0), amount); |
| 130 | + } |
| 131 | + |
| 132 | + function _transfer(address from, address to, uint256 amount) internal { |
| 133 | + require(from != address(0), "ERC20: transfer from the zero address"); |
| 134 | + require(to != address(0), "ERC20: transfer to the zero address"); |
| 135 | + |
| 136 | + uint256 fromBalance = _balances[from]; |
| 137 | + require( |
| 138 | + fromBalance >= amount, |
| 139 | + "ERC20: transfer amount exceeds balance" |
| 140 | + ); |
| 141 | + unchecked { |
| 142 | + _balances[from] = fromBalance - amount; |
| 143 | + } |
| 144 | + _balances[to] += amount; |
| 145 | + |
| 146 | + emit Transfer(from, to, amount); |
| 147 | + } |
| 148 | + |
| 149 | + function _approve(address owner, address spender, uint256 amount) internal { |
| 150 | + require(owner != address(0), "ERC20: approve from the zero address"); |
| 151 | + require(spender != address(0), "ERC20: approve to the zero address"); |
| 152 | + |
| 153 | + _allowances[owner][spender] = amount; |
| 154 | + emit Approval(owner, spender, amount); |
| 155 | + } |
| 156 | + |
| 157 | + function _spendAllowance( |
| 158 | + address owner, |
| 159 | + address spender, |
| 160 | + uint256 amount |
| 161 | + ) internal { |
| 162 | + uint256 currentAllowance = allowance(owner, spender); |
| 163 | + if (currentAllowance != type(uint256).max) { |
| 164 | + require( |
| 165 | + currentAllowance >= amount, |
| 166 | + "ERC20: insufficient allowance" |
| 167 | + ); |
| 168 | + unchecked { |
| 169 | + _approve(owner, spender, currentAllowance - amount); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments