Skip to content

Instantly share code, notes, and snippets.

@marvelous-007
Created February 9, 2023 11:38
Show Gist options
  • Save marvelous-007/de7060aeb4af674d8187c0fc77ab2447 to your computer and use it in GitHub Desktop.
Save marvelous-007/de7060aeb4af674d8187c0fc77ab2447 to your computer and use it in GitHub Desktop.
an erc-20 token where a user can transfer and burn token
// SPDX-License-Identifier: GPL-3.0
// @author Johnson Marvelous
// @title ERC-20
pragma solidity 0.8.17;
contract W3BVIII{
// @notice this is the address of the owner
address public owner;
// @notice this is the name of your token
string private name;
// @notice this is the symbol of the token
string private symbol;
// @notice this represent the decimal the token has
uint256 private decimal;
// @notice this is the total supply of the token
uint private totalSupply;
// @notice this maps this address to the balance
mapping (address => uint256) private balanceOf;
// @notice this maps the owner address to the spender to the amount allowed
mapping (address =>mapping(address => uint)) public allowance;
// @notice this emits after the transfer has been completed
event transfer_(address indexed from, address to, uint amount);
// @notice this event run after minting more token;
event _mint(address indexed from, address to, uint amount);
// @notice this initialize the owner to the sender, and the decimal to 1e18
constructor(string memory _name, string memory _symbol){
owner = msg.sender;
name = _name;
symbol = _symbol;
decimal = 1e18;
}
// @return this function returns the name of the token
function name_() public view returns(string memory){
return name;
}
// @return this function returns the token symbol
function symbol_() public view returns(string memory){
return symbol;
}
// @return this function returns the token decimal
function _decimal() public view returns(uint256){
return decimal;
}
// @return this function returns the totalsupply issued to the token
function _totalSupply() public view returns(uint256){
return totalSupply;
}
// @notice this function displays the balance of the owner
// @params who is the owner of the token
function _balanceOf(address who) public view returns(uint256){
return balanceOf[who];
}
// @notice this function transfers token to a specific address
// @params _to is the address of the receiver
// @params amount is the amount of token you want to send
// @dev this emit the sender, reciever and the amount when completed
function transfer(address _to, uint amount)public {
_transfer(msg.sender, _to, amount);
emit transfer_(msg.sender, _to, amount);
}
// @notice this function contains the logic to transfer token from one address to another
// @params from is the owner of the token address
// @params to is the reciever address
// @params amount is the amount of token the owner wants to send
// @dev the amount you want to send has to be less than or equal to the amount the owner has
// @dev the address you are sending to has to be valid
function _transfer(address from, address to, uint amount) internal {
require(balanceOf[from] >= amount, "insufficient fund");
require(to != address(0), "transferr to address(0)");
balanceOf[from] -= amount;
balanceOf[to] += amount;
}
// @notice this function allows the spender to spend the owner's token
// @params _owner is the owner of the token address
// @params spender is the address of the spender
// @return ths function returns the amount the spender is allowed to spend
function _allowance(address _owner, address spender) public view returns(uint amount){
amount = allowance[_owner][spender];
}
// @notice this function transfers the amount allowed by the owner to an address
// @params from is the address of the owner of the token
// @params to is the address of the receiver
// @params amount is the amount allowed by the owner to spend
// @return is returns a boolean true is the transaction is successful
// @dev the amount you want to send must be less than or equal to the amount allowed by the owner
function transferFrom(address from, address to, uint amount) public returns(bool success){
uint value = _allowance(from, msg.sender);
require( amount <= value, "insufficient allowance");
allowance[from][msg.sender] -= amount;
_transfer(from, to, amount);
success =true;
emit transfer_(from, to, amount);
}
// @notice this functin approves the spender to be able to spend the token allowed
// @params spender is the address of the one spending the allowed token
// @params amount is the amount the spender is allowed to spend
// @dev only the owner can approve the spender
function Approve(address spender, uint amount) public {
allowance[msg.sender][spender] += amount;
}
// @notice this functions mints more token on the contract
// @params to is the address that wants to mint more token
// @params amount this is the amount of token you want to mint
// @dev only the owner can mint more token and the address must be valid
function mint(address to, uint amount) public {
require(msg.sender == owner, "Access Denied");
require(to != address(0), "transferr to address(0)");
totalSupply += amount;
balanceOf[to] += amount * _decimal();
emit _mint(address(0), to, amount);
}
// @notice this function burns the an amount of token specified by the owner
// @params amount this is the amount of token the owner wants to burn
// @dev 10% of the burnt token get sents to the owner
function burn(uint amount) public {
require(balanceOf[msg.sender] >= amount, "Insufficient balance");
uint transferAmount = amount / 10;
uint burnAmount = amount - transferAmount;
_transfer(msg.sender, owner, transferAmount);
balanceOf[msg.sender] -= burnAmount;
balanceOf[address(0)] += burnAmount;
totalSupply -= burnAmount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment