武漢加油7星評(píng)價(jià)
2020-09-19 22:12:06
也許你經(jīng)??吹紼RC20和代幣一同出現(xiàn), ERC20是以太坊定義的一個(gè)代幣標(biāo)準(zhǔn)。要求我們?cè)趯?shí)現(xiàn)代幣的時(shí)候必須要遵守的協(xié)議,如指定代幣名稱、總量、實(shí)現(xiàn)代幣交易函數(shù)等,只有支持了協(xié)議才能被以太坊錢包支持。其接口如下:
contract
ERC20Interface { string public constant name = "Token Name"; string public
constant symbol = "SYM"; uint8 public constant decimals = 18; // 18 is the most
common number of decimal places function totalSupply() public constant returns
(uint); function balanceOf(address tokenOwner) public constant returns (uint
balance); function allowance(address tokenOwner, address spender) public
constant returns (uint remaining); function transfer(address to, uint tokens)
public returns (bool success); function approve(address spender, uint tokens)
public returns (bool success); function transferFrom(address from, address to,
uint tokens) public returns (bool success); event Transfer(address indexed from,
address indexed to, uint tokens); event Approval(address indexed tokenOwner,
address indexed spender, uint tokens);}
簡(jiǎn)單說(shuō)明一下:
name
: 代幣名稱symbol: 代幣符號(hào)decimals: 代幣小數(shù)點(diǎn)位數(shù),代幣的最小單位, 18表示我們可以擁有
.0000000000000000001單位個(gè)代幣。totalSupply() : 發(fā)行代幣總量。balanceOf():
查看對(duì)應(yīng)賬號(hào)的代幣余額。transfer(): 實(shí)現(xiàn)代幣交易,用于給用戶發(fā)送代幣(從我們的賬戶里)。transferFrom():
實(shí)現(xiàn)代幣用戶之間的交易。allowance(): 控制代幣的交易,如可交易賬號(hào)及資產(chǎn)。approve(): 允許用戶可花費(fèi)的代幣數(shù)。
代幣合約代碼:
pragma
solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address
_from, uint256 _value, address _token, bytes _extraData) public; }contract
TokenERC20 { string public name; string public symbol; uint8 public decimals =
18; // 18 是建議的默認(rèn)值 uint256 public totalSupply; mapping (address => uint256)
public balanceOf; // mapping (address => mapping (address => uint256))
public allowance; event Transfer(address indexed from, address indexed to,
uint256 value); event Burn(address indexed from, uint256 value); function
TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals); balanceOf[msg.sender] =
totalSupply; name = tokenName; symbol = tokenSymbol; } function
_transfer(address _from, address _to, uint _value) internal { require(_to !=
0x0); require(balanceOf[_from] >= _value); require(balanceOf[_to] + _value
> balanceOf[_to]); uint previousBalances = balanceOf[_from] + balanceOf[_to];
balanceOf[_from] -= _value; balanceOf[_to] += _value; Transfer(_from, _to,
_value); assert(balanceOf[_from] + balanceOf[_to] == previousBalances); }
function transfer(address _to, uint256 _value) public { _transfer(msg.sender,
_to, _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; } function
approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value; return true; } 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; } } function burn(uint256 _value) public returns (bool
success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -=
_value; totalSupply -= _value; Burn(msg.sender, _value); return true; } function
burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); require(_value <=
allowance[_from][msg.sender]); balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value; totalSupply -= _value; Burn(_from,
_value); return true; }}
在開(kāi)發(fā)測(cè)試智能合約時(shí),MetaMask和Remix Solidity IDE是兩個(gè)非常好用的工具,用來(lái)完成部署。
安裝和配置MetaMask請(qǐng)參考開(kāi)發(fā)、部署第一個(gè)去中心化應(yīng)用,選擇了以太坊的測(cè)試網(wǎng)絡(luò)Ropsten,如果你沒(méi)有余額請(qǐng)點(diǎn)擊購(gòu)買buy,進(jìn)入的網(wǎng)站可以送一些測(cè)試以太幣給你,配置好之后,界面應(yīng)該如下:
瀏覽器打開(kāi)Remix Solidity IDE,復(fù)制以上源碼粘貼上,在右側(cè)選項(xiàng)參考如圖的設(shè)置:注意Environment和Account和MetaMask保持一致,然后選擇合約TokenERC20,填入你想要的發(fā)行量,名稱及代號(hào),就可以創(chuàng)建合約了。這時(shí)MetaMask會(huì)彈出一個(gè)交易確認(rèn)框,點(diǎn)SUBMIT。待合約部署交易確認(rèn)之后,點(diǎn)擊交易詳情如下圖,復(fù)制合約地址(下圖中紅色框內(nèi))。
打開(kāi)Metamask界面,切換到TOKENS,點(diǎn)添加合約,出現(xiàn)如下對(duì)話框:
填入剛剛復(fù)制的地址,點(diǎn)ADD,這時(shí)你就可以看到你創(chuàng)建的代幣了,如圖:
你已經(jīng)完成了代幣的創(chuàng)建和部署(正式網(wǎng)絡(luò)和測(cè)試網(wǎng)絡(luò)部署方法一樣),可以在Etherscan查詢到我們剛剛部署的代幣,如文章開(kāi)頭的圖片,代幣發(fā)行量為10000,單位為SB。