46 0 1006KB
Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Global Open Versity
Global Open Versity Blockchain DevOps Hands-on Labs Training Manual
Step-By-Step Guide on Deploying Revenue Sharing Blockchain Smart Contracts using Remix-IDE on Windows Kefa Rabah Global Open Versity, Vancouver Canada [email protected] www.globalopenversity.org
Table of Contents
Page No.
STEP-BY-STEP GUIDE ON DEPLOYING REVENUE SHARING BLOCKCHAIN SMART CONTRACTS USING REMIX-IDE ON WINDOWS
3
Introduction What is Dapps? What qualifies as a Dapp? Picking what to learn: Decentralized Applications
3 3 4 4
Smart Contracts
4
Ethereum: Decentralized Applications Gas and Gas Costs
5 5
Hands-on
6
Part 1: Install Blockchain Required Tools
6
Part 2: Get started with Solidity and Smart contracts Step 1: Installations of Tools Step 2: Install Lightweight http-server Step 3: Install Remix
7 7 7 7
Part 2: How to Code a Dapps Step 1: Using Remix-IDE Step 2: Compile the Contract
9 9 15
Part 3: Deploy the Contract Step 1: A Glimpse inside Run Tag Step 2: Deploy the Contract
17 17 18
Part 4: Interacting with Deployed Contract
22 1
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
Step 1: Check Variables Step 2: Execute shareRevenue() Function
22 23
Part 5: Transaction Log
24
Part 6: Summary
25
Congratulations!
26
A Globalopenversity Open Access Technical Academic Publications Delivering Cutting-edge Technology at your Fingertips in the 21st Century 2 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
Global Open Versity Blockchain DevOps Hands-on Labs Training Manual
Step-By-Step Guide on Deploying Revenue Sharing Blockchain Smart Contracts using Remix-IDE on Windows By Kefa Rabah, [email protected]
April 16, 2018
The Lake Institute
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
Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Global Open Versity
What qualifies as a Dapp? If you look online, the word Dapp is itself is not precisely defined and can often mean different sets of things to different people. However, David Johnston has done work in formalizing the process of what a Dapp is and what its characteristics should be. The two main points that we would like to focus on is: Link: https://github.com/DavidJohnstonCEO/DecentralizedApplications/blob/master/README.md 1. The application’s source code must be open sourced. 2. The application’s data and records must be stored on a public blockchain.
Picking what to learn: Decentralized Applications As with any programming concept or idea, there are a wide range of platforms available to code on when talking about blockchain. While each platform has its pros and cons, it is important to also see how your application is structured so that you use the appropriate services at each of these stages. 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: Table 1: Comparison of web services
Scalable computation File storage External data Monetization Payments
Web 2.0 Amazon EC2 Amazon S3 3rd party APIs Ads, selling goods Credit Cards, PayPal
Web 3.0 (dApps) Ethereum, Truebit IPFS/Filecoin, Storj Oracle (Augur) Token model Ethereum, Bitcoin, state channels, Ox
Source: https://medium.com/@FEhrsam/the-dapp-developer-stack-the-blockchain-industry-barameter8d55ec1c7d4 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: 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 interact write 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.
Gas and Gas Costs The Ethereum blockchain has a concept of Gas and Gas costs that is a very important to understand before you can start coding on it. 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. 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-Buildinga-Blockchain-on-Ubuntu-16-04-Linux-Server 2. https://www.scribd.com/document/373203852/Step-By-Step-Guide-Installing-Ethereum-Buildinga-Blockchain-on-Ubuntu-16-04-Linux-Server 3. https://www.scribd.com/document/374058743/Step-By-Step-Guide-Building-Deploying-a-PrivateBlockchain-Network-on-Windows 4. https://www.scribd.com/document/375953366/Developing-Smart-Contract-on-EthereumBlockchain-Using-Truffle-Framework
Part 1: Install Blockchain Required Tools The tools we will use The most prominent tools at the moments are: •
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.
•
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.
Part 2: Get started with Solidity and Smart contracts 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 2: Install Lightweight http-server This a lightweight server that we’ll use to host your web app locally as you develop on localhost:8080. 2. Type the following command to install the server: npm i -g http-server
3. You’re done with this section.
Step 3: Install Remix 4. Install npm and node.js (see https://docs.npmjs.com/getting-started/installing-node), then do: 5. Remix-ide has been published as an npm module: npm install remix-ide -g
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
Or if you want to clone the github repository: git clone https://github.com/ethereum/remix-ide.git cd remix-ide npm install npm run build && npm run serve 6. Start the Remix IDE remix-ide
Note: Remix IDE requires Apache (http) server to be in place.
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
7. To stop the server, issue ^C command:
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. 8. You’re done with this section
Developing Contract using Remix 9. Run npm start and open http://127.0.0.1:8080 in your browser. 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: 11. You’re done with this section
Part 2: How to Code a Dapps Step 1: Using Remix-IDE The ethereum foundation has released a cloud based IDE called Remix that is a simple way to get started, and which we have now installed and working. 9 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Global Open Versity
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: pragma solidity ^0.4.0; contract Ballot { struct Voter { uint weight; bool voted; uint8 vote; address delegate; } struct Proposal { uint voteCount; } address chairperson; mapping(address => Voter) voters; Proposal[] proposals; /// Create a new ballot with $(_numProposals) different proposals. function Ballot(uint8 _numProposals) public { chairperson = msg.sender; voters[chairperson].weight = 1; proposals.length = _numProposals; } /// Give $(toVoter) the right to vote on this ballot. /// May only be called by $(chairperson). function giveRightToVote(address toVoter) public { if (msg.sender != chairperson || voters[toVoter].voted) return; voters[toVoter].weight = 1; } /// Delegate your vote to the voter $(to). function delegate(address to) public { Voter storage sender = voters[msg.sender]; // assigns reference if (sender.voted) return; while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) to = voters[to].delegate; if (to == msg.sender) return; sender.voted = true;
10 April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada
www.globalopenversity.org
Deploying Revenue Share Smart Contracts using Remix-IDE v1.0
Global Open Versity
sender.delegate = to; Voter storage delegateTo = voters[to]; if (delegateTo.voted) proposals[delegateTo.vote].voteCount += sender.weight; else delegateTo.weight += sender.weight; } /// Give a single vote to proposal $(toProposal). function vote(uint8 toProposal) public { Voter storage sender = voters[msg.sender]; if (sender.voted || toProposal >= proposals.length) return; sender.voted = true; sender.vote = toProposal; proposals[toProposal].voteCount += sender.weight; } function winningProposal() public constant returns (uint8 _winningProposal) { uint256 winningVoteCount = 0; for (uint8 prop = 0; prop < proposals.length; prop++) if (proposals[prop].voteCount > winningVoteCount) { winningVoteCount = proposals[prop].voteCount; _winningProposal = prop; } } }
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
14. Next, fire-up your browser and point it to URL: http://locahost:8080
Fig. 3
The screen is divided into several areas. •
Area for Smart Contract: we are pasting the Solidity code of contract here.
•
Area of Compilation and Runtime: in Compilation tag, here show any compilation errors or warnings. in Run tag, we deploy the contract and execute contract functions.
•
Area of Transaction Logs: all transaction detail can be observed here.
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 15. From the button click on the Detail link
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
16. Which we can now observe as shown in Fig. 5.
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: pragma solidity ^0.4.8; contract RevenueSharing { address public creator; mapping(uint => address) public shareholders; uint public numShareholders; event Disburse(uint _amount, uint _numShareholders); function RevenueSharing(address[] addresses) { creator = msg.sender; numShareholders = addresses.length; for (uint i=0; i< addresses.length; i++) { shareholders[i] = addresses[i]; } } function shareRevenue() payable returns (bool success) uint amount = msg.value / numShareholders; for (uint i=0; i