Skip to content

Instantly share code, notes, and snippets.

@jigar23
Created June 12, 2018 15:29
Show Gist options
  • Save jigar23/aabb9e1e4fc46cf41ce84e954027c6f6 to your computer and use it in GitHub Desktop.
Save jigar23/aabb9e1e4fc46cf41ce84e954027c6f6 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.23+commit.124ca40d.js&optimize=false&gist=
pragma solidity ^0.4.0;
interface ERC20 {
function transferFrom(address _from, address _to, uint _value) public returns (bool);
function approve(address _spender, uint _value) public returns (bool);
function allowance(address _owner, address _spender) public constant returns (uint);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
pragma solidity ^0.4.0;
interface ERC223 {
function transfer(address _to, uint _value, bytes _data) public returns (bool);
event Transfer(address indexed from, address indexed to, uint value, bytes indexed data);
}
pragma solidity ^0.4.0;
contract ERC223ReceivingContract {
function tokenFallback(address _from, uint _value, bytes _data) public;
}
pragma solidity ^0.4.0;
import "browser/ERC20.sol";
import "browser/ERC223.sol";
import "browser/ERC223ReceivingContract.sol";
import "browser/Token.sol";
//
// Disadvantage of ERC20:
// 1. There is no check to see if we are sending the token to contract
// If the token gets sent to a contract, we have no way of recovering it.
// With Ether, if ether gets sent to a contract the transaction fails and
// it rolls over the state
//
// ERC223
// Thats where ERC223 comes into picture, because we first check if the
// receiver addresss is a contract before sending
contract MyFirstToken is Token("MFT", "My First Token", 10, 1000), ERC20,
ERC223, ERC223ReceivingContract {
constructor() public
{
_balanceOf[msg.sender] = _totalSupply;
}
function balanceOf(address _addr) public view returns (uint)
{
return _balanceOf[_addr];
}
function transfer(address _to, uint _value) public returns (bool)
{
if (_balanceOf[msg.sender] >= _value && _value > 0
&& !isContract(_to)) {
_balanceOf[msg.sender] -= _value;
_balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
return false;
}
function transfer(address _to, uint _value, bytes _data) public returns (bool)
{
if (_value > 0 && _balanceOf[msg.sender] >= _value
&& isContract(_to)) {
_balanceOf[msg.sender] -= _value;
_balanceOf[_to] += _value;
ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
_contract.tokenFallback(msg.sender, _value, _data);
emit Transfer(msg.sender, _to, _value);
return true;
}
return false;
}
function transferFrom(address _from, address _to, uint _value) public returns (bool)
{
if (_value > 0 && _allowances[_from][_to] > _value &&
_balanceOf[_from] > _value) {
_balanceOf[_from] -= _value;
_allowances[_from][_to] -= _value;
_balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
return true;
}
return false;
}
function allowance(address _owner, address _spender) public constant returns (uint)
{
return _allowances[_owner][_spender];
}
function approve(address _spender, uint _value) public returns (bool)
{
_allowances[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
// Check to see if the codeSize is > 0
function isContract(address _addr) public view returns (bool)
{
// assembly language
uint codeSize;
assembly {
codeSize := extcodesize(_addr)
}
return codeSize > 0;
}
}
pragma solidity ^0.4.0;
// Minimum viable Token
contract Token {
string internal _symbol;
string internal _name;
uint8 internal _decimal;
uint internal _totalSupply = 1000;
mapping (address => uint) internal _balanceOf;
mapping (address => mapping (address => uint)) internal _allowances;
constructor(string symbol, string name, uint8 decimal, uint totalSupply) public
{
_symbol = symbol;
_name = name;
_decimal = decimal;
_totalSupply = totalSupply;
}
function name() public view returns (string)
{
return _name;
}
function symbol() public view returns (string)
{
return _symbol;
}
function decimal() public view returns (uint8)
{
return _decimal;
}
function totalSupply() public view returns (uint)
{
return _totalSupply;
}
function balanceOf(address _addr) public view returns (uint);
function transfer(address _to, uint _value) public returns (bool);
event Transfer(address indexed_from, address indexed _to, uint _value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment