Skip to content

Instantly share code, notes, and snippets.

@ObjSal
Created December 1, 2020 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ObjSal/442c802dc8e62e1c07b41f453c9de63f to your computer and use it in GitHub Desktop.
Save ObjSal/442c802dc8e62e1c07b41f453c9de63f to your computer and use it in GitHub Desktop.
Implementation of AguilaCoin (a Chore Coin) an ERC-20 Token
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.8.0;
contract AguilaCoin {
uint256 private _totalSupply;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowed;
// Notifications
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor() public {
// Let's see how it goes with $1,000.
// 1000(dlls) * 100(pennies)
// the second operation is a pow, example: pow(10, decimals())
uint256 dlls = 1000 * (10 ** (uint256(decimals())));
_mint(msg.sender, dlls);
}
// --------------------------------------------------------------------------------
// EIP-20: ERC-20 Token Standard Methods
// --------------------------------------------------------------------------------
// OPTIONAL - This method can be used to improve
// usability, but interfaces and other contracts
// MUST NOT expect these values to be present.
function name() public pure returns (string memory) {
return 'AguilaCoin';
}
// OPTIONAL - This method can be used to improve
// usability, but interfaces and other contracts
// MUST NOT expect these values to be present.
function symbol() public pure returns (string memory) {
return 'AC';
}
// Returns the number of decimals the token uses
// - e.g. 8, means to divide the token amount by
// 100000000 to get its user representation.
// OPTIONAL - This method can be used to improve
// usability, but interfaces and other contracts
// MUST NOT expect these values to be present.
function decimals() public pure returns (uint8) {
// just like the dollar
return 2;
}
// Returns the total token supply.
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
// Returns the account balance of another account with address owner.
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
// Transfers value amount of tokens to address 'to', and MUST fire the
// Transfer event. The function SHOULD throw if the message caller’s
// account balance does not have enough tokens to spend.
// Note Transfers of 0 values MUST be treated as normal transfers and fire
// the Transfer event.
function transfer(address to, uint256 value) public returns (bool) {
require(value <= _balances[msg.sender]);
require(to != address(0));
_balances[msg.sender] -= value;
_balances[to] += value;
emit Transfer(msg.sender, to, value);
return true;
}
// Transfers 'value' amount of tokens from address 'from' to address 'to',
// and MUST fire the Transfer event.
// The transferFrom method is used for a withdraw workflow, allowing
// contracts to transfer tokens on your behalf. This can be used for
// example to allow a contract to transfer tokens on your behalf and/or
// to charge fees in sub-currencies. The function SHOULD throw unless
// the 'from' account has deliberately authorized the sender of the message
// via some mechanism.
// Note Transfers of 0 values MUST be treated as normal transfers and
// fire the Transfer event.
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require(value <= _balances[from]);
require(value <= _allowed[from][msg.sender]);
require(to != address(0));
_balances[from] -= value;
_allowed[from][msg.sender] -= value;
_balances[to] += value;
emit Transfer(from, to, value);
return true;
}
// Allows 'spender' to withdraw from your account multiple times, up to the
// 'value' amount. If this function is called again it overwrites the current
// allowance with 'value'.
// NOTE: To prevent attack vectors like the one described here (https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit)
// and discussed here (https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729),
// clients SHOULD make sure to create user interfaces in such a way that
// they set the allowance first to 0 before setting it to another value for the
// same spender. THOUGH The contract itself shouldn’t enforce it, to allow backwards
// compatibility with contracts deployed before
function approve(address spender, uint256 value) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
// Returns the amount which 'spender' is still allowed to withdraw from 'owner'.
function allowance(address owner, address spender) public view returns (uint256) {
return _allowed[owner][spender];
}
// --------------------------------------------------------------------------------
// Internal helper functions
// --------------------------------------------------------------------------------
function _mint(address account, uint256 amount) internal {
require(account != address(0));
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment