Skip to content

Instantly share code, notes, and snippets.

@higarin
Last active July 7, 2018 02:20
Show Gist options
  • Save higarin/e626820ea3e5e6fc024846d12bf00799 to your computer and use it in GitHub Desktop.
Save higarin/e626820ea3e5e6fc024846d12bf00799 to your computer and use it in GitHub Desktop.
ERC20
pragma solidity ^0.4.24;
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function transfer(address to, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
pragma solidity ^0.4.24;
contract MyToken {
// トークン名
string public name;
// トークンシンボル
string public symbol;
//public 修飾子を指定すると getter が生成される
// => function totalSupply() returns (uint)
uint public totalSupply;
constructor(string _name, string _symbol, uint tokens) public {
name = _name;
symbol = _symbol;
balanceOf[msg.sender] += tokens;
totalSupply += tokens;
}
// ハッシュテーブル
// => function balanceOf(address owner) returns (uint)
mapping(address => uint) public balanceOf;
mapping(address => mapping(address =>uint)) public allowance;
function transfer(address to, uint tokens) public returns (bool success) {
// msg.sender: 関数の呼び出し元のアドレス
return transferFrom(msg.sender, to, tokens);
}
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
// 送信元が必要な量のトークンを所持しているか確認する
require(balanceOf[msg.sender] >= tokens);
// 呼び出し元と送信元が同じでない時、承認されているか確認する
if (msg.sender != from) {
require(allowance[from][msg.sender] >= tokens);
allowance[from][msg.sender] -= tokens;
}
// トークンを移動する
balanceOf[from] -= tokens;
balanceOf[to] += tokens;
// Transfer イベントを発火する
emit Transfer(from, to, tokens);
return true;
}
function approve(address spender, uint tokens) public returns (bool success) {
allowance[msg.sender][spender] += tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
@higarin
Copy link
Author

higarin commented Jul 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment