____  ___    _  _     _   _ _____     _______
 / ___|/ _ \  | || |   | | | |_ _\ \   / / ____|
| |  _| | | | | || |_  | |_| || | \ \ / /|  _|
| |_| | |_| | |__   _| |  _  || |  \ V / | |___
 \____|\___/     |_|   |_| |_|___|  \_/  |_____|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

智能合约

BY: @zhongdewang | CREATED: June 24, 2018, 3:10 p.m. | VOTES: 1 | PAYOUT: $0.00 | [ VOTE ]
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方法来实现。

参考意见:

TAGS: [ #cn-reader ] [ #cn-cryptocurrency ] [ #eth ] [ #contract ] [ #safemath ]

Replies

@steemitboard | March 13, 2019, 12:47 p.m. | Votes: 0 | [ VOTE ]

Congratulations @zhongdewang! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@zhongdewang/birthday1.pngHappy 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!
@steemitboard | March 13, 2020, 2:10 a.m. | Votes: 0 | [ VOTE ]

Congratulations @zhongdewang! You received a personal award!

https://steemitimages.com/70x70/http://steemitboard.com/@zhongdewang/birthday2.pngHappy 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!
[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>