Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction 3
What is Dapps? 3
What qualifies as a Dapp? 4
Picking what to learn: Decentralized Applications 4
Smart Contracts 4
Hands-on 6
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Part 6: Summary 25
Congratulations! 26
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Introduction
In this Hands-On guide you will learn how to get started learning on how to code a Decentralized
Applications or Dapps and what are the things that you must cover when making one. It’s important to
keep in mind that this field is constantly evolving and that it is always a good idea to keep refreshing your
knowledge about whatever you learn.
Blockchain has been around for about a decade now. Blockchain technologies and how it works, is the
next big thing to shape our lives and our futures. Blockchain lets us for the first time to have a shared
version of truth that is open, distributed and tamper proof in its nature. Blockchain has these properties
because at the heart of it, it has a way to derive consensus (thereby having a shared version of truth) and
also has cryptography baked into it by design (thereby letting us have a tamper proof record), and more
importantly it’s decentralized – i.e. it’s not controlled by any central authority or government.
Blockchain has a wide ranging set of applications and almost any field can benefit from having such a
technology at its disposal, as you must by now have learned from our Part 1 of this book. Blockchain is
also heralded as a way by which trust can be ensured in the online world and lets us have greater
confidence in the range of products and services that we use online.
However, it isn’t quite enough to know about the properties of blockchain and how it will impact the world
around us. To get a thorough understanding of the blockchain it is essential to know how to interact with it
programmatically and thinking about solutions from the software development aspect that we are used to.
Developing applications on the blockchain is certainly something that is new and cutting edge – in fact,
there is a new word to describe the type of applications that are powered by the blockchain. These are
called Decentralized Applications or Dapps.
What is Dapps?
Dapps are a new way about thinking about how we can go about writing applications for the internet that
powers blockchain technologies and applications. While previously we would have the backend code
residing in a server or a set of servers, Dapps let us run web enabled applications where the backend is
hosted and that is on a blockchain network, which executes the code that is needed for it.
3
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
As we are familiar with modern computing, it is important to have all the required components of a tech
stack with their counter parts in the decentralized world. However, it is not trivial to find out and do
research on each of the components. Thankfully, there is this handy info graphic that we can use to get a
sense of the ecosystem:
As we can observe, we have solutions for computing, file storage, API calls (external data), monetization
and payments. In this guide we are mainly going to be focused on ethereum and how we can tackle the
computing aspect of Dapps. Since computing is one of the most important aspects of building dapps, it
makes sense to get familiar with interacting with smart contracts and then proceeding further.
Smart Contracts
The term Smart contracts was coined by Nick Szabo in 1994. Smart contracts and blockchain are closely
linked – smart contracts are the programs that we write on the blockchain and let us interact with it. It is in
a smart contract that we can define business logic and let us code the rules with which we want to interact
with the blockchain. The most powerful feature of a smart contract is the fact that once it is deployed on to
the blockchain, it is immutable and you cannot go back and edit the programs. This makes us embrace a
paradigm where we have a piece of code with the logic that we want in it will be immutable and can be
4
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
execute by itself when it is called. It is smart contracts that truly unleash the power of the blockchain, and
hence, it is important to learn to develop smart contracts on the blockchain.
Ethereum has a public blockchain that lets people run code on and it one of the simplest way to start
programming on the blockchain.
Gas is amount of fuel that you need to pay to get your transaction executed. The transaction could either
be deploying your contract on the blockchain or can be running a function that already exists. Every time a
function is called, there is some code that is executed. This code is executed on the computer of the
person who is mining the transaction and requires computation power – to incentivize people to share
their computational power and execute the transaction; each operation is charged some gas depending
on the complexity of the transaction. The Gas in an ethereum smart contract can range all the way to 21
million. The gas that your transaction needs is multiplied by the gas cost to get the gas price.
5
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Gas price is paid in ether, and hence, it is important to make sure that you have the correct balance in
your account to deploy the contract on the blockchain. This is why instead of developing on the main
network (which costs ether), we will run our code on a local instance of the ethereum virtual machine.
Another interesting facet of this is that while coding, you would want to structure your program in a way
that reduces your overall gas cost.
Hands-on
In this Hands-On guide you will learn how to get started learning on how to code a Decentralized
Applications or Dapps and what are the things that you must cover when making one. It’s important to
keep in mind that this field is constantly evolving and that it is always a good idea to keep refreshing your
knowledge about whatever you learn.
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
• Mist: It’s a browser for decentralized web apps. It seeks to be the equivalent of Chrome or Firefox, but
for Dapps. It’s still insecure and you shouldn’t use it with untrusted dapps as of yet.
6
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
• Ethereum wallet: It’s a version of Mist, but only opens one single dapp, the Ethereum Wallet. Mist and
Ethereum Wallet are just UI fronts. And we need a core that will connect us to an Ethereum
blockchain(It could be the real Ethereum blockchain, or a test one).
• Geth: Is the core application on your computer that will connect you to a blockchain. It can also start a
new one (in our case we will create a local test blockchain), create contract, mine ether etc.
npm i -g http-server
7
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
remix-ide
Note: the "ballot.sol" is a default file that come with Remix IDE. So go ahead and overwrite it.
8
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Remix is a suite of tools to interact with the Ethereum blockchain in order to debug transactions
(directly quoted from here). There is an IDE version (Remix IDE) and an online version, which we will
use here.
There are many tools inside Remix, but the following tools are of our interests,
• Solidity Compiler. which generates a lot of useful information that we will use in another
environment
• Runtime Environment. Remix provides three:
o Injected Web3: to provider such as Mist or MetaMask
o Web3 Provider: to local host through ipc
o JavaScript VM: a simulated environment
Among the runtime environments, we are using JavaScript VM. In the JavaScript VM, Remix comes
with five Ethereum accounts, each of which is deposited with 100 ethers. This is good enough for
testing our smart contract. Also, mining is not required as it is done automatically.
10. Then open your text editor and start developing. The browser will automatically refresh when files
are saved.
Note: most of the time working with other modules (like debugger etc.) hosted in the Remix repository
is not needed. But in case changes have to be made in that repository too, the following command will
help you link Remix with your local remix-ide repository:
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
The simplest thing to do in any programming language is declare and write variables so let’s see an
example of how to do that is to simple use the existing default Solidity file Ballot.sol., which have
copied and pasted here or brevity:
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
12. From the code, there are two functions – one that lets us set the variable value and one that lets us
read it.
• The first line of code tells us that we using a particular version of Solidity.
• The contract SimpleStorage tells us the name of the contract and it helps us group code and
logic into a single unit that can be referenced directly.
• The next line of the code declares a variable – storeData is of type unit.
Note: the next two functions are simply getter and setter functions of the variable storeData.
13. On Windows, from Start Run CMD, you can start the Remix IDE, as follows:
remix-ide
11
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Fig. 3
Note: let’s take a minute to see what happens when we click on the Create button. Every time you
click on create, you are taking your Solidity based smart contract and deploying it on the blockchain.
However, to simplify things and make it faster to develop applications, the blockchain that it deploys to
is the ethereum virtual machine and resides in your browser and lets you interact with it. Since code
once deployed cannot be changed, each time you have to make a change to your code, you have to
redeploy it and test it out again
12
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Fig. 4
Fig. 5
Note 1: the console output that you see when you create the smart contract.
13
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Note 2: all you need to do is to deploy to the mainnet blockchain (the actual ethereum blockchain) is
change the environment in the run tab and you should be able to write to the blockchain – keep in
mind that interacting with the ethereum main-net will actually cost you money and every time you call
a function or deploy a new contract, you have pay a gas cost in ethereum to make sure that your
contract or function call gets mined and that you get your output.
Note 3: once you are up and running with Ethereum, you can read up the Solidity documentation to
get yourself familiarized with the platform and its syntax, and it will form the core task on our next
Hands-On Lab. Solidity is fairly easy to learn and is very similar to javascript in its syntax. Let’s have a
look at another Solidity example code to give a better feeling of what programming on it looks like.
This one lets us conduct an open auction in a fair manner:
contract RevenueSharing {
address public creator;
mapping(uint => address) public shareholders;
uint public numShareholders;
function kill() {
if (msg.sender == creator) selfdestruct(creator);
}
}
• Function RevenueSharing() has the same name of the contract itself. It is the
constructor, and only called once when the contract is deployed. We see in this contract an
array of addresses is supplied, and this array of addresses are stored in another array called
shareholders.
14
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
• Function shareRevenue() is the only main function in this contact. When executing this
function with an amount of ethers (in msg.value), the amount is divided into the number of
shareholders (numShareholders), and each address in the shareholder array will get the
portion. We will execute this function in our demo.
• Function kill() is used for removing the contract. We will not use this function in the
demo.
Note: that all variables are defined with public. This helps us to observe more detail in the contract.
In real life, we should be careful when making variables or functions public due to security
consideration.
Fig. 6
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
The we’re noticing in Fig. 6a, at time comes about because of wrong Remix version, if we go to Setting
button and choose, the correct version here solidity ^0.4.10; then as can be observed, all the errors are
longer there! Also ensure to check Enable Optimization, while there, Fig. 6b.
Fig. 6a
Fig. 6b
Note:
If we click Details link, we will see a lot of information for this contract. Among them they are,
• Bytecode
• ABI
16
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
• Web3 Deploy
They are needed when deploying this contract in another environment. We will refer this back later.
Fig. 7
Fig. 8
After selecting JavaScript VM, we will see some accounts are populated in Account fields.
17
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Fig. 9
And as said, each of which are pre-deposited 100 ethers, just for testing. As we will use these accounts
later, we can copy them first.
Fig. 10
The Gas limit is to specify how much gas we can spend on any transaction. As we are in test
environment, we do not worry this too much. I have tried some big contract deployment and the default
gas limit is not adequate. Anyway, it can be increased to whatever value when needed.
The Value part is where we send the amount of ethers during contract deployment and executing a
function. In our case, we do not put any value in contract deployment, but put some ethers when
executing the function. See below for more detail.
Fig. 11
4. Something is required, as hinted in the input area: “address[] addresses”, when the contract is
deployed. Remember this contract requires a list of addresses as sharing targets? For demo purpose,
we will use the 3rd, 4th and 5th addresses listed above as the address list. So paste this beside the
Create button:
["0x4b0897b0513fdc7c541b6d9d7e929c4e5364d2db",
"0x583031d1113ad414f02576bd6afabfb302140225","0xdd870fa1b7c4700f2bd7f44238821c26f73921
48"]
18
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Fig. 12
6. Click on “Create” under your “Run” tab. You should now see your contract created just like below.
Fig. 13
19
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Note: the amount of ether on our account ticked down slightly from 100 after creating the contract.
7. Right off the bat you can see the 3 functions that we had created — “kill”, “shareRevenue”, and
“creator”, and “numShareholders”, “shareholders”. You can also see that our state variable
has a value assigned to it of Uint256.
Fig. 14
8. Now, click “shareholders” which display the changes as shown. Then go ahead and click
“numshareholders” to call our newly modified content displayed.
Fig. 15
20
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Fig. 16
The contract is now deployed in JavaScript VM (memory), and the contract address is shown (0x692…).
We do not use this address in our demo. That address can be referred in other cases when needed.
Also, we see the variables marked as “public” is now shown, they are,
• shareholders
• numShareholders
• creator
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
• kill()
Before everything, we observe that the account balance is reduced by a small amount of ethers. The
difference (417,626 weis, 1 wei = 10-18 ether) is the cost of deploying this contract. In real life, it is the
real ethers deducted from your account when you deploy a contract.
Fig. 17
Fig. 18
22
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
We use the same place to execute the function. Here we make sure to,
• in Account field, choose the first account (begins with 0xca3…)
(0xca35b7d915458ef540ade6068dfe2f44e8fa733c)
• place 30 ethers in the Value
Fig. 19
23
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
6. Then press shareRevenue. After the function is executed, we examine the balance of each
accounts, and see if it is executed according to our design.
First, we see 30 ethers are deducted from 1st account, and all the three accounts on the list now have
110 ethers. So the 30 ethers deducted from 1st account is now distributed among the three accounts.
This part works perfects according to the contract.
Also, if we closely examine the balance of 1st account, some additional amount of ethers is deducted.
The difference is 47,776 wei, which is the cost for this transaction. Every transaction, execution of
function, or deployment of contract costs you some amount of ethers.
Fig. 20
24
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
We can see who has deployed this contract, the contract address, and the transaction cost required to
deploy it.
Note: again we see it as a transaction cost. In shareRevenue(), there is a return boolean value and
we see the “decoded out” there is a “true” return. Also, we have an event for successful distribution
and we see it inside “logs”.
Part 6: Summary
This is how Remix helps in testing the code we develop. It comes with very handy features and intuitive
user interface. In next Hands-On Labs we will use another environment, testrpc (or ganache-cli), to
work on the same contract and see how it works.
25
April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Global Open Versity Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Congratulations!
Congrats on finishing the tutorial on Revenue Sharing and having deployed and interacting with the smart
contract! This is your first time to learn on how Remix helps in testing the code that you have developed. It
comes with very handy features and intuitive user interface. Again, lots of details and tools, but hope that
some of these made sense, and gave you a little more clarity on how blockchain smart contract work.
Plenty more to do and learn now that you know the basics.
-----------------------------------------------
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.
www.globalopenversity.org