Make yourself a Token in 18 years

Note: Generally, everyone likes to call Token token , but this is very inaccurate. If it has to be called Chinese, I agree with the understanding of the token. However, in order to keep the original flavor, there is no need to translate it, so this article is uniformly kept in English.

Generally, the threshold for token production is not very high, so everyone has the ability to create their own tokens.

However, don’t expect to be able to make some tokens with additional logic without learning anything, such as crowdfunding contracts, etc., so if you want to do something different, you need to spend some cost.

Let’s go straight to the topic, we don’t need to know how to write smart contracts, because the ERC20 protocol provided by Ethereum is foolish enough. To make a simple Token, the official has a standard smart contract code as an example, then we just need to know The production process will do.

Long-winded, in fact, the same is true for learning other technical knowledge. We should not rush to pursue very internal things. We need a positive feedback learning method. First make some simple demos, and gradually build up self-confidence and stimulate enthusiasm while continuing to learn more deeply. .

Install MetaMask

We first need to install a browser plug-in MetaMask , the author of the plug-in is very nice, and this practical plug-in can be installed on the three major browsers (Google, FireFox, Opera). The process of installing this plug-in will not be repeated. Now assume that we have installed it, click on the plug-in, and after two operations of Accept and agree to the terms, enter the page for creating an account.

Note the three arrows on the picture

  • The first arrow indicates that we are currently in the Ropsten Test Net test network. The main difference from the Ethereum main network that everyone uses is that everyone only recognizes the value of ether on the main network, and this test network is mainly used for development. test.
  • The second arrow means creating a new account contract as a legitimate user on the Ethereum network.
  • The third arrow means importing an account contract, if you have an account contract on the network.

Create a new contract account

After entering your own password greater than eight characters on the account creation page, enter the mnemonic phrase page, these mnemonic phrases are the only keys that can help you save your account contract and keep it safe.

buy ether

Don't panic, it's not for you to actually pay for it, you can get ether with just a few button clicks on the website provided by MetaMask (after all, the ether on these testnets has no real value).

After saving the mnemonic, we will enter the main account page,

We can see that the balance on our account is zero, click the purchase button of arrow 1 to enter the confirmation page
Then click the arrow 1 button in the above picture to enter the purchase page, the website is Test Ether Faucet
Click the arrow 1 button to get one ether (click to get one at a time), I got 6 ether, and the arrow 2 is the transfer transaction record.

OK, let's put MetaMask for a while , then let's play Remix !

Meet the Remix IDE

Remix is ​​a browser-based Solidity IDE, and it is quite comfortable to develop and debug basic smart contract code. Remix - Solidity IDE

  • The first arrow is the project directory bar, where you can create a file directory
  • The second arrow is the code bar, where our contract code is written
  • The third arrow is the log bar, and the log output of deployment debugging is displayed here
  • The fourth arrow is the debug bar, where the deployment and debugging of the contract are carried out.

Need to familiarize yourself with this tool? Of course, and the more familiar the better, but not now, don't forget that our goal now is to make our own Token.

Token contract

There is a standard Token contract on the official Ethereum website for learners' reference, and there are also very detailed tutorials to teach you how to deploy your own Token. Create a cryptocurrency contract in Ethereum

The difference between this article and the official one is that we use the light wallet MetaMask and the online development environment Remix for development, debugging and deployment. The official Ethereum wallet has a very troublesome place for beginners. It is very slow to synchronize the full node data of the main network or the test network. , and it takes up a lot of disk space. Try to choose a relaxed way for the first experience, don't leave yourself a bad mood :)

We copy the official Token contract code to the code column of Remix, and paste the contract code here:

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;
    }
}

deploy contract

Next, we first select the version of our compiler, corresponding to the version number of the first line of the contract codepragma solidity ^0.4.16

Click the tab of arrow 1 to enter the setting interface, and then click arrow 2 to select the corresponding compiler version, here is 0.4.16.

Then click the compile tab (Compile), you will see that the compilation has been successful

Then click the Run tab (Run) to deploy

  • The first arrow selects the deployment environment, which will automatically detect the network (Ropsten Test Net) where our MetaMask is currently located.
  • The second arrow enters some basic information about our Token
    • The first parameter represents the total issuance of Tokens, I just fill in 10 million here.
    • The second parameter indicates the Token name
    • The third parameter represents the Token symbol, which is generally used as the Token identification during the circulation process
  • Click the third arrow to create a deployment transaction order and start the final deployment

The only things that can be changed here are Gas Limit and Gas Price. Generally speaking, these two data will be automatically generated according to the complexity of the current contract and the average market level. Therefore, it is generally not necessary to change these two values. If you are shy, you can change it to a lower value, but it may happen that the gas is consumed (once the gas is consumed and the contract has not yet been deployed to the network, then All operations are rolled back, except for the miner fee you pay).

Click the publish button (SUBMIT) and wait for the release to be successful!

Open the MetaMask page

The picture above shows the status of the release, and the picture below shows the status of the release. At this time, we will find that our account balance has decreased a little because it is used to pay the miner fee for publishing the contract.

At this point, it is not clear whether it has been successfully released. Click the transaction ticket to enter the transaction details page. When you see Success at arrow 1, it means that our Token has been released successfully!

Click the contract address link at arrow 2 to enter the contract details page

Click the Token address link at the arrow to enter the Token details page

So far our Token is done!

Token is displayed in the wallet

Let's take MetaMask as an example to demonstrate. Other wallets such as ImToken can see the same by copying the Token address.

Switch to the Token tab first

Click Add Token and enter the Token contract address just now

Click the Add button to see our own Token on MetaMask.

summary

In the whole process of making Token, there is no difficulty, but most people do not know the process, and they can get started immediately after practicing it. We will play with some more complex smart contracts later, but again, some basic Solidity programming language knowledge and blockchain-related knowledge are required. Solidity, the standard programming language of Ethereum, will also be explained in detail, so please look forward to it~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982290&siteId=291194637