注:通常你們都廣泛喜歡把 Token 叫成 代幣,可是這樣講很不許確,若是非要叫成中文,我更認同 通證 這種理解。但爲了保持原汁原味,沒必要非得翻譯過來叫,因此本文統一保持英文的形式。編程
通常 Token 製做的門檻其實沒有多高,因此你們徹底都有能力能夠去製做出本身命名的 Token。瀏覽器
不過你也別期望啥也不學就能作出一些擁有額外邏輯的 Token,好比衆籌合約等,因此想要作的不同凡響,那麼就須要花些成本。bash
下面就直奔主題,咱們不須要了解如何編寫智能合約,由於以太坊提供的 ERC20 協議已經足夠傻瓜式了,作一個簡單的 Token 官方就有一個標準的智能合約代碼做爲示例,那麼咱們只須要知道製做的流程就好了。網絡
囉嗦一句,其實學習其餘的技術知識也是如此,咱們不要急於追求很內在的東西,咱們須要 正反饋 式學習法,先作出一些簡單的 Demo,逐步在創建自信心和激發熱情的同時不斷深刻學習。app
咱們首先須要安裝一個瀏覽器的插件 MetaMask,該插件做者很是 nice,主流的三大瀏覽器(Google、FireFox、Opera)上均可以安裝這款實用的插件。安裝這款插件的過程就不贅述了,如今假定咱們已經裝好了,點開該插件,通過兩次 Accept 贊成條款的操做後,進入建立帳戶的頁面。 編程語言
在建立帳戶頁面上輸入本身的大於八位字符的密碼後,進入 助記詞 頁面,這些助記詞是惟一能幫助你保存本身帳戶合約的密鑰,妥善保管。 工具
不要慌,不是讓你真的花錢去買,只須要在 MetaMask 提供的網站上點幾下按鈕就能獲取到以太幣(畢竟這些測試網絡上的以太幣沒有實際價值)。學習
保存完助記詞後咱們就會進入到帳戶主頁面, 區塊鏈
OK,咱們先暫時放一放 MetaMask,接下來讓咱們去玩玩 Remix!測試
Remix 是一個基於瀏覽器的 Solidity IDE,基本的智能合約代碼的開發調試在這上面進行是至關舒服的。Remix - Solidity IDE
須要熟悉這個工具嗎?固然,並且越熟悉越好,但不是如今,別忘了咱們如今的目標是製做本身的 Token。
以太坊官方網站上有一份標準的 Token 合約供學習者參考,並且也有很詳細的教程教你們如何去部署本身的 Token。Create a cryptocurrency contract in Ethereum
本文與官方不一樣的是咱們使用 輕錢包 MetaMask 和在線開發環境 Remix 進行開發調試部署,官方的以太坊錢包對於初學者來講有個挺苦惱的地方,同步主網絡或者測試網絡的全節點數據很慢,並且佔用磁盤空間較大。初次體驗儘可能仍是選擇輕鬆的方式,不要給本身留下壞心情 :)
咱們將官方的 Token 合約代碼拷貝到 Remix 的代碼欄中,合約代碼這裏也貼一下:
pragma solidity ^0.4.16;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constructor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
function TokenERC20(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value) internal {
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != 0x0);
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
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);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public {
_transfer(msg.sender, _to, _value);
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` on behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
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;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value` tokens on your behalf
*
* @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;
}
/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
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;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
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;
}
/**
* Destroy tokens from other account
*
* 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; } } 複製代碼
接下來咱們先選擇好咱們的編譯器的版本,對應合約代碼首行的版本號 pragma solidity ^0.4.16
點擊箭頭1的標籤頁,進入設置界面,而後點擊箭頭2選擇相應的編譯器版本,這裏即選擇 0.4.16。
而後點擊編譯標籤(Compile),會看到已經編譯成功
而後點擊運行標籤(Run),進行部署操做
這裏惟一能夠改的是 Gas Limit 和 Gas Price,通常來說這兩個數據會自動根據當前合約的複雜度和市場平均水準生成。因此通常不須要去改這兩個值,若是是囊中羞澀,改低一點也是能夠的,可是可能會出現 Gas 被消耗完的狀況(Gas 一旦被消耗完而合約仍未部署到網絡上,那麼全部操做都會進行回滾,除了你支付的礦工費)。
點擊發布按鈕(SUBMIT),靜靜等待發布成功吧!
此時打開 MetaMask 頁面
上圖爲發佈中狀態,下圖爲發佈結束的狀態,此時會發現咱們的帳戶餘額減小了一些,由於被用於支付發佈合約的礦工費了。
到這裏還不清楚是否成功發佈了,點擊交易單進入交易詳情頁面,當你看到箭頭1處的 Success 時就表示咱們的 Token 發佈成功啦!
點擊箭頭2處的合約地址連接,進入合約詳情頁
點擊箭頭處的 Token 地址連接,進入 Token 詳情頁
至此咱們的 Token 就作完啦!
咱們以 MetaMask 爲例演示一下,其餘錢包好比 ImToken 一樣的只須要將 Token 地址複製過去就能看到了。
先切換到 Token 標籤頁
點擊添加 Token,輸入剛纔的 Token 合約地址
點擊添加按鈕,就能在 MetaMask 上看到咱們本身的 Token 了。
在製做 Token 的整個過程當中,並無難點,只是大多數人不清楚流程,當本身實操一遍後立馬就能上手。後面咱們會去玩一些複雜一點的智能合約,但一樣的,須要一些基本的 Solidity 編程語言的知識以及區塊鏈相關的知識。Solidity 這門以太坊的標準編程語言以後的文章也會詳細講解,你們盡請期待~
歡迎關注公衆號:『比特扣』,與我一塊兒探索區塊鏈的世界。