Vous êtes sur la page 1sur 24

Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.

Global Open Versity


Blockchain DevOps Hands-on Labs Training Manual

Step-By-Step Guide Writing & Testing Real-world Crowdsale Contracts


on Ethereum
Kefa Rabah
Global Open Versity, Vancouver Canada
krabah@globalopenversity.org
www.globalopenversity.org

Table of Contents Page No.

STEP-BY-STEP GUIDE WRITING & TESTING REAL-WORLD CROWDSALE CONTRACTS


ON ETHEREUM 2

Introduction 2

What is Ethereum Smart Contracts 2

Ethereum: Decentralized Applications 3


Crowdfunding Campaign 3
How to create a Crowdsale contract for ICO 3

Overview 4

Hands-on 4

Part 1: Install Blockchain Required Tools 4

Part 2: Get started with Smart contracts with Web3 on testRPC 5


Step 1: Installations of Tools 5
Step 3: Install Ganache 5
Step 4: Install Other Required Tools 5
Step 5: Install Openze 6

Part 3: Get started with Token contracts & Initial Coin Offering 6
Step 1: Setting up the project 7
Step 2: Creating the Code 7
Step 3: Compiling and Deploying the Token Contracts 13
Step 4: Test our Contract 16

Part 4: Deploy to Ropsten TestNet 17


Step 1: Setting up MetaMask 17
Step 2: Buying ether 21
Step 3: Way forward 23

1
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Global Open Versity


Blockchain DevOps Hands-on Labs Training Manual

Step-By-Step Guide Writing & Testing Real-world Crowdsale Contracts


on Ethereum
By Kefa Rabah, krabah@globalopenversity.org April 28, 2018 The Lake Institute

Introduction
As you might have realized by now that there is tremendous buzz about Ethereum smart contracts out
there and as you have already learned from our Blockchain Labs Tech Series of Hands-On. And as you
may recall, a blockchain is a decentralized distributed database that is used to maintain a continuously
growing list of records, called blocks, that is not controlled by a central authority like government or a third
party. Smart technology is deemed to be secure, reliable, world-changing disruptive technology and
overall predict our near-future to be governed through micro-transactions between individuals – that is a
world that is expected to be trustworthy only when we have smart-contracts in all areas of our life”. This is
all certainly true, yet, to a certain extent, as shown in a sample representation of Fig. 1.

Fig. 1

What is Ethereum Smart Contracts


In general, any contract is a set of rules that describes what happens in certain given conditions.
However, a smart contract is a set of rules executed automatically if those conditions occur! Thus, it is a
piece of code written onto the blockchain, meaning it is transparent (visible to anybody) and reliable as the

2
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

blockchain itself. And this is what actually makes it so powerful if you think about it. Since it’s so
transparent and runs on a blockchain, it eliminates the need for trust to the third parties. Individuals and
small companies can interact without even knowing each other and feel safe for their funds, because it is
the contract that governs outcome.

To make it even more clear: there is no need for third parties, that would create paper contracts, verify
personalities, make decisions in conflict situations, etc. – that is the contract is right there and it is
automatic. Yes, nowadays technology allows that! With certain limitations, though. Yet it is a great
decentralization already!

Ethereum: Decentralized Applications


Ethereum is probably the most popular blockchain platform that exists right now. They have had a public
release for more than 3 years and it has a thriving community that maintains the project. They also have
developed their own programming language called Solidity that lets us write and interact with smart
contracts. Ethereum has a public blockchain that lets people run code on and it one of the simplest way to
start programming on the blockchain.

Crowdfunding Campaign
Suppose some creative folks are about to create an awesome product, so they need funding to make it
happen at a large scale. How do they get a perfect sponsor? Run a crowdfunding campaign! First they
determine how much money is needed, secondly advertise the idea to the world and showcase the
prototype, and, most importantly, make a promise that anyone investing now would get the product later
proportional to the investment made. We can leverage this thanks to the smart contracts technology!

Sometimes a good idea takes a lot of funds and collective effort. You could ask for donations, but donors
prefer to give to projects they are more certain will get traction and proper funding. This is an example
where a crowdfunding would be ideal: you set up a goal and a deadline for reaching it. If you miss your
goal, the donations are returned, therefore reducing the risk for donors. Since the code is open and
auditable, there is no need for a centralized, trusted platform and therefore the only fees everyone will pay
are just the gas fees.

