Reader small image

You're reading from  Blockchain By Example

Product typeBook
Published inNov 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788475686
Edition1st Edition
Languages
Concepts
Right arrow
Authors (3):
Bellaj Badr
Bellaj Badr
author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

Richard Horrocks
Richard Horrocks
author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

View More author details
Right arrow

Blockchain-Based Futures System

Reaching this chapter is a notable accomplishment in your Ethereum learning trip, during which you've thoroughly learned how to write smart contracts with Solidity, and how to build a complete DApp using web3.js and Drizzle. We focused, in the last two chapters, on building a decentralized web application on Ethereum using a set of tools, such as Remix or Truffle. In this walkthrough, we will learn how to harness a powerful language, such as Java, to build an Ethereum desktop or mobile application. Along the way, we will discover new concepts in Solidity, such as calling third-party APIs in your smart contracts or libraries.

As we learn by example in this book, we will break into finance and build a financial project that manages a futures smart contract using Java (SE). Throughout this interesting use case, we will go over the following points...

Project presentation

I know you're a developer and not a financial nerd. Hence, I will try to introduce the futures concept in the easiest terms. Futures contracts (more colloquially, futures) are legal agreements to buy or sell a given quantity of commodity or asset (barrel of oil, gold, and silver) at a predetermined price at a specified time in the future, hence the name futures.

For the sake of ease and clarity, let's consider an oversimplified version of a real futures contract from the financial industry. Let's suppose that an airline company wants to lock in the fuel price to manage their exposure to risk and to avoid any unpleasant surprises. Instead of buying the oil and storing it until they need it, they buy a futures contract for oil with a specific date of delivery and price per gallon. For example, an airline company can agree on a futures contract...

Futures smart contract

As usual, we kick off the chapter by writing the smart contract code. First, create a directory called FuturesWeb3j (in your home folder) and initiate an empty Truffle project using truffle init.

Afterward, in the contracts/ folder, create a Futures.sol file with the following code:

contract CFutures {   
address user;
address hedger;
address speculator;
struct asset{
uint id;
uint Quantity;
uint price;
address seller;
address buyer;
uint date;
}
asset TradedAsset;

event DepositEv(uint amount, address sender);

function CFutures(
uint assetID,
uint Quantity,
uint price,
address buyer,
address seller,
uint date) public {
TradedAsset.id = assetID;
TradedAsset.Quantity = Quantity;
TradedAsset.price = price;
TradedAsset...

Web3j

Web3j is a real treat for Java lovers who want to make their first steps in the new blockchain environment. It represents a lightweight Java and Android API for integration with Ethereum clients. It enables you to build a decentralized Java application easily based on Ethereum. You can find all the details about the project at their official website: https://web3j.io/.

Prerequisites

To get the most out of the following sections, you need to have a basic knowledge of Java. You should also have the following elements installed to build the application:

  • Java 8 JDK
  • Eclipse and Maven for coding and development

Setting...

Java client

As introduced earlier in this chapter, we are building a simple Java application using web3j. Let's understand briefly the mechanics behind this API.

Web3j is the Java port of Ethereum's web3 API, which manages communication (JSON-RPC) between Ethereum clients and the network. For each smart contract, web3j generates a wrapper class, simplifying the access and smart contract interaction with Ethereum. Thus, all complexities will be hidden and you will not be forced to initiate RPC calls on your own. The following diagram (from the official documentation) depicts the interaction flow between web3j and Ethereum:

The wrapper generator

Before using web3j, we need to build the Java wrapper class for our...

Summary

In this chapter, we have scratched the surface of working with the Ethereum blockchain using your beloved language, Java. Indeed, it was a gentle introduction to web3j by building an Oracle-based application, opening the door to build complex Ethereum applications in Java. From now on, all you need is to dive deep into the web3j documentation. You will find a number of smart contract examples in their main project repository (https://github.com/web3j/web3j/tree/master/codegen/src/test/resources/solidity).

To enrich your knowledge, you can also explore EthereumJ, which is a Java implementation of the Ethereum protocol, or Ethereum Harmony. To step outside of the Java world, you can explore Web3py (https://github.com/ethereum/web3.py), which is a complete Python implementation of web3js.

In the next chapter, our Ethereum journey will continue as we examine how to use Ethereum...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Blockchain By Example
Published in: Nov 2018Publisher: PacktISBN-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.
undefined
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

Authors (3)

author image
Bellaj Badr

Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies. Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Read more about Bellaj Badr

author image
Richard Horrocks

Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher. The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering. When not in front of a computer, he enjoys yoga and falling off motorbikes.
Read more about Richard Horrocks

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu