以太坊发token教程
以太坊發token教程
如果需要幣的話,請留下聯系方式!!!
在發Token前,你先的確定一下幾點:
我的選擇是:
小數位是18位,表示MFC這個Token最小可以到 .0000000000000000001。
編寫 MFC的智能合約:
Token的合約代碼我們參考Token-Factory的代碼。
pragma solidity ^0.4.4;contract Token {/// @return 返回token的發行量function totalSupply() constant returns (uint256 supply) {}/// @param _owner 查詢以太坊地址token余額/// @return The balance 返回余額function balanceOf(address _owner) constant returns (uint256 balance) {}/// @notice msg.sender(交易發送者)發送 _value(一定數量)的 token 到 _to(接受者) /// @param _to 接收者的地址/// @param _value 發送token的數量/// @return 是否成功function transfer(address _to, uint256 _value) returns (bool success) {}/// @notice 發送者 發送 _value(一定數量)的 token 到 _to(接受者) /// @param _from 發送者的地址/// @param _to 接收者的地址/// @param _value 發送的數量/// @return 是否成功function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}/// @notice 發行方 批準 一個地址發送一定數量的token/// @param _spender 需要發送token的地址/// @param _value 發送token的數量/// @return 是否成功function approve(address _spender, uint256 _value) returns (bool success) {}/// @param _owner 擁有token的地址/// @param _spender 可以發送token的地址/// @return 還允許發送的token的數量function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}/// 發送Token事件event Transfer(address indexed _from, address indexed _to, uint256 _value);/// 批準事件event Approval(address indexed _owner, address indexed _spender, uint256 _value); } /* This implements ONLY the standard functions and NOTHING else. For a token like you would want to deploy in something like Mist, see HumanStandardToken.sol.If you deploy this, you won't have anything useful.Implements ERC 20 Token standard: https://github.com/ethereum/EIPs/issues/20實現ERC20標準 .*/pragma solidity ^0.4.4;import "./Token.sol";contract StandardToken is Token {function transfer(address _to, uint256 _value) returns (bool success) {//默認token發行量不能超過(2^256 - 1)//如果你不設置發行量,并且隨著時間的發型更多的token,需要確保沒有超過最大值,使用下面的 if 語句//if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {if (balances[msg.sender] >= _value && _value > 0) {balances[msg.sender] -= _value;balances[_to] += _value;Transfer(msg.sender, _to, _value);return true;} else { return false; }}function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {//向上面的方法一樣,如果你想確保發行量不超過最大值//if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {balances[_to] += _value;balances[_from] -= _value;allowed[_from][msg.sender] -= _value;Transfer(_from, _to, _value);return true;} else { return false; }}function balanceOf(address _owner) constant returns (uint256 balance) {return balances[_owner];}function approve(address _spender, uint256 _value) returns (bool success) {allowed[msg.sender][_spender] = _value;Approval(msg.sender, _spender, _value);return true;}function allowance(address _owner, address _spender) constant returns (uint256 remaining) {return allowed[_owner][_spender];}mapping (address => uint256) balances;mapping (address => mapping (address => uint256)) allowed;uint256 public totalSupply; } /* This Token Contract implements the standard token functionality (https://github.com/ethereum/EIPs/issues/20) as well as the following OPTIONAL extras intended for use by humans.In other words. This is intended for deployment in something like a Token Factory or Mist wallet, and then used by humans. Imagine coins, currencies, shares, voting weight, etc. Machine-based, rapid creation of many tokens would not necessarily need these extra features or will be minted in other manners.1) Initial Finite Supply (upon creation one specifies how much is minted). 2) In the absence of a token registry: Optional Decimal, Symbol & Name. 3) Optional approveAndCall() functionality to notify a contract if an approval() has occurred..*/ pragma solidity ^0.4.4;import "./StandardToken.sol";contract MyFreeCoin is StandardToken {function () {//if ether is sent to this address, send it back.throw;}/* Public variables of the token *//*NOTE: The following variables are OPTIONAL vanities. One does not have to include them.They allow one to customise the token contract & in no way influences the core functionality.Some wallets/interfaces might not even bother to look at this information.*/string public name; //token名稱: MyFreeCoin uint8 public decimals; //小數位string public symbol; //標識string public version = 'H0.1'; //版本號function MyFreeCoin(uint256 _initialAmount,string _tokenName,uint8 _decimalUnits,string _tokenSymbol) {balances[msg.sender] = _initialAmount; // 合約發布者的余額是發行數量totalSupply = _initialAmount; // 發行量name = _tokenName; // token名稱decimals = _decimalUnits; // token小數位symbol = _tokenSymbol; // token標識}/* 批準然后調用接收合約 */function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {allowed[msg.sender][_spender] = _value;Approval(msg.sender, _spender, _value);//調用你想要通知合約的 receiveApprovalcall 方法 ,這個方法是可以不需要包含在這個合約里的。//receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData)//假設這么做是可以成功,不然應該調用vanilla approve。if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { throw; }return true;} }如果想要發行自己的token,只需要把 MyFreeCoin出現的地方替換為你的token名稱。
需要注意的一點是,你發行的數量需要相對token小數點來設置。例如如果token的小數點是0,而你要發行1000個token,那么發行數量的值1000。但是如果token的小數點是18位,你要發行1000個token,那么發行數量的值是1000000000000000000000(1000后面加上18個0)。
balances[msg.sender] = _initialAmount;
這行代碼,我們把合約的發布者的余額設置為發行量的數量。
在測試網絡上發行我們的token:
安裝MetaMask之后,登陸Metamask, 左上角選擇Ropsten。如下圖:
1.jpg
這個賬號將會是我們的智能合約的所有者,也就是說token發行數量都是存入到這個賬號。
打開Solidity Remix Compiler ,remix 是一個在線編譯器可以幫我們把智能合約直接發布到以太坊上。
把上面三個文件代碼復制到remix編輯器中。可以先刪除remix中默認ballot.sol 文件,在新建 Token.sol , StandardToken.sol, MyFreeCoin.sol 三個文件, 相應的把代碼復制到文件中,如下圖:
2.jpg
點擊 start to compile 編譯代碼文件。
給我們的測試賬號申請點 eth來測試,如下圖點擊 buy按鈕,再點擊ropsten test faucet。
4.jpg
會打開 faucet metamask 網站,點擊
request 1 eth from faucet。成功后會生成 交易記錄。
6.jpg
可以查看到我們的測試賬戶上已經有了eth可以用了。
7.jpg
選中remix中的run 菜單,下拉框中選擇MyFreeCoin, 在create按鈕的左邊輸入框中輸入 "10000000000000000000000","MyFreeCoin",18,"MFC", 如下圖
點擊create 按鈕,需要注意的是發行量需要包含在 "" 中。 metamask會彈出確認框。如下圖:
9.jpg
確定后,會進入掛起狀態,等待曠工打包。
等一段時間后,交易完成,會顯示MyFreeCoin 合約。
10.jpg
點擊MyFreeCoin 的復制按鈕,復制合約地址在 ropsten etherscan中查詢,可以查詢到我們的合約情況,如下圖:
驗證我們發布的token。在metamask的token中點擊 add token 按鈕。如下圖:
12.jpg
在add token 的地址填入我們剛才復制的合約地址,如下圖:
13.jpg
可以在token中看到我們新創建的token。如下圖:
14.jpg
認證我們的合約代碼。 在剛才ropsten ethscan 的合約地址頁面中,點擊Contract code, 如下圖:
15.jpg
點擊Verify and Publish, 會進入如下頁面:
16.jpg
去掉原來代碼文件中的 import語句。最后提交。成功后,會顯示下面的頁面表示驗證成功:
17.jpg
最后讓我們在不同地址之間流通這個token。我們第一個賬戶已經有1000的MFC了。
先讓我們在創建一個新的賬戶,如下圖:
18.jpg
可以看到我們新創建的賬戶 MFC的值是0.
19.jpg
在切回我們的第一個賬戶,在transfer 中填入第二賬戶的地址和轉入的數量("0xe4da4CBC744708A6656BeD252f49DF5exxxxxxC97","1000000000000000000")。如下圖:
20.jpg
點擊transfer 會彈出彈框讓你確定,點擊sumbit,等待區塊打包。切換到第二個賬戶,查看MFC余額。可以看到已經到轉過來的1MFC了。
21.jpg
最終我們的發token的流程已經結束了。但是這還只是第一步,后面我們還需要程序化的執行token的充幣,提幣操作。也是一個應用若想引入token進來必須要有的功能。后面我會繼續研究下去,實現用程序來操作token的轉讓。
總結
以上是生活随笔為你收集整理的以太坊发token教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DAPP(分布式应用),区块链新物种,程
- 下一篇: CSDN蒋涛大数据表明:DCO - 区块