One of the most important and complex tasks that each project leader faces is the creation of a Crowdsale
contract (the contract). This is a program for creation and distribution (sale) of project’s tokens. The price
of one Crowdsale contract is exactly 1 ETH. This is a complete solution used by a number of projects to
raise funds.

How to create a Crowdsale contract for ICO


In this article we will look at the process of preparing for ICO — the production and sale of tokens to attract
investment in the project. After the team is assembled and the business plan is developed, the project
goes to the stage of its implementation. One of the most important and complex tasks that each project
leader faces is the creation of a Crowdsale contract (the contract). This is a program for creation and

3
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

distribution (sale) of project’s tokens. In this article, we will consider possible ways of creating the contract,
their pros and cons; and also emphasize the simplest and the most reliable ones.

Overview
TestRPC is a simulation of an Ethereum blockchain, which comes with 10 pre-defined Ethereum accounts
and supports mnemonics (that is, one can generate the same set of accounts with the same set of
mnemonics). It does not come with a User Interface as Remix, and we need node console plus the web3
library to interact with this blockchain.

Hands-on
It's no secret - ICOs are all the rage these days. Many new companies are raising millions of dollars by
selling their tokens in crowdsale. If you are reading this article, you are probably pursuing the idea of
developing an ICO. Here you’ll be using the usual tools (testrpc) or Ganache-cli, MetaMask etc. to
complete your hands-on lab.

Pre-requisite or the Hands-on Labs


This tutorial requires that you already have some knowledge of Truffle, Ethereum, and Solidity. You can
start with this tutorial to get yourself up to-speed, check our Blockchain Labs Tech Series to quick you a
quick start:

1. https://www.scribd.com/document/373203852/Step-By-Step-Guide-Installing-Ethereum-Building-
a-Blockchain-on-Ubuntu-16-04-Linux-Server

2. https://www.scribd.com/document/373203852/Step-By-Step-Guide-Installing-Ethereum-Building-
a-Blockchain-on-Ubuntu-16-04-Linux-Server

3. https://www.scribd.com/document/374058743/Step-By-Step-Guide-Building-Deploying-a-Private-
Blockchain-Network-on-Windows

4. https://www.scribd.com/document/375953366/Developing-Smart-Contract-on-Ethereum-
Blockchain-Using-Truffle-Framework

5. https://www.scribd.com/document/376560099/Step-By-Step-Guide-on-Deploying-Revenue-
Sharing-Blockchain-Smart-Contracts-using-Remix-IDE-on-Windows

6. https://www.scribd.com/document/376874686/Step-By-Step-Guide-on-Deploying-Blockchain-
Voting-Smart-Contracts-on-Windows

Part 1: Install Blockchain Required Tools


The tools we will use
The most prominent tools at the moments are:

4
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

• Truffle: A development environment, testing framework and asset pipeline for Ethereum. In other
words, it helps you develop smart contracts, publish them, and test them, among other things. You
can read the docs of the Truffle suite for more informations.
• Ganache: It was called TestRPC before, if you have read a tutorial from a few months ago, chances
are they use TestRPC with Truffle, but it was renamed upon the integration of TestRPC within the
Truffle Suite. What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates
some fake accounts that we will use during development.
• The Metamask Chrome extension

We will start by using Truffle and Ganache, and then use Truffle with geth and Mist.

Part 2: Get started with Smart contracts with Web3 on testRPC


We’re going initially install our required tools for this Hands-On

Step 1: Installations of Tools


1. The requirements for this tutorial are that you know what is and how to use a command-line tool, and
you are a bit familiar with NPM, which we must download and install first. At the time of writing this
article, we used “node-v9.8.0-x64.exe”

Step 3: Install Ganache


2. Then, install Ganache’s command-line interface:

npm install -g ganache-cli

3. If all is OK, then type command ganache-cli,

4. If you are unsure about something, here is Ganache’s Github page.

Note: there is a GUI for Ganache, but because we are such haxors, we will use the CLI.

Step 4: Install Other Required Tools


5. Next we need to install some of the tools required:

npm i -D webpack react react-dom babel-core babel-loader babel-preset-react


babel-preset-env css-loader style-loader json-loader web3@0.20.0

Note: these are all the tools that we need to create the front-end of the dapp. There are a lot of
dependencies because we’ll be creating a web application with the latest version of javascript and
React.js.

5
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Mainly we installed webpack, react, babel and web3.

Note that we are installing the version 0.20.0 of web3 with web3@0.20.0 because the latest version
of web3, the 1.0 is still in beta and it’s unstable.

Step 5: Install Openze


6. To install the OpenZeppelin library, run the following in your Solidity project root directory:

npm init –y

npm install -E zeppelin-solidity@1.8.0

Note that OpenZeppelin does not currently follow semantic versioning. You may encounter
breaking changes upon a minor version bump. We recommend pinning the version of OpenZeppelin
you use, as done by the -E (--save-exact) option.

OpenZeppelin source files are available in your node_modules folder.

After that, you'll get all the library's contracts in the node_modules/zeppelin-
solidity/contracts folder. You can use the contracts in the library like so:

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';

contract MyContract is Ownable {


...
}

Security: OpenZeppelin is meant to provide secure, tested and community-audited code, but please
use common sense when doing anything that deals with real money! We take no responsibility for
your implementation decisions and any security problem you might experience.

7. You’re done with this section.

Part 3: Get started with Token contracts & Initial Coin Offering
Here we are going to use some contracts from OpenZeppelin. There is no need to reinvent the wheel, so
we can just use safe and proven patterns built by world-class experts and verified by OpenZeppelin.

6
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Step 1: Setting up the project


1. Go to your workspace and create your project directory, which here have named it
"L10trufJCR_SC":

$ mkdir trufCrowdsaleProject && cd trufCrowdsaleProject

2. Next, initialize the truffle project by running the following command:

$ truffle init

Once the command finishes you will see the following directory structure:

contracts/
migrations/
test/
truffle.js
truffle-config.js

Note: as the name suggests, all your Solidity source files go inside contracts/ directory. After you
compile your files for the first time, you can see a build/ directory which contains all your compiled
code.

3. Now use npm to initialize the project by running the following command:

$ npm init

Note: this will add two files "package.json", and "package-lock.json"

4. To install Open Zeppelin, run the following command:

npm install zeppelin-solidity@1.5.0

Note: at the time of writing, we used zeppelin-solidity@1.8.0. The above command should install the
zeppelin-solidity package inside node_modules. You can find the token & ICO contracts
inside "node_modules/zeppelin-solidity/contracts" directory. We'll import these from our
own solidity code.

Step 2: Creating the Code


5. To import a contract from OpenZeppelin, just use the import command and write down the path of the
contract, which you are going to import, as follows:

import "zeppelin-solidity/contracts/token/MintableToken.sol";

7
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

6. First step is to create the token contract, under the contracts directory.

$ nano contracts/HashnodeToken.sol

and copy and paste the code:

pragma solidity ^0.4.18;

import 'zeppelin-solidity/contracts/token/MintableToken.sol';

contract HashnodeToken is MintableToken {


string public name = "Hashnode Token";
string public symbol = "HT";
uint8 public decimals = 18;
}

As you may have guessed, we are just extending MintableToken provided by zeppelin-solidity
package. MintableTokenitself inherits ERC20 token contract (find it inside zeppelin-
solidity/contracts/token/ directory). So, the end result is that our new token HashnodeToken is an
ERC20 token.

Note: MintableToken means that the total supply of the token starts with 0 and increases as people
purchase the tokens in the crowdsale. If you decide to create 100 tokens and sell 60 of them in
crowdsale, the supply will increase up to 60 (as people pay ETH and buy the tokens). Once the
crowdsale is over, 40 more tokens will be minted making the total supply 100.

Feel free to change the above code and supply appropriate values for name and symbol. It's
recommended to set the decimals to 18 in order to be standard compliant.

7. Now that our ERC20 token is ready, let's proceed to code our crowdsale contract. Create the file
contracts/HashnodeCrowdsale.sol and paste the following content:

nano contracts/HashnodeCrowdsale.sol

We are basically going to inherit the contract at

node_modules/zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol'

and

node_modules/zeppelin-solidity/contracts/crowdsale/RefundableCrowdsale.sol'

8. Thus, copy and paste it in the file "HashnodeCrowdsale.sol" .

pragma solidity ^0.4.18;

import './HashnodeToken.sol';
import 'zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol';
8
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

import 'zeppelin-solidity/contracts/crowdsale/RefundableCrowdsale.sol';

contract HashnodeCrowdsale is CappedCrowdsale, RefundableCrowdsale {

// ICO Stage
// ============
enum CrowdsaleStage { PreICO, ICO }
CrowdsaleStage public stage = CrowdsaleStage.PreICO; // By default it's Pre Sale
// =============

// Token Distribution
// =============================
uint256 public maxTokens = 100000000000000000000; // There will be total 100
Hashnode Tokens
uint256 public tokensForEcosystem = 20000000000000000000;
uint256 public tokensForTeam = 10000000000000000000;
uint256 public tokensForBounty = 10000000000000000000;
uint256 public totalTokensForSale = 60000000000000000000; // 60 HTs will be sold
in Crowdsale
uint256 public totalTokensForSaleDuringPreICO = 20000000000000000000; // 20 out
of 60 HTs will be sold during PreICO
// ==============================

// Amount raised in PreICO


// ==================
uint256 public totalWeiRaisedDuringPreICO;
// ===================

// Events
event EthTransferred(string text);
event EthRefunded(string text);

// Constructor
// ============
function HashnodeCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate,
address _wallet, uint256 _goal, uint256 _cap) CappedCrowdsale(_cap)
FinalizableCrowdsale() RefundableCrowdsale(_goal) Crowdsale(_startTime, _endTime,
_rate, _wallet) public {
require(_goal <= _cap);
}
// =============

// Token Deployment
// =================
9
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

function createTokenContract() internal returns (MintableToken) {


return new HashnodeToken(); // Deploys the ERC20 token. Automatically called
when crowdsale contract is deployed
}
// ==================

// Crowdsale Stage Management


// =========================================================

// Change Crowdsale Stage. Available Options: PreICO, ICO


function setCrowdsaleStage(uint value) public onlyOwner {

CrowdsaleStage _stage;

if (uint(CrowdsaleStage.PreICO) == value) {
_stage = CrowdsaleStage.PreICO;
} else if (uint(CrowdsaleStage.ICO) == value) {
_stage = CrowdsaleStage.ICO;
}

stage = _stage;

if (stage == CrowdsaleStage.PreICO) {
setCurrentRate(5);
} else if (stage == CrowdsaleStage.ICO) {
setCurrentRate(2);
}
}

// Change the current rate


function setCurrentRate(uint256 _rate) private {
rate = _rate;
}

// ================ Stage Management Over =====================

// Token Purchase
// =========================
function () external payable {
uint256 tokensThatWillBeMintedAfterPurchase = msg.value.mul(rate);
if ((stage == CrowdsaleStage.PreICO) && (token.totalSupply() +
tokensThatWillBeMintedAfterPurchase > totalTokensForSaleDuringPreICO)) {
msg.sender.transfer(msg.value); // Refund them
EthRefunded("PreICO Limit Hit");
return;
}
10
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

buyTokens(msg.sender);

if (stage == CrowdsaleStage.PreICO) {
totalWeiRaisedDuringPreICO = totalWeiRaisedDuringPreICO.add(msg.value);
}
}

function forwardFunds() internal {


if (stage == CrowdsaleStage.PreICO) {
wallet.transfer(msg.value);
EthTransferred("forwarding funds to wallet");
} else if (stage == CrowdsaleStage.ICO) {
EthTransferred("forwarding funds to refundable vault");
super.forwardFunds();
}
}
// ===========================

// Finish: Mint Extra Tokens as needed before finalizing the Crowdsale.


// ====================================================================

function finish(address _teamFund, address _ecosystemFund, address _bountyFund)


public onlyOwner {

require(!isFinalized);
uint256 alreadyMinted = token.totalSupply();
require(alreadyMinted < maxTokens);

uint256 unsoldTokens = totalTokensForSale - alreadyMinted;


if (unsoldTokens > 0) {
tokensForEcosystem = tokensForEcosystem + unsoldTokens;
}

token.mint(_teamFund,tokensForTeam);
token.mint(_ecosystemFund,tokensForEcosystem);
token.mint(_bountyFund,tokensForBounty);
finalize();
}
// ===============================

