contract ERC20 {
//**********9個函數*******
//1.代幣的名字,如:"黑馬幣"
function name() constant public returns (string name);
//2.代幣的簡稱,例如:HMB
function symbol() public constant returns (string symbol);
//3.代幣的最小分割量 token使用的小數點後幾位。比如如果設置爲3,就是支持0.001表示
function decimals() public constant returns (uint8 decimals);
//4.token的總量
function totalSupply() public constant returns (uint totalSupply);
//5.餘額 返回某個地址(賬戶)的賬戶餘額
function balanceOf(address _owner) public constant returns (uint balance);
/*6.轉賬 交易代幣 從消息發送者賬戶中往_to賬戶轉數量爲_value的token,
從代幣合約的調用者地址上轉移 _value的數量token到的地址 _to
【注意:並且必須觸發Transfer事件】*/
function transfer(address _to, uint _value) public returns (bool success);
/*7.兩個地址轉賬
從賬戶_from中往賬戶_to轉數量爲_value的token,與approve方法配合使用
從地址 _from發送數量爲 _value的token到地址 _to
【注意:並且必須觸發Transfer事件】
transferFrom方法用於允許合約代理某人轉移token。條件是from賬戶必須經過了approve。*/
function transferFrom(address _from, address _to, uint _value) public returns (bool success);
//8.批准_spender能從合約調用賬戶中轉出數量爲_value的token
function approve(address _spender, uint _value) public returns (bool success);
//9.獲取_spender可以從賬戶_owner中轉出token的剩餘數量
function allowance(address _owner, address _spender) public constant returns (uint remaining);
//**********2個事件*******
//1.發生轉賬時必須要觸發的事件,transfer 和 transferFrom 成功執行時必須觸發的事件
event Transfer(address indexed _from, address indexed _to, uint _value);
//2.當函數 approve(address _spender, uint256 _value)成功執行時必須觸發的事件
event Approval(address indexed _owner, address indexed _spender, uint _value);
// 此合約實現不記得是哪看到後複製保存的......和官網案例類似
pragma solidity ^0.4.25;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }
contract TokenERC20 {
string public name; // ERC20標準
string public symbol; // ERC20標準
uint8 public decimals = 2; // ERC20標準,decimals 可以有的小數點個數,最小的代幣單位。18 是建議的默認值
uint256 public totalSupply; // ERC20標準 總供應量
// 用mapping保存每個地址對應的餘額 ERC20標準
mapping (address => uint256) public balanceOf;
// 存儲對賬號的控制 ERC20標準
mapping (address => mapping (address => uint256)) public allowance;
// 事件,用來通知客戶端交易發生 ERC20標準
event Transfer(address indexed from, address indexed to, uint256 value);
// 事件,用來通知客戶端代幣被消費 ERC20標準
event Burn(address indexed from, uint256 value);
/**
* 初始化構造
*/
function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // 供應的份額,份額跟最小的代幣單位有關,份額 = 幣數 * 10 ** decimals。
balanceOf[msg.sender] = totalSupply; // 創建者擁有所有的代幣
name = tokenName; // 代幣名稱
symbol = tokenSymbol; // 代幣符號
}
/**
* 代幣交易轉移的內部實現
*/
function _transfer(address _from, address _to, uint _value) internal {
// 確保目標地址不爲0x0,因爲0x0地址代表銷燬
require(_to != 0x0);
// 檢查發送者餘額
require(balanceOf[_from] >= _value);
// 確保轉移爲正數個
require(balanceOf[_to] + _value > balanceOf[_to]);
// 以下用來檢查交易,
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
Transfer(_from, _to, _value);
// 用assert來檢查代碼邏輯。
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* 代幣交易轉移
* 從自己(創建交易者)賬號發送`_value`個代幣到 `_to`賬號
* ERC20標準
* @param _to 接收者地址
* @param _value 轉移數額
*/
function transfer(address _to, uint256 _value) public {
_transfer(msg.sender, _to, _value);
}
/**
* 賬號之間代幣交易轉移
* ERC20標準
* @param _from 發送者地址
* @param _to 接收者地址
* @param _value 轉移數額
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/**
* 設置某個地址(合約)可以創建交易者名義花費的代幣數。
*
* 允許發送者`_spender` 花費不多於 `_value` 個代幣
* ERC20標準
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/**
* 設置允許一個地址(合約)以我(創建交易者)的名義可最多花費的代幣數。
*-非ERC20標準
* @param _spender 被授權的地址(合約)
* @param _value 最大可花費代幣數
* @param _extraData 發送給合約的附加數據
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
// 通知合約
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/**
* 銷燬我(創建交易者)賬戶中指定個代幣
*-非ERC20標準
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
Burn(msg.sender, _value);
return true;
}
/**
* 銷燬用戶賬戶中指定個代幣
*-非ERC20標準
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender
* @param _value the amount of money to burn
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
require(_value <= allowance[_from][msg.sender]); // Check allowance
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
totalSupply -= _value; // Update totalSupply
Burn(_from, _value);
return true;
}
}