Building Blockchain Projects
eBook - ePub

Building Blockchain Projects

Narayan Prusty

Share book
  1. 266 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Building Blockchain Projects

Narayan Prusty

Book details
Book preview
Table of contents
Citations

About This Book

Develop real-time practical DApps using Ethereum and JavaScriptAbout This Book• Create powerful, end-to-end applications for Blockchain using Ethereum• Write your first program using the Solidity programming language• Change the way you think and design your applications by using the all new database-BlockchainWho This Book Is ForThis book is for JavaScript developers who now want to create tamper-proof data (and transaction) applications using Blockchain and Ethereum. Those who are interested in cryptocurrencies and the logic and database empowering it will find this book extremely useful.What You Will Learn• Walk through the basics of the Blockchain technology• Implement Blockchain's technology and its features, and see what can be achieved using them• Build DApps using Solidity and Web3.js• Understand the geth command and cryptography• Create Ethereum wallets• Explore consortium blockchainIn DetailBlockchain is a decentralized ledger that maintains a continuously growing list of data records that are secured from tampering and revision. Every user is allowed to connect to the network, send new transactions to it, verify transactions, and create new blocks, making it permission-less.This book will teach you what Blockchain is, how it maintains data integrity, and how to create real-world Blockchain projects using Ethereum. With interesting real-world projects, you will learn how to write smart contracts which run exactly as programmed without any chance of fraud, censorship, or third-party interference, and build end-to-end applications for Blockchain.You will learn about concepts such as cryptography in cryptocurrencies, ether security, mining, smart contracts, solidity, and more. You will also learn about web sockets, various API services for Ethereum, and much more.The blockchain is the main technical innovation of bitcoin, where it serves as the public ledger for bitcoin transactions.Style and approachThis is a project-based guide that not only gets you up and running with Blockchain, but also lets you create intuitive real-world applications that will make you an independent Blockchain developer.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Building Blockchain Projects an online PDF/ePUB?
Yes, you can access Building Blockchain Projects by Narayan Prusty in PDF and/or ePUB format, as well as other popular books in Computer Science & Databases. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781787125339
Edition
1

Building Enterprise Level Smart Contracts

Until now, we were using browser Solidity to write and compile Solidity code. And we were testing our contracts using web3.js. We could have also used the Solidity online IDE to test them. This seemed alright as we were only compiling a single small contract and it had very few imports. As you start building large and complicated smart contracts, you will start facing problems with compiling and testing using the current procedure. In this chapter, we will learn about truffle, which makes it easy to build enterprise-level DApps, by building an altcoin. All the crypto-currencies other than bitcoin are called altcoins.
In this chapter, we'll cover the following topics:
  • What the ethereumjs-testrpc node is and how to use it?
  • What are topics of events?
  • Working with contracts using the truffle-contract package.
  • Installing truffle and exploring the truffle command-line tool and configuration file
  • Compiling, deploying, and testing Solidity code using truffle
  • Package management via NPM and EthPM
  • Using the truffle console and writing external scripts
  • Building clients for the DApp using truffle

Exploring ethereumjs-testrpc

ethereumjs-testrpc is a Node.js-based Ethereum node used for testing and development. It simulates full-node behavior and makes the development of Ethereum applications much faster. It also includes all popular RPC functions and features (such as events) and can be run deterministically to make development a breeze.
It's written in JavaScript and is distributed as an npm package. At the time of writing this, the latest version of ethereumjs-testrpc is 3.0.3 and requires at least Node.js version 6.9.1 to run properly.
It holds everything in memory; therefore, whenever the node is restarted, it loses the previous state.

Installation and usage

There are three ways to simulate an Ethereum node using ethereumjs-testrpc. Each of these ways has its own use cases. Let's explore them.

The testrpc command-line application

The testrpc command can be used to simulate an Ethereum node. To install this command-line app, you need to install ethereumjs-testrpc globally:
 npm install -g ethereumjs-testrpc 
Here are the various options that can be provided:
  • -a or --accounts: This specifies the number of accounts to be generated at startup.
  • -b or --blocktime: This specifies the blocktime in seconds for automatic mining. The default is 0, and there's no auto-mining.
  • -d or --deterministic: Whenever the node is run, it will generate 10 deterministic addresses; that is, when you provide this flag, the same set of addresses are generated every time. This option can be used to generate deterministic addresses based on a predefined mnemonic as well.
  • -n or --secure: Locks the available accounts by default. When this option is used without the --unlock option, the HD wallet will not be created.
  • -m or --mnemonic: Uses a specific HD wallet mnemonic to generate initial addresses.
  • -p or --port: The port number to listen on. Defaults to 8545.
  • -h or --hostname: The hostname to listen on. Defaults to Node's server.listen() default.
  • -s or --seed: The arbitrary data to generate the HD wallet mnemonic to be used.
  • -g or --gasPrice: Uses a custom gas price (defaults to 1). If the gas price is not provided while sending the transaction to the node, then this gas price is used.
  • -l or --gasLimit: Uses a custom limit (defaults to 0x47E7C4). If the gas limit is not provided while sending the transaction to node, then this gas limit is used.
  • -f or --fork: This is the fork from another currently running Ethereum node at a given block. The input should be the HTTP location and port of the other client; for example, http://localhost:8545. Optionally, you can specify the block to fork from using an @ sign: http://localhost:8545@1599200.
  • --debug: Outputs VM opcodes for debugging.
  • --account: This option is used to import accounts. It specifies --account=... any number of times, passing arbitrary private keys and their associated balances to generate initial addresses. An testrpc --account="privatekey,balance" [--account="privatekey,balance"] an HD wallet will not be created for you when using --account.
  • -u or --unlock: Specifies --unlock ... any number of times, passing either an address or an account index to unlock specific accounts. When used in conjunction with --secure, --unlock will override the locked state of the specified accounts: testrpc --secure --unlock "0x1234..." --unlock "0xabcd...". You can also specify a number, unlocking accounts by their index: testrpc --secure -u 0 -u 1. This feature can also be used to impersonate accounts and unlock addresses you wouldn't otherwise have access to. When used with the --fork feature, you can use the testrpc to make transactions as any address on the blockchain, which is very useful in testing and dynamic analysis.
  • --networkId: Used to specify a network ID that this node is part of.
Note that private keys are 64 characters long and must be input as a 0x-prefixed hex string. The balance can either be input as an integer or a 0x-prefixed hex value specifying the amount of wei in that account.

Using ethereumjs-testrpc as a web3 provider or as an HTTP server

You can use ethereumjs-testrpc as a web3 provider like this:
 var TestRPC = require("ethereumjs-testrpc"); 
web3.setProvider(TestRPC.provider());
You can use ethereumjs-testrpc as a general HTTP server like this:
 var TestRPC = require("ethereumjs-testrpc"); 
var server = TestRPC.server();
server.listen(port, function(err, blockchain) {});
Both provider() and server() take a single object that allows you to specify the behavior of the ethereumjs-testrpc. This parameter is optional. The available options are as follows:
  • accounts: Value is an array of objects. Each object should have a balance key with a hexadecimal value. The secretKey key can also be specified, which represents the account's private key. If there's no secretKey, the address is autogenerated with the given balance. If specified, the key is used to determine the account's address.
  • debug: Outputs VM opcodes for debugging.
  • logger: Value is an object that implements a log() function.
  • mnemonic: Uses a specific HD wallet mnemonic to generate initial addresses.
  • port: The port number to listen on when running as a server.
  • seed: Arbitrary data to generate the HD wallet mnemonic to be used.
  • total_accounts: The number of accounts to generate at start up.
  • fork: The same as the preceding --fork option.
  • network_id: The same as the --networkId option. Used to specify a network ID that this node is part of.
  • time: The date that the first block should start. Use this feature along with the evm_increaseTime method to test time-dependent code.
  • locked: Specifies whether or not accounts are locked by default.
  • unlocked_accounts: An array of addresses or address indexes specifying which accounts should be unlocked.

Available RPC methods

Here is the list of RPC methods made available with ethereumjs-testrpc:
  • eth_accounts
  • eth_blockNumber
  • eth_call
  • eth_coinbase
  • eth_compileSolidity
  • eth_estimateGas
  • eth_gasPrice
  • eth_getBalance
  • eth_getBlockByNumber
  • eth_getBlockByHash
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getCode (only supports block number "latest")
  • eth_getCompilers
  • eth_getFilterChanges
  • eth_getFilterLogs
  • eth_getLogs
  • eth_getStorageAt
  • eth_getTransactionByHash
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionCount
  • eth_getTransactionReceipt
  • eth_hashrate
  • eth_mining
  • eth_newBlockFilter
  • eth_newFilter (includes log/event filters)
  • eth_sendTransaction
  • eth_sendRawTransaction
  • eth_sign
  • eth_syncing
  • eth_uninstallFilter
  • net_listening
  • net_peerCount
  • net_version
  • miner_start
  • miner_stop
  • rpc_modules
  • web3_clientVersion
  • web3_sha3
There are also special nonstandard methods that aren't included within the original RPC specification:
  • evm_snapshot: Snapshots the state of the blockchain at the current block. Takes no parameters. Returns the integer ID of the snapshot created.
  • evm_revert: Reverts the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot ID to revert to. If no snapshot ID is passed, it will revert to the latest snapshot. Returns true.
  • evm_increaseTime: Jumps forward in time. Takes one parameter, which is the amount of time to increase in seconds. Returns the total time adjustment in seconds.
  • evm_mine: Forces a block to be mined. Takes no parameters. Mines a block independent of whether or not mining is started or stopped.

What are event topics?

Topics are values used for indexing events. You cannot search for events w...

Table of contents