// REMOVE THIS FUNCTION ONCE YOU ARE READY FOR PRODUCTION


// USEFUL FOR TESTING `finish()` FUNCTION
function hasEnded() public view returns (bool) {
return true;
}
11
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

The code is pretty self-explanatory and is well commented. Let me outline a few important points:
• Our crowdsale contract inherits CappedCrowdsale and RefundableCrowdsale (supplied by
zeppelin-solidity) and therefore has a goal and a hard cap. If the contract isn't able to raise a
certain minimum amount of ETH during the crowdsale, the ETH amounts will be refunded to
the investors. Similarly, the contract will not be able to raise more than a specific amount of
ETH due to a hard cap.

• Total 100 tokens will be created by the end of the crowdsale. Out of 100, 60 will be sold in the
crowdsale. Once the crowdsale is over, rest 40 tokens will be (minted and) divided among
three wallets such as teamFund, ecosystemFund and bountyFund.
• The crowdsale has two stages: PreICO and ICO. You can change the stage and update rate
variable to offer extra discounts during presale. As per the above crowdsale contract 1 ETH
can buy 5 tokens in PreICO and just 2 tokens in public sale. So, the early investors get extra
discounts. Note: Max 20 tokens will be sold in PreICO.
• When PreICO is live, the incoming ETH amounts are immediately transferred to the
beneficiary wallet (supplied while deploying the contract). However, in the public sale the
raised ETH amounts are sent to a refund vault. If the crowdsale reaches its goal, the funds
are transferred to the beneficiary wallet. Otherwise, investors are allowed to claim refunds
(check zeppelin-solidity/contracts/crowdsale/RefundVault.sol).
• You have to call finish() to close the crowdsale. This is where remaining tokens are minted
and distributed among various reserved funds. Note: Any unsold tokens are added to the
ecocystem fund.

9. Now, let’s add this to truffle.js file. It is for configuration purposes:

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
gas: 6500000,
network_id: "5777"
}
},
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
};

10. Next, go on (you may need to create it) to the file migrations/2_HashnodeCrowdsale.js and
modify it to this:

cd migrations
12
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

nano 2_HanshnodeCrowdsale.js

and copy and paste into it the code below:

var HashnodeCrowdsale = artifacts.require("./HashnodeCrowdsale.sol");

module.exports = function(deployer) {
const startTime = Math.round((new Date(Date.now() - 86400000).getTime())/1000);
// Yesterday
const endTime = Math.round((new Date().getTime() + (86400000 * 20))/1000); //
Today + 20 days
deployer.deploy(HashnodeCrowdsale,
startTime,
endTime,
5,
"0x75b67ad7705fbe440c3d6e1b308a581183cea458", // Replace this wallet address
with the last one (10th account) from Ganache UI. This will be treated as the
beneficiary address.
2000000000000000000, // 2 ETH
500000000000000000000 // 500 ETH
);
};

8. You’re done with this section.

Step 3: Compiling and Deploying the Token Contracts


First and foremost, we need to start Ganache-cli (TestRPC).

1. Start TestRPC
9. Open two terminals, on terminal 1, start the TestRPC:

$ testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
Available Accounts
==================
(0) 0x2377b69895039cc4b04950aeba2068f0855c6c75
(1) 0x6fa8fa7d660463600f0839c1661983a2984d38ff
(2) 0x0928baabdd60e6c617c46dcf31b654775d08faef
(3) 0xbfbbc66a115aedfa294b66cc7c9e3e7480957e9b
(4) 0xacae8ffc5011be365ba0eb49160093facc0aaa52
(5) 0xe78c6b168c115e39fcaf9663d8cb9edffdf556ab
(6) 0x5efe7397edb26510633c8dab82bfaa9cbe9fd312
(7) 0x99a8a06d0068bca1c76ee4d3e1cf70e9995078e5
(8) 0x84c04e530fcbcebb8bfd38712f728743252d3a04
(9) 0x75b67ad7705fbe440c3d6e1b308a581183cea458

