Created
November 8, 2022 13:00
-
-
Save jO-Osko/5d236fe07e4a883364e56113af092cce 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.9; | |
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | |
error InsufficientBalance(uint256 available, uint256 required); | |
contract Token is IERC20Metadata { | |
mapping (address => uint256) private _balances; | |
mapping (address => mapping (address => uint256)) private _allowances; | |
uint256 public override totalSupply; | |
string public override name = "My Fancy Token"; | |
string public override symbol = "MFT"; | |
uint8 public override decimals = 4; | |
constructor (uint256 initialSupply) { | |
_balances[msg.sender] = initialSupply; | |
totalSupply = initialSupply; | |
} | |
function balanceOf(address account) | |
external | |
view | |
override | |
returns (uint256) | |
{ | |
return _balances[account]; | |
} | |
function transfer(address to, uint256 amount) | |
external | |
override | |
returns (bool) | |
{ | |
// Older versions of solidity would need to use string as error message | |
// require(_balances[msg.sender] >= amount, "Not enough tokens"); | |
if (amount > _balances[msg.sender]){ | |
revert InsufficientBalance(_balances[msg.sender], amount); | |
} | |
_balances[msg.sender] -= amount; | |
_balances[to] += amount; | |
emit Transfer(msg.sender, to, amount); | |
return true; | |
} | |
function allowance(address owner, address spender) | |
external | |
view | |
override | |
returns (uint256) | |
{ | |
return _allowances[owner][spender]; | |
} | |
function approve(address spender, uint256 amount) | |
external | |
override | |
returns (bool) | |
{ | |
revert("ERROR"); | |
} | |
function transferFrom( | |
address from, | |
address to, | |
uint256 amount | |
) external override returns (bool) { | |
revert("ERROR"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment