Skip to content

Instantly share code, notes, and snippets.

@cygnusv
Created February 20, 2023 12:11
Show Gist options
  • Save cygnusv/7f527e3bb73dbf86d76a3177b63a5898 to your computer and use it in GitHub Desktop.
Save cygnusv/7f527e3bb73dbf86d76a3177b63a5898 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPLv3
pragma solidity ^0.8;
//
// __ __ .___ ___. ___ .___________. ______ __ ___ _______ .__ __.
// | | | | | \/ | / \ | | / __ \ | |/ / | ____|| \ | |
// | | | | | \ / | / ^ \ `---| |----`| | | | | ' / | |__ | \| |
// | | | | | |\/| | / /_\ \ | | | | | | | < | __| | . ` |
// | `--' | | | | | / _____ \ | | | `--' | | . \ | |____ | |\ |
// \______/ |__| |__| /__/ \__\ |__| \______/ |__|\__\ |_______||__| \__|
//
//
// Based on https://theethereum.wiki/w/index.php/ERC20_Token_Standard
// ----------------------------------------------------------------------------
// Interface for ERC-20 Token Standard
// https://eips.ethereum.org/EIPS/eip-20
// ----------------------------------------------------------------------------
interface ERC20Interface {
function totalSupply() external view returns (uint);
function balanceOf(address tokenOwner) external view returns (uint balance);
function transfer(address to, uint tokens) external returns (bool success);
function approve(address spender, uint tokens) external returns (bool success);
function allowance(address tokenOwner, address spender) external view returns (uint remaining);
function transferFrom(address from, address to, uint tokens) external returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
// ----------------------------------------------------------------------------
// ERC20 Token, with the addition of symbol, name and decimals and assisted
// token transfers
// ----------------------------------------------------------------------------
contract UMAToken is ERC20Interface {
string public constant symbol = "UMA";
string public constant name = "UMA Token";
uint8 public constant decimals = 2;
uint public immutable _totalSupply;
// State variables
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
constructor() {
_totalSupply = 1000000 * (10**uint(decimals));
balances[msg.sender] = _totalSupply;
}
// ------------------------------------------------------------------------
// Total supply
// ------------------------------------------------------------------------
function totalSupply() public view returns (uint) {
return _totalSupply;
}
// ------------------------------------------------------------------------
// Get the token balance for account tokenOwner
// ------------------------------------------------------------------------
function balanceOf(address tokenOwner) public view returns (uint balance) {
return balances[tokenOwner];
}
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to to account
// - Owner's account must have sufficient balance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = balances[msg.sender] - tokens;
balances[to] = balances[to] + tokens;
emit Transfer(msg.sender, to, tokens);
return true;
}
// ------------------------------------------------------------------------
// Token owner can approve for spender to transferFrom(...) tokens
// from the token owner's account
//
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// recommends that there are no checks for the approval double-spend attack
// as this should be implemented in user interfaces
// ------------------------------------------------------------------------
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------
function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
return allowed[tokenOwner][spender];
}
// ------------------------------------------------------------------------
// Transfer tokens from the from account to the to account
//
// The calling account must already have sufficient tokens approve(...)-d
// for spending from the from account and
// - From account must have sufficient balance to transfer
// - Spender must have sufficient allowance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = balances[from] - tokens;
allowed[from][msg.sender] = allowed[from][msg.sender] - tokens;
balances[to] = balances[to] + tokens;
emit Transfer(from, to, tokens);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment