搬运工系列(1)--《正确的发币姿式》

 

《正确的发币姿式》html

 

前言:程序员

在以前的《程序员的自我救赎》系列中其实已经讲解过如何用发行区块链代币。web

                12.2.1 :虚拟币交易平台(区块链) 上 【发行区块链代币】浏览器

                12.2.2: 虚拟币交易平台(区块链) 中 【开发交易所】服务器

                12.2.3: 虚拟币交易平台(区块链) 下 【C#与以太坊通信】app

 

也有不少人抱怨说基于以太坊发币的流程,虽然比拿比特币源码修改发币要简单,可是因为要同步区块。须要等待很长的时间,有没有不用同步区块的办法?ide

答案是:固然有啦!区块链

 

今天,开始一个新的系列-----《搬运工》测试

 

============================华丽的分割线 ===============================ui

 

 

第一步:准备一份智能合约

 

先讲解一下,若是再不须要同步区块的状况下,利用以太坊发币!

其实很简单,首先咱们须要一份以太坊发币的智能合约,这个官网就有下载了,我这里直接贴出一份。

 

 https://ethereum.org/token

 官网的智能合约是能够直接用的,可是咱们知道照抄也要改个名字嘛!

 

 

 

我再贴一个我改好的合约:

 

pragma solidity ^0.4.16;

interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }

contract AllCoin{
    // Public variables of the token
    string public name;
    string public symbol;
    uint8 public decimals = 8;
    // 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
     */
   constructor(
        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;
        emit 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
        emit 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
        emit Burn(_from, _value);
        return true;
    }
}
View Code

 

 

=======================华丽的分割线==========================

 

第二步:编译智能合约

 

好了,这里咱们已经准备好了发行本身代币的一份智能合约,下面要介绍一个神器。

以太坊智能合约在线编译器:remix  !

http://remix.ethereum.org/

 

咱们把刚刚那份智能合约复制--粘贴到remix中,这个是即时编译的!

 

 

 选择Run,运行智能合约。这里系统会分配5个以太坊帐号给到咱们,每一个里面有100个以太币。

 不要笑! 是测试环境! (正式环境要有500个以太坊,就发了!)

 

接着日后面说,这里随便选择一个帐号,反正都是测试的。而后咱们输入发币的基本参数:

 

 

 

 

 

1,选择JavaScript VM,程序员朋友看到VM就太熟悉了!

2,展开要填写的参数,不展开的话也能够同样写完,可是那样看起来不舒服。

3,填写发币所需的参数:

  initialSupply:发行总数

  tokenName:代币名称,好比比特币是:BitCoin,简写是"BTC"

     tokenSymbol: 简写好比 ETH,EOS

4,复制钱包地址到 At Address 那一栏,意思就是将合约部署在哪一个帐号。

5,粘贴钱包地址,点击At Address。

6,点击Transact,部署智能合约。

 

 

 

 这里咱们就已经成功的在测试环境部署了一份代币智能合约,咱们测试一下,将钱包地址复制到文本框,而后点击 balance Of。就能够看到

咱们在这个钱包下就有币了。(这个后面会多不少“0”,不用在乎,能够理解为去掉了小数点,实际位数是对的!)

 

===================================华丽的分割线============================

 

第三步:使用matemask

 

这里咱们已经利用remix 顺利的在 “测试环境”下 发币成功,那么如何部署在正式环境?

咱们要用到另一件神器: MateMask

 

https://metamask.io/

 

咱们能够直接用谷歌浏览器,安装这个插件。(怎么安装? 直接拖进去就能够了!)

若是不能下载的,请使用***,自备梯子!

 

 

 简单介绍一下matemask,它就是一个在线钱包,等于别人在他服务器上同步好了区块,而后以web的方式呈现给你用。

默认的咱们会分配一个帐户。Account1。

 

这个Account1里面是没有以太币的,咱们都知道发代币是要消耗gas的,也就是以太币。没有币的话有两种方式:

 

1,去交易所买而后再提币到这个钱包,可是交易所会收提币手续费,这个具体买多少看着办吧!发币的话,其实0.1个以太币就足够了!

2,将本身有币的以太坊钱包,导入到matemask。

 

我这里使用的第二种方式:

 

 

 

 

 

===================================华丽的分割线============================

 

第四步:正式环境部署!

 

第四步最简单,咱们再次回到remix界面上。

 

 

 这个时候,咱们会发现MateMask,会经过web3.js 自动给咱们注入钱包到remix,咱们选择injected web3。

这里就能够看到咱们本身的钱包地址。而后其余的流程都是同样(重复第二步)

 

等同于以前是用虚拟环境部署在虚拟帐户上,如今是经过matemask部署在正式环境上!

 

===================================华丽的分割线============================

 

第五步:收尾

 

 

部署的时候,会弹出确认框,这里有说明须要9Gwei 以太币做为gas,这里我建议不要改数字。

咱们知道,给的gas越多,打包确认的速度就越快。给的越少,就越慢。 我此次部署须要9Gwei 是remix自动帮我算出来的合理的价格。

我以前就想少给一点,结果发现,等了5个小时没有被确认。(以太坊12个区块确认,才算正式确认)

 

最后放弃了,好在MateMask,有追加gas的功能。我补了点进去,最后几秒钟就确认成功了!

 

 

发币是否成功,以及等待时间,咱们能够上  https://etherscan.io/ 去查询!

 

最后,咱们发币成功后,就能够自由的转帐了,好比我把钱包导入到Imtoken钱包。

剩下的,至于您要空投,仍是要上交易所,那就是运营的事情了!

 

是否是比同步钱包而后发币,简单多了!