Skip to content

Instantly share code, notes, and snippets.

@carver
Last active October 31, 2018 12:46
Show Gist options
  • Save carver/0102906286e8ce3b8c4d1267c7b83897 to your computer and use it in GitHub Desktop.
Save carver/0102906286e8ce3b8c4d1267c7b83897 to your computer and use it in GitHub Desktop.
Unsafe ERC20 Example for Web3.py Workshop
pragma solidity ^0.4.25;
// This contract is an example, not to be used in production
// One example issue is that there are potential overflow/underflow bugs.
contract UnsafeERC20 {
mapping (address => uint256) private _balances;
uint256 private _totalSupply;
event Transfer(
address indexed from,
address indexed to,
uint256 value
);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
constructor(uint256 startBalance) public {
_mint(msg.sender, startBalance);
}
/**
* @dev Total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev Gets the balance of the specified address.
* @param owner The address to query the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
/**
* @dev Transfer token for a specified address
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
/**
* @dev Transfer token for a specified addresses
* @param from The address to transfer from.
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function _transfer(address from, address to, uint256 value) internal {
require(value <= _balances[from]);
require(to != address(0));
_balances[from] = _balances[from] - value;
_balances[to] = _balances[to] + value;
emit Transfer(from, to, value);
}
/**
* @dev Internal function that mints an amount of the token and assigns it to
* an account. This encapsulates the modification of balances such that the
* proper events are emitted.
* @param account The account that will receive the created tokens.
* @param value The amount that will be created.
*/
function _mint(address account, uint256 value) internal {
require(account != 0);
_totalSupply = _totalSupply + value;
_balances[account] = _balances[account] + value;
emit Transfer(address(0), account, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment