区块链学习笔记(二)

第一天主要是了解以太坊的基本操作,第二天是合约入门,第三天是Dapps入门,不过由于Dapps比较难,需要结合其他的编程语言,我们培训时是利用JavaScript以网页的形式展示,因此还需要配置一个服务器环境,比较繁琐,所以这部分的内容我就不写了,这是最后一篇

Solidity

首先要编写合约那么自然是需要一种编程语言,Solidity是一种基于以太坊的区块链语言,语法类似JavaScript,常用于智能合约的编写。Solidity自然也需要一个编辑器,这里我们使用一个在线IDE——Remix。首先选择Solidity环境,然后左边会多出许多按钮,我们待会儿,将会用到它们

先新建一个sol文件,名字叫什么都可以。

从头开始写一个合约对于我们来说还太过困难,所以我们找一个现成的模板,那么最常见的合约自然就是代币的合约了。

发行属于自己的代币

我发行的代币LZH的合约地址是0xe34c6bf29e43edc9d4c7bc06cb3ba79638185149,因为我已经公开了代码所以大家可以直接在etherscan里找到我的代码,不过由于需要翻墙,所以我直接把代码放出来(我也放在了GitHub上,也可以从上面下)。

/**
 *Submitted for verification at Etherscan.io on 2017-12-26
*/

pragma solidity ^0.4.18;
/**
 * This smart contract code is Copyright 2017 Bitmart. For more information see https://www.bitmart.com
 *
 * Licensed under the Apache License, version 2.0
 */

/**
 * @title SafeMath
 * @dev Math operations with safety checks that revert() on error
 */
library SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically revert()s when dividing by 0
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return c;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
}


/*
 * LZH                            //从这里开始把所有的LZH换成你的代币
 *
 * Abstract contract that create Bitmart Token based on ERC20.
 *
 */
contract LZH {                   //这里是第一个
    using SafeMath for uint256;
    string public name;
    string public symbol;
    uint8 public decimals;
    uint256 public totalSupply;
    address public owner;

    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
    mapping (address => uint256) public freezeOf;
    mapping (address => mapping (address => uint256)) public allowance;

    /* This generates a public event on the blockchain that will notify clients */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /* This notifies clients about the amount burnt */
    event Burn(address indexed from, uint256 value);

    /* This notifies clients about the amount frozen */
    event Freeze(address indexed from, uint256 value);

    /* This notifies clients about the amount unfrozen */
    event Unfreeze(address indexed from, uint256 value);

    /* This notifies the owner transfer */
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /* Initializes contract with initial supply tokens to the creator of the contract */       //下面这个函数里总共有三个
    function LZH( uint256 initialSupply, uint8 decimalUnits) public {
        balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
        totalSupply = initialSupply; // Update total supply
        name = "LZHToken";   // Set the name for display purposes
        symbol = "LZH";    // Set the symbol for display purposes
        decimals = decimalUnits;  // Amount of decimals for display purposes
        owner = msg.sender;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
      require(msg.sender == owner);
      _;
    }

    /**
     * @dev Allows the current owner to transfer control of the contract to a newOwner.
     * @param newOwner The address to transfer ownership to.
     */
    function transferOwnership(address newOwner) public onlyOwner {
      require(newOwner != address(0));
      OwnershipTransferred(owner, newOwner);
      owner = newOwner;
    }

    /* Send Coins */
    function transfer(address _to, uint256 _value) public {
        require(_to != 0x0);
        require(_value > 0);
        require(balanceOf[msg.sender] >= _value );// Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows

        balanceOf[msg.sender] = balanceOf[msg.sender].sub(_value); // Subtract from the sender
        balanceOf[_to] = balanceOf[_to].add(_value);  // Add the same to the recipient
        Transfer(msg.sender, _to, _value);   // Notify anyone listening that this transfer took place
    }

    /* Allow another contract to spend some tokens in your behalf */
    function approve(address _spender, uint256 _value) public returns (bool) {
        require(_value > 0);
        allowance[msg.sender][_spender] = _value;
        return true;
    }

    /* A contract attempts to get the coins */
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
        require(_to != 0x0);
        require(_value > 0);
        require(balanceOf[_from] >= _value );// Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        require(_value <= allowance[_from][msg.sender]); // Check allowance

        balanceOf[_from] = balanceOf[_from].sub(_value);   // Subtract from the sender
        balanceOf[_to] = balanceOf[_to].add(_value);  // Add the same to the recipient
        allowance[_from][msg.sender] = allowance[_from][msg.sender].sub(_value);
        Transfer(_from, _to, _value);
        return true;
    }

    function burn(uint256 _value) public onlyOwner returns (bool) {
        require(balanceOf[msg.sender] >= _value);// Check if the sender has enough
        require(_value > 0);

        balanceOf[msg.sender] = balanceOf[msg.sender].sub(_value);  // Subtract from the sender
        totalSupply = totalSupply.sub(_value); // Updates totalSupply
        Burn(msg.sender, _value);
        return true;
    }

    function freeze(uint256 _value) public returns (bool) {
        require(balanceOf[msg.sender] >= _value);// Check if the sender has enough
        require(_value > 0);

        balanceOf[msg.sender] = balanceOf[msg.sender].sub(_value); // Subtract from the sender
        freezeOf[msg.sender] = freezeOf[msg.sender].add(_value);  // Updates totalSupply
        Freeze(msg.sender, _value);
        return true;
    }

    function unfreeze(uint256 _value) public returns (bool) {
        require(freezeOf[msg.sender] >= _value); // Check if the sender has enough
        require(_value > 0);

        freezeOf[msg.sender] = freezeOf[msg.sender].sub(_value); // Subtract from the sender
        balanceOf[msg.sender] = balanceOf[msg.sender].add(_value);
        Unfreeze(msg.sender, _value);
        return true;
    }

    // transfer contract balance to owner
    function withdrawEther(uint256 amount) public onlyOwner {
        owner.transfer(amount);
    }

    // can accept ether
    function() payable public {
    }
}

首先我们先编译一下,看看有没有问题。

然后选择好你要发行的代币数量,你的代币就成功发行了。

代码公开

你可以选择公开你的合约代码,这样别人可以参考你的合约,也可以帮你寻找漏洞。在发行完后会返回一个地址,这是你创建合约这一交易的地址,你可以从中找到你的合约的地址(需要翻墙)。


你可以在这里看到合约的代码,如果已经公开了的话

公开代码需要证明你是合约创建人

最后把代码粘贴上去即可,不过有时候可能会莫名报错,可以多试几次。

再回到之前的页面,你可以看到Contract旁边多了个绿色的小圆点,说明你的代码已经公开了,所有人都可以在这看到。

函数的调用

回到编辑器那,点击左下角,我们可以看到合约中包含的函数,我们可以直接在这里通过可视化的界面调用它们。

到此为止,我们已经对智能合约有个大概的了解了,之前说了Dapps比较难通过这种方式将清楚,如果你想要更深入的了解智能合约,深入学习Solidity这门语言,或者你想要学一下Dapps,你可以玩一下这个CryptoZombies(点击访问),它会一步一步教你如何用Solidity编写合约,最后开发一个Dapps,总共有六关,一关需要一个多小时,全部玩完,你将会对智能合约和Dapps有个深入的认识