零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币

前言


  目前区块链是互联网中最最火的风口,没有之一。我周围的很多朋友也加入了“炒币”行列,但很不幸,几乎都被“割韭菜”了。而经过我的几天研究,发现,如果自己要发行一种空气币,简直太简单了。只需要下面几个步骤:

1.使用MetaMask

2.找Solidity代码模板

3.部署智能合约

4.空气币转账测试

 

一、MetaMask


在Chrome浏览器的网上应用店搜索MetaMask,如下图所示,如果搜到小狐狸logo的插件就对了,这就是以太坊浏览器(如果有无法打开Chrome网上应用店的朋友,就去搜索怎样科学上网的教程)。

把它添加到你的Chrome中。

打开浏览器的右上角的图标快捷菜单中,打开这个插件。如下图,选择Ropsten测试网络,并输入密码登录。第一次登陆需要设置密码。

默认会帮你创建一个账号,如果需要再创建账号,如下图所示,点击Crreate Account。

默认账号里是没有以太币的,如果选择的是主网(Main Ethereum Network),则需要从别的账号里转一些以太币过来。而这里,我用的是测试网络,如下图所示,点击“BUY”按钮,去免费零一些以太币来。

再点击“ROPSTEN TEST FAUCET”

如下图使用,打开了一个网页

狂点“request 1 ether from faucet”按钮,每次点击,就会获得免费的1个以太币。大概等5分钟左右时间,测试用的币就会到帐了。再检查你的账户余额,就不是零了。

 二、找代码模板


 如下图所示,打开火币Pro网站:https://www.huobipro.com/zh-cn/btc_usdt/exchange/

在其创新区里,几乎有90%的币都是基于ERC20智能合约发行的空气币。这里插一句题外话,如果有炒币的朋友,就要小心这些空气币。

我们拿前段时间炒的最火的币——EDU为例,来讲一下怎么发币。

如下图所示,我们找到区块查询的网址,这个网址就是以太坊ERC20这个智能合约发型Token的交易查询地址。

上面的币种类简介和白皮书不要看,都是忽悠人的。大家记号了,这些都是空气币,不需要“挖”就能有币。而谁发行的币,币就归谁所有。空气币通常会打着教育和医疗的幌子来圈钱。

如下图所示,我们打开这个网站:https://etherscan.io/address/0xf263292e14d9d8ecd55b58dad1f1df825a874b7

Token名是EduCion,我们点进去看,如下图示所,进去这个网页:https://etherscan.io/token/0xf263292e14d9d8ecd55b58dad1f1df825a874b7c

它的创建者一次性收到了"15,000,000,000"的代币,然后就以“8xx,xxx,xxx”的数量转给了其他账号。看到这,我想,大家应该都懂了吧。

好,我们回到刚才的页面,点击“Code”标签页。

复制里面的代码,作为我们发行空气币的代码模板。

三、部署智能合约


如下图所示,我们打开网址:http://remix.ethereum.org

这个地址是以太坊Solidity智能合约语言的在线编辑器。并把刚才复制的代码粘贴进去,并修改以下几处地方:

分别修改:合约名称,代币名称,代币符号,小数位数,发行总量,构造函数名称。好了就这么简单,以下就是我修改后的代码:

其中,name是代码名称,symbol是代币符号,decimals是小数位数,INITIAL_SUPPLY是发型总量。

我分别修改为: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标签页,选择到LiuDongCion合约,点击部署(Deploy)按钮。如下图示所,弹出MetaMask插件,点击SUBMIT按钮,支付“0.001362“的以太币就能完毕这个智能合约的部署。

看吧,才花这么点钱,这就发行了很多空气币。

等待片刻,Solidity编辑器的底部控制台处打印出网站:https://ropsten.etherscan.io/tx/0x2c78cab134e7ce18e12a39f9a4b3ea2687ff017da12e85b1ea2e7f47af63b7f8

这说明智能合约部署好了,也就是已经写入到区块链接中了。

 我们打开这个页面,就发现,自己的代币已经部署成功了。

 如下图所示,点击合约的地址:

LiuDong币已经部署上去了。

四、空气币转账测试


如下图所示,合约地址是:0xA06263304AbEBAcf4f885Faf9630ea697E6901a9

把这个地址复制到Solidity编辑器的At Address中,遍出现了该智能合约的函数。

在banlanceOf中输入合约创建者的地址:0x9dd6bd0d543ff85a1782d683d0c9a63964fc00dd

 1200xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,这么多的币的余额就出来了。

那么,现在来往别的账号里转账试试。如下图所示,找到刚才创建的账号,点击复制账号地址菜单:

 复制地址到转账(transfer)的输入框中,输入0x7DB59BE385dA0D6B5BB5B99626Cb1a11f5f5eCd6,12000000000000

代表的是转入的账号和转入的数量,如下图所示,点击transfer按钮,弹出MetaMask,点击提交按钮:

稍等片刻,查看交易情况。该交易就记录在区块链中了:

然后,我们查询一下刚才转入账号的余额。

有两种方式:

一种是输入网址:https://ropsten.etherscan.io/token/0xa06263304abebacf4f885faf9630ea697e6901a9?a=0x7db59be385da0d6b5bb5b99626cb1a11f5f5ecd6

网址的规则是:Token地址 + ?a=转入的地址,如下图所示。余额为:0.000012 个LDC。

另一种方式是在Soldity编辑器中的balanceOf输入转入账号的地址,并调用这个函数。如下图所示:

好了,以上发行空气币的整个过程就讲完了,是不是觉得很简单呢?

如果你觉得我的博客对你有帮助,可以给我点儿打赏,左侧微信,右侧支付宝。

有可能就是你的一点打赏会让我的博客写的更好:)

作者:刘冬.NET 博客地址:http://www.cnblogs.com/GoodHelper/ 欢迎转载,但须保留版权
 
 
 
 

 

 

 
 
 

猜你喜欢

转载自www.cnblogs.com/GoodHelper/p/9164894.html
今日推荐