solidity 版本
pragma solidity ^0.5.0;
.mul 乘法
.div 除法
.sub 减法
.add 加法
library 库
library SafeMath {
function mul( uint256 a, uint256 b) internal pure returns ( uint256 c) {
if ( a == 0) {
return 0;
}
c = a * b;
assert( c / a == b) ;
return c;
}
}
contract BasicToken{
//使用之前的工具类 只要是uint256类型的都可以用这个库里的方法
using SafeMath for uint256;
//账户及余额的mapping 其实就像一个map key是地址 value是余额
mapping( address = > uint256) balances;
function transfer(( address _to, uint256 _value) public returns ( bool success) {
balances[ msg.sender] = balances[ msg.sender] .sub( _value) ;
}
}
//https://eips.ethereum.org/EIPS/eip-20
// erc20协议提供的基本核心函数
contract ERC20Basic {
//查询合约上代币总数
function totalSupply( ) public view returns ( uint256) ;
//查询指定地址代币余额
function balanceOf( address who) public view returns ( uint256) ;
//代币交易
function transfer( address to, uint256 value) public returns ( bool) ;
event Transfer( address indexed from, address indexed to, uint256 value) ;
}
//is 继承 继承了上面ERC20Basic合约
contract BasicToken is ERC20Basic {
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 ;
}
function balanceOf( address _owner) public view returns ( uint256) {
return balances[ _owner] ;
}
}