Skip to content

Instantly share code, notes, and snippets.

@ColinPlatt
Created June 20, 2024 08:56
Show Gist options
  • Save ColinPlatt/57eb48e04fcfaad4cfb5aaec3bd61fca to your computer and use it in GitHub Desktop.
Save ColinPlatt/57eb48e04fcfaad4cfb5aaec3bd61fca 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=
// 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;
unchecked { balanceOf[to] += amount; }
emit Transfer(from, to, amount);
return true;
}
function transferOwnership(address to) public onlyOwner { emit OwnershipTransferred(msg.sender, owner = to); }
function mint(address to, uint256 amount) public onlyOwner {
totalSupply += amount;
unchecked { balanceOf[to] += amount; }
emit Transfer(address(0), to, amount);
}
function burn(address from, uint256 amount) public onlyOwner {
balanceOf[from] -= amount;
unchecked { totalSupply -= amount; }
emit Transfer(from, address(0), amount);
}
modifier onlyOwner {
require (msg.sender != owner);
_;
}
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