{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A Guide to Building Your First Decentralized Application\n", "\n", "\n", "## Demo - We'll build a decentralized voting app. No need to trust anyone if the tallied votes are not modified! \n", "\n", "## But first, what happened to the Web? \n", "\n", "The Web started off 'decentralized'. Everyone who wanted a website bought their own server and hosted their own data on it. But After the Dot-Com bubble, data started to become centralized. \n", "\n", "![alt text](https://imgur.com/a/vNkYw \"Logo Title Text 1\")\n", "\n", "## This is bad. Real bad. \n", "\n", "- It's shrinking the economy\n", "- Too much concentrated power.\n", "- We have no say in how our data is used.\n", "- We should get paid for our data. \n", "\n", "![alt text](https://images-na.ssl-images-amazon.com/images/I/715sBEVwvBL.jpg \"Logo Title Text 1\")\n", "\n", "## Decentralized vs Centralized vs Distributed. What's the difference? \n", "![alt text](https://cdn-images-1.medium.com/max/600/1*nnpzTe1hx74WKICL3Gj34A.jpeg \"Logo Title Text 1\")\n", "\n", "### Centralized \n", "\n", "Centralized systems directly control the operation of the individual units and flow of information from a single center. All individuals are directly dependent on the central power to send and receive information, and to be commanded. \n", "\n", "- single server \n", "- easy to publish \n", "- difficult to scale \n", "- single point of failure\n", "\n", "Examples (Google, Facebook, Amazon, etc.)\n", "\n", "### Distributed \n", "\n", "Distributed systems spread computation across multiple nodes instead of just one. Google for example has adopted a distributed architecture internally to speed up computing and data latency. This means that a system can be both centralized and distributed. \n", "\n", "Examples (Google, Facebook, Amazon, etc.)\n", "\n", "### Decentralized\n", "\n", "Decentralized systems are ones where no node is telling any other node what to do. Bitcoin is both distributed because its timestamped public ledger, the blockchain, resides on multiple computer and decentralized because if one node goes down, the network is still able to operate.\n", "\n", "- Multiple Servers\n", "- Demand and Failures better handled \n", "\n", "Examples (Bitcoin, Ethereum, Steemit)\n", "\n", "## So does a profitable decentralized app look like? \n", "\n", "![alt text](\n", "https://cdn-images-1.medium.com/max/2000/1*AbPxypwGqabLrdu9EBf-iA.png \"Logo Title Text 1\")\n", "\n", "\n", "### Feature 1 - its open source \n", "![alt text](\n", "https://catatansemester.files.wordpress.com/2012/04/closed_source_versus_open_source_small.png \"Logo Title Text 1\")\n", "\n", "If we delve into the traditional business models, all of them require the product or service for sale to be better than that of the competitor. Open sourcing your product would mean that any competitor could take all of your work, white label it, and sell it as their own.\n", "\n", "Having the app be open source allowed the network the transparency it needed to improve itself with developer contributions and grow trust amongst its users to give its coins real world value. Open sourcing your dapp will gain the trust of potential users. Anyone can fork your dapp, but they can’t fork your development team. Users want to get behind the people best suited to maintain the dapp, and those tend to be the original developers.\n", "\n", "### Feature 2: Use of Cryptocurrency\n", "\n", "![alt text](\n", "https://ip.bitcointalk.org/?u=https%3A%2F%2Flookrev.com%2Fimages%2Fdiagram%2Flookrevroadmap.png&t=580&c=6cQkr6aGY6QHqQ\n", " \"Logo Title Text 1\")\n", "\n", "Traditional modes of monetization\n", "- transaction fees\n", "- advertising revenues\n", "- referral commissions\n", "- access rights to user data\n", "- subscription services.\n", "\n", "Cryptocurrency way\n", "- Allocate scarce resources in the network using a scarce token\n", "- Users need this appcoin to use the network. \n", "- Owners of scarce resources get paid in appcoins. \n", "\n", "Example 1 (Bitcoin) - The owners (miners) of the scarce resources (computing power) get paid with transaction fees directly from the users so that they can use the service. Because the network grew to include more users and there were a fixed amount of coins from the output, the values of the coins grew as well. \n", "\n", "Example 2 (FileCoin) - Miners of the scarce resource (storage space) get paid in filecoin. More people using the service to store files, more the value of filecoin rises. \n", "\n", "We can apply this model to any kind of dapps. Scarce resources could be storage space, trades, images, videos, texts, ads, etc. \n", "\n", "\n", "### Feature 3: Decentralized Consensus \n", "\n", "![alt text](\n", "https://gendal.files.wordpress.com/2015/02/smartcontracts4.png\n", " \"Logo Title Text 1\")\n", "\n", "- The Use of a proof algorithm to maintain consensus (like proof of work) \n", "- Distributed Hash Table to store data \n", "- Blockchain for app level constructs (usernames, status updates, high scores, timestamps)\n", "- DHT + Blockchain since blockchain solves the major security issue of DHTs (not forcing nodes to trust each other on the validity of data\n", "- Smart Contracts (cryptoeconomically secured code) \n", "\n", "```javascript\n", "\n", "if (user.sendsMoney(customerID))\n", "{\n", "runContract();\n", "}\n", " \n", "func runContract()\n", "{\n", " println('hello world');\n", "}\n", "```\n", "\n", "### Feature 4: No central point of failure\n", "\n", "Use IPFS\n", "\n", "![alt text](\n", "https://cdn.hackaday.io/images/5211791461026701572.png \"Logo Title Text 1\")\n", "\n", "![alt text](\n", "https://i.ytimg.com/vi/skMTdSEaCtA/maxresdefault.jpg \"Logo Title Text 1\")\n", "\n", "![alt text](\n", "https://blog.resellerclub.com/wp-content/uploads/2015/10/Capture-ipfs.png \"Logo Title Text 1\")\n", "\n", "\n", "## These technologies are ways of building more autonomous software \n", "\n", "![alt text](\n", "https://blog.ethereum.org/wp-content/uploads/2013/12/dao-quadrants.jpg \"Logo Title Text 1\")\n", "\n", "## Where value generation and capital generation are more closely aligned\n", "\n", "# Alright lets get to our voting dapp demo...\n", "\n", "## Step 1 - Setting up Environment\n", "## Step 2 - Creating Voting Smart Contract\n", "## Step 3 - Interacting with the Contract via the Nodejs Console\n", "## Step 4 - Creating GUI interface\n", "\n", "![alt text](\n", "https://cdn-images-1.medium.com/max/1440/1*y7Cdz1uGBGLxZ3ekIE13RA.png \"Logo Title Text 1\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1 - Setting up Environment\n", "\n", "Instead of developing the app against the live Ethereum blockchain, we will use an in-memory blockchain (think of it as a blockchain simulator) called testrpc. \n", "\n", "``npm install ethereumjs-testrpc web3``\n", "\n", "testrpc creates 10 test accounts to play with automatically. These accounts come preloaded with 100 (fake) ethers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2 - Creating Voting Smart Contract\n", "\n", "- We'll use Ethereum's solidity programming language to write our contract \n", "- Our contract (think of contract as a class) is called Voting with a constructor which initializes an array of candidates\n", "- 2 functions, one to return the total votes a candidate has received & another to increment vote count for a candidate.\n", "- Deployed contracts are immutable. If any changes, we just make a new one. \n", "\n", "Let's install the solidity compiler via npm\n", "\n", "``npm install solc``\n", "\n", "After writing our smart contract, we'll use Web3js to deploy our app and interact with it\n", "\n", "```\n", "siraj:~/hello_world_voting$ node\n", "> Web3 = require('web3')\n", "> web3 = new Web3(new Web3.providers.HttpProvider(\"http://localhost:8545\"));\n", "```\n", "\n", "Then ensure Web3js is initalized and can query all accounts on the blockchain\n", "\n", "```\n", "> web3.eth.accounts\n", "```\n", "\n", "Lastly, compile the contract by loading the code from Voting.sol in to a string variable and compiling it\n", "\n", "```\n", "> code = fs.readFileSync('Voting.sol').toString()\n", "> solc = require('solc')\n", "> compiledCode = solc.compile(code)\n", "```\n", "\n", "Deploy the contract!\n", "\n", "- dCode.contracts[‘:Voting’].bytecode: bytecode which will be deployed to the blockchain.\n", "- compiledCode.contracts[‘:Voting’].interface: interface of the contract (called abi) which tells the contract user what methods are available in the contract. \n", "\n", "```\n", "> abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)\n", "> VotingContract = web3.eth.contract(abiDefinition)\n", "> byteCode = compiledCode.contracts[':Voting'].bytecode\n", "> deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})\n", "> deployedContract.address\n", "> contractInstance = VotingContract.at(deployedContract.address)\n", "```\n", "\n", "- deployedContract.address. When you have to interact with your contract, you need this deployed address and abi definition we talked about earlier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3 - Interacting with the Contract via the Nodejs Console\n", "```\n", "> contractInstance.totalVotesFor.call('Rama')\n", "{ [String: '0'] s: 1, e: 0, c: [ 0 ] }\n", "> contractInstance.voteForCandidate('Rama', {from: web3.eth.accounts[0]})\n", "'0xdedc7ae544c3dde74ab5a0b07422c5a51b5240603d31074f5b75c0ebc786bf53'\n", "> contractInstance.voteForCandidate('Rama', {from: web3.eth.accounts[0]})\n", "'0x02c054d238038d68b65d55770fabfca592a5cf6590229ab91bbe7cd72da46de9'\n", "> contractInstance.voteForCandidate('Rama', {from: web3.eth.accounts[0]})\n", "'0x3da069a09577514f2baaa11bc3015a16edf26aad28dffbcd126bde2e71f2b76f'\n", "> contractInstance.totalVotesFor.call('Rama').toLocaleString()\n", "'3'\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4 - Creating GUI interface\n", "\n", "Let's use our HTML + JS client side skills w00t! \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }