Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?


More than 3 years have passed since last update.


 ERC20トークンはethereum上で利用できるトークンの標準規格。これに準拠しているトークンは Ethereumウォレット、および同じ基準を使用する他のクライアントまたはコントラクトと互換性がある。今回は標準的なトークンの機能を実装したTokenERC20コントラクトとトークンの中央管理者を設定するowendコントラクトを作成し、それらを継承したより多くの機能を持つトークンを実装する。


 トークンの中央管理者を設定する。modifier onlyOwnerによって関数の実行可能者をownerに限定することができる。

contract owned {
    address public owner;
    //コンストラクタ、  ownerを設定
    function owned() public {
        owner = msg.sender;
    modifier onlyOwner {
        require(msg.sender == owner);
    //  ownerの交代
    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;


contract TokenERC20 {
    // トークンの名前、シンボル、供給量
    string public name;
    string public symbol;
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

    function TokenERC20(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) public {
        totalSupply = initialSupply * 10 ** uint256(decimals);
        balanceOf[msg.sender] = totalSupply;
        name = tokenName;
        symbol = tokenSymbol;

    function _transfer(address _from, address _to, uint _value) internal {
       require(_to != 0x0);
        require(balanceOf[_from] >= _value);
        require(balanceOf[_to] + _value > balanceOf[_to]);
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        Transfer(_from, _to, _value);
        // 転送が成功したかチェック
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);

    function transfer(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);

    function transferFrom(address _from,adress _to,uint256 _value) public returns (bool success) {
      require(_value <= allowance[_from][msg.sender]);
      allowance[_from][msg.sender] -= _value;

    function approve(address _spender,uint256 _value) public returns(bool success) {
      allowance[msg.sender][_spender] = _value;
      return true;


contract MyAdvancedToken is owned, TokenERC20 {
    uint256 public sellPrice;
    uint256 public buyPrice;
    uint minBalanceForAccounts;
    mapping (address => bool) public frozenAccount;

    event FrozenFunds(address target, bool frozen);

    function setMinBalance(uint minimumBalanceInFinney) onlyOwner {
        minBalanceForAccounts = minimumBalanceInFinney * 1 finney;

    function MyAdvancedToken(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}

    function _transfer(address _from, address _to, uint _value) internal {
        require (_to != 0x0);
        require (balanceOf[_from] >= _value);
        require (balanceOf[_to] + _value > balanceOf[_to]);
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        Transfer(_from, _to, _value);

    function transfer(address _to, uint256 _value) {
      if(msg.sender.balance < minBalanceForAccounts){
        sell((minBalanceForAccounts - msg.sender.balance) / sellPrice);
      _transfer(msg.sender, _to, _value);

    function mintToken(address target, uint256 mintedAmount) onlyOwner public {
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        Transfer(0, this, mintedAmount);
        Transfer(this, target, mintedAmount);

    function freezeAccount(address target, bool freeze) onlyOwner public {
        frozenAccount[target] = freeze;
        FrozenFunds(target, freeze);

    function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
        sellPrice = newSellPrice;
        buyPrice = newBuyPrice;

    function buy() payable public {
        uint amount = msg.value / buyPrice;
        _transfer(this, msg.sender, amount);

    function sell(uint256 amount) public {
        require(this.balance >= amount * sellPrice);
        _transfer(msg.sender, this, amount);
        msg.sender.transfer(amount * sellPrice);

    function giveBlockReward() {
      balanceOf[block.coinbase] += 1;

    uint currentChallenge = 1;
    function rewardMathGeniuses(uint answerToCurrentReward, uint nextChallenge) {
      require(answerToCurrentReward**3 == currentChallenge);
      balanceOf[msg.sender] += 1;
      currentChallenge = nextChallenge;


Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away


No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
Help us understand the problem. What is going on with this article?