Copied


Smart Contract 101: Build you own token by Truffle

Johnny J   Nov 12, 2020 15:03 0 Min Read


20201105_truffle_feature.jpg

To begin, you need to prepare a Mac / Linux device.

Step 1: Install NPM (Download Link)

Step 2: Install Truffle, type the below command on your command prompt

npm install -g truffle

Step 3: Create a folder for your project

mkdir myToken

cd myToken

truffle init

Step 4: Install Openzeppelin, this is a standard template library for smart contracts. Using the verified smart contract code is safer and faster to build your token.

npm install @openzeppelin/contracts

Step 5: Create a document named Token.sol inside the folder contracts. Use a text editor to type following codes.

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

contract Token is ERC20, ERC20Detailed {

  constructor () public

  ERC20Detailed("MyToken", "TKN", 18){

    _mint(msg.sender,100 * (10 ** uint256(decimals())));

  }

 

}

From the above code, you are going to create a Token named MyToken, short name is TKN with 18 decimal places. The token owner (you) will get 100 tokens of TKN.

Step 6: Compile the contract by Truffle, just type the command:

truffle compile

Step 7: Deploy to the main network, you need to edit the file truffle-config.js

var HDWalletProvider = require("truffle-hdwallet-provider"); 

var mnemonic_mainnet = "12 Phrases"; 

module.exports = {

networks: {

      mainnet: {

        provider: new HDWalletProvider(mnemonic_mainnet, "https://mainnet.infura.io/xxxxxx"),  //apply the key from https://infura.io/

        network_id: 1,

        gas: 3020000,

        gasPrice: 18000000000,

        confirmations: 0,    

        timeoutBlocks: 50,  

        skipDryRun: true     

      }

}

};

Step 8: Type the following command to put your smart contract to the network

truffle migrate --network mainnet

Congratulations! You have your own Token published!


Read More