Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Blockchain By Example

You're reading from  Blockchain By Example

Product type Book
Published in Nov 2018
Publisher Packt
ISBN-13 9781788475686
Pages 528 pages
Edition 1st Edition
Languages
Concepts
Authors (3):
Bellaj Badr Bellaj Badr
Profile icon Bellaj Badr
Richard Horrocks Richard Horrocks
Profile icon Richard Horrocks
Xun (Brian) Wu Xun (Brian) Wu
Profile icon Xun (Brian) Wu
View More author details

Table of Contents (13) Chapters

Preface Say Hello to Blockchain Building a Bitcoin Payment System Building Your Own Cryptocurrency Peer-to-Peer Auctions in Ethereum Tontine Game with Truffle and Drizzle Blockchain-Based Futures System Blockchains in Business Creating an ICO Distributed Storage IPFS and Swarm Supply Chain on Hyperledger Letter of Credit (LC) Hyperledger Other Books You May Enjoy

Peer-to-Peer Auctions in Ethereum

In the first part of this book, we introduced bitcoin as the first embodiment of blockchain technology and the world's most popular cryptocurrency. Throughout the previous chapters, we tried to illuminate from a developmental angle, what a blockchain is, by building applications using bitcoin. In this second part of the book, we'll introduce the most well-established and mature smart-contract-driven blockchain: Ethereum.

As we discussed in Chapter 1, Say Hello to Blockchain, the primary aim of bitcoin is to establish itself as a payment alternative to regular money, whereas Ethereum focuses more on enabling developers to build and run distributed applications, decentralized apps (DApps), by providing them with built-in tools and currency vehicles.

Faithful to our approach of learning by example, we will start our exploration by building...

Introduction to Ethereum

In its first years, the bitcoin system has proven impressive, but it has narrow capabilities. It seemed that we would keep whittling away at its constraints, but the situation changed after the introduction of the Ethereum blockchain, which, unlike bitcoin, can be extended to much more than just managing a digital currency. In fact, Ethereum is a general-purpose blockchain that is more suited to describing business logic, through advanced scripts, also known as smart contracts. As you saw in previous chapters, bitcoin scripts are primarily about expressing ownership conditions and payment rules. For instance, the standard pay to pubkey hash script describes a small program that allows a sender to send coins to a receiver identified by a public key; no wonder, as bitcoin was designed as a cash system. In contrast, Ethereum was designed with a broader vision...

Building an auction DApp

Now that you have a basic idea of what Ethereum and DApps are, we can start to build our auction DApp. An auction is a typical example, but it is complex enough to provide a perfect first DApp. It demonstrates the trustless nature of the blockchain, in which we can manage funds automatically and securely, without requiring legal recourse or relying on a trusted third party. Moreover, auctions are generally needed for building smart contracts for token sales in ICOs, which is a hot topic that we will cover in Chapter 8, Creating an ICO.

To summarize, our auction DApp will be a web application that enables users to start in auctions using ether. Let's see what we can throw together:

  • First, we will write (in Solidity) and compile the auction contract
  • We will interact with our contract through a web page
  • We will deploy our smart contract on different...

Deployment environment – multiple ways to enjoy!

In Ethereum, we have multiple ways to deploy a smart contract without spending real ether. In this section, we will present how to set up and deploy your contract in the following testing environments:

  • Ganache and Remix
  • Testnet
  • Private network

Option 1 – Ganache

If you're looking for a testing blockchain with a graphical interface, Ganache (previously TestRpc) is for you. It's an in-memory blockchain (think of it as a blockchain simulator) that runs locally.

Download and install it from the official Ganache repository (https://github.com/trufflesuite/ganache/releases) for the appropriate version for your OS.

When you run Ganache, you will get a graphical...

Running the auction DApp

At this level, you should have already deployed your contract on one of the deployment environments presented beforehand. To try what we have built so far, follow these instructions:

  1. In auction.js, define your RPC and port provider: web3.setProvider(new web3.providers.HttpProvider("http://<IP>:<port>"));.
  2. Change the contract's address: var contractAddress = "your contract address";.
  3. Afterwards, open the index.html file directly in your browser or by using a web server. The loaded web page looks as follows:

To start interacting with this DApp, the easiest method is to use MetaMask connected to Ganache. In this case, we'll need to import some of the accounts created by Ganache into MetaMask. To do that, we copy the private key from the Ganache interface and in MetaMask, we import the corresponding accounts by...

Summary

Tired? I really hope not, because this was just the beginning of a long adventure. Hopefully, throughout this chapter, you were able to develop an understanding of the fundamentals of DApps and smart contract development. We covered, in a simplified way, the basics of Solidity and web3.js and how to build and deploy a simple full DApp.

While we have created a working DApp, it's by no means the final word in smart contract security and structural optimization. You'll still need to define functions to transfer ownership and open a bid, and add over- and underflow protection measures, just to name a few. I'll be glad to receive your propositions on the project's repository (https://github.com/bellaj/Car_Auction).

In the next chapter, we will take the learning process a step further, covering new concepts in Ethereum and digging deeper into Solidity&apos...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Blockchain By Example
Published in: Nov 2018 Publisher: Packt ISBN-13: 9781788475686
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}