Private Keys
==================
(0) ded40af96542ea9916c97c13927bdf14eade07ab5aae735074683da8994b3b0c
(1) a206d748778d023a9622d0a9a367926ed0093de14ce1768315fca3ec5764ad12
(2) 7857148c7e51b15fd2218185a9eb41414b7e6e1b44112bb15a2665460c2c5ae0
(3) 921f838bcd0a656d5811f6b54dccf64114533b8b0609310ac75dfbaf1e2e3f17
(4) 616d256ca11b32d46e8f840031a29cddedfab3cb5b67aa39213f9cc16ea9bd20
(5) 530b4f33b880d8306f220220a97ec52b9251c4131bb4600d665fc113924a8eea
(6) ebddacacfc2baa03658d0b79fb10e124cb4bd2c8d5f84d59c374fa246c1f13e3
(7) e52931d70a66e4130f0ff8063dd65afab7cb35eab4d3ef6cb7c3174bebda586e
13
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

(8) 3fdee951549e417093c24877d7578dfd07b6dfbea715d52ed67e61dc088cb2b8
(9) 82c9fb061361f9ad18b0e6f87a7d5cc6e6773af6bc700789646a5f3898b6750f

HD Wallet
==================
Mnemonic: horn convince peasant science build ocean narrow vehicle fragile
glare right crisp
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545

Note: it will run testrpc. Which we are using for our development needs.

10. Now open up "zeppelin-solidity/contracts/crowdsale/Crowdsale.sol" and comment


out line number 44 where it says:

require(_startTime >= now);

Note: we need to do the above modification so that we can set the crowdsale start date to yesterday
while deploying. Otherwise, you have to provide a future date as the crowdsale start date which will be
difficult to test.

The above code keeps our contract ready for deployment with the following settings:
• startTime is yesterday
• endTime is startTime + 20 days
• Current rate is 5 i.e. 1 ETH can buy 5 HTs
• "0x75b67ad7705fbe440c3d6e1b308a581183cea458" is the beneficiary wallet. You should
replace this with the 10th wallet address (usually the last one) from Ganache UI. If you
choose a different wallet from ganache, the last test case will fail! (Can you find out the
reason?)
• Goal is 2 ETH and hard cap is 500 ETH

2. Compile and Deploy the Token Contract


11. Now, go ahead and compile our contract and see what we get. Just type "truffle compile" in the
project’s root.

$ truffle compile

12. Now go to "truffle.js" and paste the following:

module.exports = {
networks: {
development: {
host: "localhost",
port: 7545,

14
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

gas: 6500000,
network_id: "5777"
}
},
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
};

Note: Ganache (installed earlier) is running on port 7545. The above code configures the local
blockchain (Ganache) details and registers with truffle.

13. Now it's time to compile, deploy and test our code.

$ truffle compile

Fig. 2

Next, let’s check out our build folder. Here we can see all the build artifacts of the contracts we used,
from ERC20 to Mintable token and HashnodeCrwodsale. Build artifacts are stored in JSON files,
which contain the name of the contract, Application Binary Interface definition (ABI), unlinked binary
sting, which will be used by Ethereum Virtual Machine, and some other info.

15
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

$ truffle migrate // deploy to local blockchain (Ganache)

$ truffle migrate
Using network 'development'.

Running migration: 1_initial_migration.js


Deploying Migrations...
... 0x2c7168d7ed0c78329f814b34974864b0bd52983b4a1878f84aa6bad198fad3c6
Migrations: 0x775dd2e259eaecdc61484b3cbc403a5ff160e0c4
Saving successful migration to network...
... 0x0454d94cc3c95564896e6d8505da966a133f9fb1bc909e8338408b8e3ae877b1
Saving artifacts...
Running migration: 2_HashnodeCrowdsale.js
Deploying HashnodeCrowdsale...
... 0xeefdc3c7b380d1a7457c97ed9d6ed268855ba13662563fbf1f70427032ed5a53
HashnodeCrowdsale: 0x079bfc42cc846b33a5cb2ecc9fbed6251cecc112
Saving artifacts...

Note: if you happen to look at the testrpc tab you will see that the contract was deployed successfully.

14. You’re done with this section.

Step 4: Test our Contract


15. To test, run the following command:

$ truffle test

If the tests are successful, you should see something like this:

16
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Fig. 3

Note: that "truffle test" command automatically compiles and deploys your contracts before
running the tests.

16. Congrats! You have verified your contract on your local machine.

17. You’re done with this section and this Hands-On lab.

Part 4: Deploy to Ropsten TestNet


Now let's go one step ahead and deploy our code to Ropsten TestNet. In order to do that we need an
ethereum wallet. For the sake of simplicity let's use MetaMask (also my personal favorite). So, go ahead
and download the extension for your browser. Currently, it's available for Chrome, Firefox, Opera and
Brave.

Step 1: Setting up MetaMask


1. To use Ganache with MetaMask, click the MetaMask icon in your browser and this screen will appear:

2. Click Import Existing DEN. In the box marked Wallet Seed, enter the mnemonic that was displayed
when launching Ganache.

Warning: Do not use this mnemonic on the main Ethereum network (mainnet). Make sure that you set
the network to "Private Network" (use the "Custom RPC" setting). See below for details.

17
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Fig. 4

3. Enter the Mnemonic 12 words and the password below that and click OK.

Fig. 5
From testrpc (ganache-cli) we have: MetaMask seed phrase:

Mnemonic: horn convince peasant science build ocean narrow vehicle fragile
glare right crisp

18
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

4. Now we need to connect MetaMask to the blockchain created by Ganache. Click the menu that shows
"Main Network" and select Custom RPC.

Fig. 6

5. In the box titled "New RPC URL" enter http://127.0.0.1:8545 and click Save.

6. The network name at the top will switch to say "Private Network". Click the left-pointing arrow next to
"Settings" to close out of the page and return to the Accounts page.

Fig. 7

19
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Note 1: now that we've connected MetaMask to Ganache, you'll be taken to the accounts screen.
Each account created by Ganache is given 100 ether. The first account should have less than the
others because that account supplies the gas for smart contract deployment. Since you've deployed
your smart contract to the network, this account paid for it.

Note 2: you an now create new accounts. To do so, click the account icon in the upper-right to create
new accounts, the first 10 of which will correspond to the 10 accounts displayed when you launched
Ganache, continue until you can create all the 10 accounts from testrpc.

Fig. 8a

Fig. 8b

7. You’re done with this section.

20
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Step 2: Buying ether


Now that we have connected to Metamask, we can go ahead and buy some test ether.

8. To buy some test Ether click on "Buy" button in MetaMask. It'll take you to a faucet where you can
request some test ETH. We'll need this to pay our transaction fees while deploying the contract.

Fig. 9

Note: from the truffle migration we can note the crowdsale address, which we can use

Fig. 10
Contact address: "0x079bfc42cc846b33a5cb2ecc9fbed6251cecc112"

9. Let’s send 1 Ether from account 4 to Account 1. To do so, follow the Fig. 11 (a) to (e).

21
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Fig. 11

10. Finally, you should observe that there is reduction in the ether that Account 4 holds, and addition in
Account 1, see Fig. 12

22
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org
Global Open Versity Writing and Testing Real world Crowsale Contracts on Ethereum v1.0

Fig. 12

11. Congratulation. You’re done with this section and this lab.

Warning: you should not actually release your ICO with the code found here. It is for demo purposes
only. But it is a good start.

Step 3: Way forward


You could go ahead and test this code on the Ethereum testnet, or you could create a web app that would
allow users to interact with the CrowdsaleToken contract, you could add more functionality to the contract
itself such as deadline, token cap, etc. Unleash your inner token creator.

The Ethereum community grows by the day and though there are not many tools out there yet, the ones
such as Truffle and Zeppelin Solidity do a great job at improving developer’s experience.

A Creative Common Publication:

-----------------------------------------------
Kefa Rabah is the Founder of The Lake Institute. Kefa is knowledgeable in several fields of Science &
Technology, Information Security Compliance, Blockchain Technology, Distributed Ledger Technology
(DLT) and Project Management, and Renewable Energy Systems. He is also the founder of Global Open
Versity, a place to enhance your educating and career goals using the latest innovations and
technologies.

Fellow us on Twitter: The Lake Institute and Kefa Rabah

A Globalopenversity Open Access Technical Academic Publications


Delivering Cutting-edge Technology at your Fingertips in the 21st Century

23
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org

Vous aimerez peut-être aussi