Skip to content

Instantly share code, notes, and snippets.

@rgottleber
Created August 28, 2021 19:12
Show Gist options
  • Save rgottleber/86232aad43430e9169affdbe5808d65a to your computer and use it in GitHub Desktop.
Save rgottleber/86232aad43430e9169affdbe5808d65a 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.4.26+commit.4563c3fc.js&optimize=false&runs=200&gist=
pragma solidity ^0.4.0;
contract SimpleCoin {
mapping (address => uint256) public coinBalance;
// Allowance is used to limit the number of coins an address can
// use in transferFrom
mapping (address => mapping (address => uint256)) public allowance;
mapping (address => bool) public frozenAccount;
address public owner;
// Events are used to notify other clients / contracts about things that happen
// Define the transfer event.
event Transfer(address indexed from, address indexed to, uint256 value);
//Define freeze event for account
event FrozenAccount(address target, bool frozen);
// Modifiers are used to dry out checks for functions
modifier onlyOwner {
if (msg.sender != owner) revert();
_;
}
// Define the owner on contract creation
constructor(uint256 _initialSupply) public {
owner = msg.sender;
mint(owner, _initialSupply);
}
function transfer(address _to, uint256 _amount) public {
require(_to != 0x0);
require(coinBalance[msg.sender] >= _amount);
require((coinBalance[_to] += _amount) >= coinBalance[_to]);
coinBalance[msg.sender] -= _amount;
coinBalance[_to] += _amount;
// Publish Transfer event
emit Transfer(msg.sender, _to, _amount);
}
function authorize(address _authorizedAccount, uint256 _allowance) public returns (bool success){
allowance[msg.sender][_authorizedAccount] = _allowance;
return true;
}
// Check the balances and the allowance to make sure that the address can transferFrom
// the amount. Emit transfer and success
function transferFrom(address _from, address _to, uint256 _amount) public returns (bool success){
require(_to != 0x0);
require(coinBalance[_from] >= _amount);
require((coinBalance[_to] += _amount) >= coinBalance[_to]);
require(_amount <= allowance[_from][msg.sender]);
coinBalance[_from] -= _amount;
coinBalance[_to] += _amount;
allowance[_from][msg.sender] -= _amount;
// Publish transfer event
emit Transfer(_from, _to, _amount);
return true;
}
// Creating coins is limited to the owner and considered a transfer even though
// the coins are 'created'
function mint(address _recipient, uint256 _mintedAmount) onlyOwner public {
coinBalance[_recipient] += _mintedAmount;
emit Transfer(owner, _recipient, _mintedAmount);
}
// Modify the state of an account to 'frozen'
function freezeAccount(address target, bool freeze) onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenAccount(target, freeze);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment