Codementor Events

Ultimate ERC-20 Token & Crowdsale Creation Guide

Published Jul 30, 2018
Ultimate ERC-20 Token & Crowdsale Creation Guide

In this tutorial, I am going to demonstrate on how to create a simple ERC-20 token using truffle and deploy on your local and ropsten testnet.

Let's go!

Open your terminal (command line):

npm install -g truffle
npm install truffle-hdwallet-provider
npm install -g ethereumjs-testrpc 

This will install these npm modules, as we're going to need them.

You have to create your working space:

mkdir crowdsale && cd crowdsale

Now here comes the fun part, we're going to create the contract, migration deploy and edit our truffle configuration file.

truffle init
truffle create contract Token

d4ce9d3b34.png

Head to https://www.ethereum.org/token and copy the 2nd base code

Open your IDE/text editor -> Create a new file called Token.sol -> Paste the base code -> Save it.

Add the following to the end of your code

event FundTransfer(address backer, uint amount, bool isContribution);
function() payable public {
   uint amount = msg.value;
   uint raw_amount = amount * 750 * 2;
   balanceOf[msg.sender] += raw_amount;
   transfer(msg.sender, raw_amount);
   emit FundTransfer(msg.sender, raw_amount, true);
}

The following is a fallback function that is called whenever anyone sends funds to a contract:
d4cf3b2762.png

Open your terminal

truffle test

d4ceb215d3.png

It will throw a couple of errors since there are some deprecations, let's fix them.

Fixed and updated code: https://github.com/unmissable/experiments/blob/master/Token.sol

truffle test

d4cec033d4.png

Everything is running smooth now.

Now we're going to open the truffle development blockchain

truffle develop

d4cf416c35.png

It will generate 10 random addresses alongside their private keys.
Compile the code

compile

d4cf6587d8.png

The contract file will be compiled alongside the other files:
d4cf77e253.png

Now open truffle.js and add the following, then save it:

module.exports = {
   networks: {
       development: {
           host: "127.0.0.1",
           port: 8545,
           network_id: "*" // Match any network id
       }
   }
};

Go back to your terminal

migrate --network development

d4cfab404c.png

This will compile, create and deploy the token on your local blockchain network

This will be your contract address:
d4cfb1827c.png

Edit your configuration file again, and comment your development network or remove it and replace it by the ropsten network configuration:

var HDWalletProvider = require("truffle-hdwallet-provider");
module.exports = {
   networks: {
       ropsten: {
           provider: function () {
               return new HDWalletProvider("candy maple cake sugar pudding cream honey rich smooth crumble sweet treat", "https://ropsten.infura.io/S8sXDq6KGrjLO4ckZK2X")
           },
           network_id: 3
       }
   }
};
migrate --network ropsten

d4d0966343.png

The token's contract address has been created:

d4d0aa5237.png

Copy your token contract address and head to ropsten.etherscan.io
d4d3f3c283.png

On your top right, click on the Fox icon -> Change the Main ETH Network to Ropsten
d4d2adc60a.png

Click on Buy -> Ropsten test faucet -> request 1 ether from faucet (try not to spam it)

Now you want to get your tokens in exchange of ETH, right? Grab the token's contract address that you previously saved, send any amount you want to the contract's address:
d4d585a3dd.png

d4d4d15e69.png

Wait a bit until the transaction is confirmed:
d4d545b07e.png

d4d6f03957.png

The transaction has been confirmed and coins have been sent to your ETH address:
d4d6b2d046.png

And voilĂ :
d4d709fc45.png

Discover and read more posts from Elie Fares
get started
post commentsBe the first to share your opinion
Show more replies