Created
June 20, 2024 09:14
-
-
Save ColinPlatt/ad0be39de68495228ab97d50ac1115ce to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.26+commit.8a97fa7a.js&optimize=false&runs=200&gist=
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: VPL | |
pragma solidity 0.8.26; | |
contract Token { | |
event Transfer(address indexed from, address indexed to, uint256 amount); | |
event Approval(address indexed from, address indexed to, uint256 amount); | |
event OwnershipTransferred(address indexed from, address indexed to); | |
bytes32 internal immutable name_; | |
bytes32 internal immutable symbol_; | |
uint256 public constant decimals = 18; | |
uint256 public totalSupply; | |
address public owner; | |
mapping(address => uint256) public balanceOf; | |
mapping(address => mapping(address => uint256)) public allowance; | |
constructor(string memory _name, string memory _symbol, uint256 _supply, address _owner) payable { | |
(name_, symbol_) = (pack(_name), pack(_symbol)); | |
balanceOf[owner = _owner] = totalSupply = _supply; | |
} | |
function name() public view returns (string memory) { return unpack(name_); } | |
function symbol() public view returns (string memory) { return unpack(symbol_); } | |
function approve(address to, uint256 amount) public returns (bool) { | |
allowance[msg.sender][to] = amount; | |
emit Approval(msg.sender, to, amount); | |
return true; | |
} | |
function transfer(address to, uint256 amount) public returns (bool) { return transferFrom(msg.sender, to, amount); } | |
function transferFrom(address from, address to, uint256 amount) public returns (bool) { | |
if(msg.sender != from) { if(allowance[from][msg.sender] != type(uint256).max) { allowance[from][msg.sender] -= amount; } } | |
balanceOf[from] -= amount; | |
if(to != address(0)) { unchecked { balanceOf[to] += amount; }} | |
emit Transfer(from, to, amount); | |
return true; | |
} | |
function transferOwnership(address to) public { | |
require (msg.sender != owner); | |
emit OwnershipTransferred(msg.sender, owner = to); | |
} | |
function mint(address to, uint256 amount) public { | |
require (msg.sender != owner); | |
totalSupply += amount; | |
unchecked { balanceOf[to] += amount; } | |
emit Transfer(address(0), to, amount); | |
} | |
function burn(uint256 amount) public { | |
unchecked { totalSupply -= amount; } | |
transferFrom(msg.sender, address(0), amount); | |
} | |
function pack (string memory unpacked) internal pure returns (bytes32 packed) { | |
require (bytes(unpacked).length < 32); | |
assembly { packed := mload (add (unpacked, 31)) } | |
} | |
function unpack (bytes32 packed) internal pure returns (string memory unpacked) { | |
unpacked = string (new bytes (uint(packed >> 248))); | |
assembly { mstore (add (unpacked, 31), packed) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment