零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币
前言
目前區(qū)塊鏈?zhǔn)腔ヂ?lián)網(wǎng)中最最火的風(fēng)口,沒(méi)有之一。我周圍的很多朋友也加入了“炒幣”行列,但很不幸,幾乎都被“割韭菜”了。而經(jīng)過(guò)我的幾天研究,發(fā)現(xiàn),如果自己要發(fā)行一種空氣幣,簡(jiǎn)直太簡(jiǎn)單了。只需要下面幾個(gè)步驟:
1.使用MetaMask
2.找Solidity代碼模板
3.部署智能合約
4.空氣幣轉(zhuǎn)賬測(cè)試
一、MetaMask
在Chrome瀏覽器的網(wǎng)上應(yīng)用店搜索MetaMask,如下圖所示,如果搜到小狐貍logo的插件就對(duì)了,這就是以太坊瀏覽器(如果有無(wú)法打開(kāi)Chrome網(wǎng)上應(yīng)用店的朋友,就去搜索怎樣科學(xué)上網(wǎng)的教程)。
把它添加到你的Chrome中。
打開(kāi)瀏覽器的右上角的圖標(biāo)快捷菜單中,打開(kāi)這個(gè)插件。如下圖,選擇Ropsten測(cè)試網(wǎng)絡(luò),并輸入密碼登錄。第一次登陸需要設(shè)置密碼。
默認(rèn)會(huì)幫你創(chuàng)建一個(gè)賬號(hào),如果需要再創(chuàng)建賬號(hào),如下圖所示,點(diǎn)擊Crreate Account。
默認(rèn)賬號(hào)里是沒(méi)有以太幣的,如果選擇的是主網(wǎng)(Main Ethereum Network),則需要從別的賬號(hào)里轉(zhuǎn)一些以太幣過(guò)來(lái)。而這里,我用的是測(cè)試網(wǎng)絡(luò),如下圖所示,點(diǎn)擊“BUY”按鈕,去免費(fèi)零一些以太幣來(lái)。
再點(diǎn)擊“ROPSTEN TEST FAUCET”
如下圖使用,打開(kāi)了一個(gè)網(wǎng)頁(yè)
狂點(diǎn)“request 1 ether from faucet”按鈕,每次點(diǎn)擊,就會(huì)獲得免費(fèi)的1個(gè)以太幣。大概等5分鐘左右時(shí)間,測(cè)試用的幣就會(huì)到帳了。再檢查你的賬戶余額,就不是零了。
二、找代碼模板
如下圖所示,打開(kāi)火幣Pro網(wǎng)站:https://www.huobipro.com/zh-cn/btc_usdt/exchange/
在其創(chuàng)新區(qū)里,幾乎有90%的幣都是基于ERC20智能合約發(fā)行的空氣幣。這里插一句題外話,如果有炒幣的朋友,就要小心這些空氣幣。
我們拿前段時(shí)間炒的最火的幣——EDU為例,來(lái)講一下怎么發(fā)幣。
如下圖所示,我們找到區(qū)塊查詢的網(wǎng)址,這個(gè)網(wǎng)址就是以太坊ERC20這個(gè)智能合約發(fā)型Token的交易查詢地址。
上面的幣種類簡(jiǎn)介和白皮書(shū)不要看,都是忽悠人的。大家記號(hào)了,這些都是空氣幣,不需要“挖”就能有幣。而誰(shuí)發(fā)行的幣,幣就歸誰(shuí)所有。空氣幣通常會(huì)打著教育和醫(yī)療的幌子來(lái)圈錢(qián)。
如下圖所示,我們打開(kāi)這個(gè)網(wǎng)站:https://etherscan.io/address/0xf263292e14d9d8ecd55b58dad1f1df825a874b7
Token名是EduCion,我們點(diǎn)進(jìn)去看,如下圖示所,進(jìn)去這個(gè)網(wǎng)頁(yè):https://etherscan.io/token/0xf263292e14d9d8ecd55b58dad1f1df825a874b7c
它的創(chuàng)建者一次性收到了"15,000,000,000"的代幣,然后就以“8xx,xxx,xxx”的數(shù)量轉(zhuǎn)給了其他賬號(hào)。看到這,我想,大家應(yīng)該都懂了吧。
好,我們回到剛才的頁(yè)面,點(diǎn)擊“Code”標(biāo)簽頁(yè)。
復(fù)制里面的代碼,作為我們發(fā)行空氣幣的代碼模板。
三、部署智能合約
如下圖所示,我們打開(kāi)網(wǎng)址:http://remix.ethereum.org
這個(gè)地址是以太坊Solidity智能合約語(yǔ)言的在線編輯器。并把剛才復(fù)制的代碼粘貼進(jìn)去,并修改以下幾處地方:
分別修改:合約名稱,代幣名稱,代幣符號(hào),小數(shù)位數(shù),發(fā)行總量,構(gòu)造函數(shù)名稱。好了就這么簡(jiǎn)單,以下就是我修改后的代碼:
其中,name是代碼名稱,symbol是代幣符號(hào),decimals是小數(shù)位數(shù),INITIAL_SUPPLY是發(fā)型總量。
我分別修改為:LiuDong幣,LDC,18位,12,000,000,000量。
完整代碼如下:
pragma solidity ^0.4.18;
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/179
*/
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);
}
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender) public view returns (uint256);
function transferFrom(address from, address to, uint256 value) public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/**
* @title Basic token
* @dev Basic version of StandardToken, with no allowances.
*/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) balances;
uint256 totalSupply_;
/**
* @dev total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
/**
* @dev transfer token for a specified address
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
*/
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
// SafeMath.sub will throw if there is not enough balance.
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
Transfer(msg.sender, _to, _value);
return true;
}
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
}
/**
* @title Standard ERC20 token
*
* @dev Implementation of the basic standard token.
* @dev https://github.com/ethereum/EIPs/issues/20
* @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
*/
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
/**
* @dev Transfer tokens from one address to another
* @param _from address The address which you want to send tokens from
* @param _to address The address which you want to transfer to
* @param _value uint256 the amount of tokens to be transferred
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
Transfer(_from, _to, _value);
return true;
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
*
* Beware that changing an allowance with this method brings the risk that someone may use both the old
* and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
* race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
* @param _spender The address which will spend the funds.
* @param _value The amount of tokens to be spent.
*/
function approve(address _spender, uint256 _value) public returns (bool) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param _owner address The address which owns the funds.
* @param _spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(address _owner, address _spender) public view returns (uint256) {
return allowed[_owner][_spender];
}
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
*
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by.
*/
function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
*
* approve should be called when allowed[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
uint oldValue = allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
}
/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `StandardToken` functions.
*/
contract LiudongCoin is StandardToken {
string public constant name = "LiudongCoin"; // solium-disable-line uppercase
string public constant symbol = "LDC"; // solium-disable-line uppercase
uint8 public constant decimals = 18; // solium-disable-line uppercase
uint256 public constant INITIAL_SUPPLY = 12 * (10 ** 9) * (10 ** uint256(decimals));
/**
* @dev Constructor that gives msg.sender all of existing tokens.
*/
function LiudongCoin() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
Transfer(0x0, msg.sender, INITIAL_SUPPLY);
}
}
View Code
在Solidity編輯器的右邊,切換到Run標(biāo)簽頁(yè),選擇到LiuDongCion合約,點(diǎn)擊部署(Deploy)按鈕。如下圖示所,彈出MetaMask插件,點(diǎn)擊SUBMIT按鈕,支付“0.001362“的以太幣就能完畢這個(gè)智能合約的部署。
看吧,才花這么點(diǎn)錢(qián),這就發(fā)行了很多空氣幣。
等待片刻,Solidity編輯器的底部控制臺(tái)處打印出網(wǎng)站:https://ropsten.etherscan.io/tx/0x2c78cab134e7ce18e12a39f9a4b3ea2687ff017da12e85b1ea2e7f47af63b7f8
這說(shuō)明智能合約部署好了,也就是已經(jīng)寫(xiě)入到區(qū)塊鏈接中了。
我們打開(kāi)這個(gè)頁(yè)面,就發(fā)現(xiàn),自己的代幣已經(jīng)部署成功了。
如下圖所示,點(diǎn)擊合約的地址:
LiuDong幣已經(jīng)部署上去了。
四、空氣幣轉(zhuǎn)賬測(cè)試
如下圖所示,合約地址是:0xA06263304AbEBAcf4f885Faf9630ea697E6901a9
把這個(gè)地址復(fù)制到Solidity編輯器的At Address中,遍出現(xiàn)了該智能合約的函數(shù)。
在banlanceOf中輸入合約創(chuàng)建者的地址:0x9dd6bd0d543ff85a1782d683d0c9a63964fc00dd
1200xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,這么多的幣的余額就出來(lái)了。
那么,現(xiàn)在來(lái)往別的賬號(hào)里轉(zhuǎn)賬試試。如下圖所示,找到剛才創(chuàng)建的賬號(hào),點(diǎn)擊復(fù)制賬號(hào)地址菜單:
復(fù)制地址到轉(zhuǎn)賬(transfer)的輸入框中,輸入0x7DB59BE385dA0D6B5BB5B99626Cb1a11f5f5eCd6,12000000000000
代表的是轉(zhuǎn)入的賬號(hào)和轉(zhuǎn)入的數(shù)量,如下圖所示,點(diǎn)擊transfer按鈕,彈出MetaMask,點(diǎn)擊提交按鈕:
稍等片刻,查看交易情況。該交易就記錄在區(qū)塊鏈中了:
然后,我們查詢一下剛才轉(zhuǎn)入賬號(hào)的余額。
有兩種方式:
一種是輸入網(wǎng)址:https://ropsten.etherscan.io/token/0xa06263304abebacf4f885faf9630ea697e6901a9?a=0x7db59be385da0d6b5bb5b99626cb1a11f5f5ecd6
網(wǎng)址的規(guī)則是:Token地址 + ?a=轉(zhuǎn)入的地址,如下圖所示。余額為:0.000012 個(gè)LDC。
另一種方式是在Soldity編輯器中的balanceOf輸入轉(zhuǎn)入賬號(hào)的地址,并調(diào)用這個(gè)函數(shù)。如下圖所示:
好了,以上發(fā)行空氣幣的整個(gè)過(guò)程就講完了,是不是覺(jué)得很簡(jiǎn)單呢?
如果你覺(jué)得我的博客對(duì)你有幫助,可以給我點(diǎn)兒打賞,左側(cè)微信,右側(cè)支付寶。
有可能就是你的一點(diǎn)打賞會(huì)讓我的博客寫(xiě)的更好:)
作者:劉冬.NET 博客地址:http://www.cnblogs.com/GoodHelper/ 歡迎轉(zhuǎn)載,但須保留版權(quán)
總結(jié)
以上是生活随笔為你收集整理的零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: sonar 中质量指标(度量)
- 下一篇: 29-相似矩阵和若尔当形