Ultimate ERC-20 Token & Crowdsale Creation Guide
In this tutorial, we're 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.
We'll need to create our 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
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
Open your terminal
truffle test
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
Everything is running smooth now.
Now we're going to open the truffle development blockchain
truffle develop
It will generate 10 random addresses alongside their private keys.
Compile the code
compile
The contract file will be compiled alongside the other files:
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
This will compile, create and deploy the token on your local blockchain network
This will be your contract address:
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
The token's contract address has been created:
Copy your token contract address and head to ropsten.etherscan.io
On your top right, click on the Fox icon -> Change the Main ETH Network to Ropsten
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:
Wait a bit until the transaction is confirmed:
The transaction has been confirmed and coins have been sent to your ETH address:
And voilà: