智能合约

in #cn-reader6 years ago
contract BasicToken is ERC20Basic {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

  uint256 totalSupply_;

  /**
  * @ token总量
  */
  function totalSupply() public view returns (uint256) {
    return totalSupply_;
  }

  /**
  * @dev 转账
  * @param _to 转给谁
  * @param _value 转多少.
  */
  function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));//不能自己转自己
    require(_value <= balances[msg.sender]);//转账金额小于等于账户余额

    balances[msg.sender] = balances[msg.sender].sub(_value);//转出账户减去金额
    balances[_to] = balances[_to].add(_value);//收入账户添加金额
    emit Transfer(msg.sender, _to, _value);//发送时间通知
    return true;
  }

  /**
  * @查询账户的余额
  * @param _owner  账户地址
  * @return An uint256 返回余额
  */
  function balanceOf(address _owner) public view returns (uint256) {
    return balances[_owner];
  }

}

通过这个源码可以看到、基本的token实现、主要是实现了,查询余额、转账、转账事件、token总量等方法。
需要注意的地方是uint256进行运算的时候,防止被黑客攻击,一定用safeMath来处理计算。
对于改变区块链状态的方法,一定要多重校验。
通过require方法来实现。

参考意见:

  • 只要涉及到计算,一定要用safeMath
  • 代码一定要反复测试!
  • 代码一定要有经验的同事review!
  • 必要时,要请专门做代码审计的公司来 测试代码
Sort:  

Congratulations @zhongdewang! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @zhongdewang! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Vote for @Steemitboard as a witness to get one more award and increased upvotes!