Design Tontine Ethereum DApp Game Interfaces


Design Tontine Ethereum DApp Game Interfaces

Recipe ID: hsts-r43


Recipe Overview

Ethereum is a general-purpose blockchain that is more suited to describing business logic, through advanced scripts, also known as smart contracts. Ethereum was designed with a broader vision, as a decentralized or world computer that attempts to marry the power of the blockchain, as a trust machine, with a Turing-complete contract engine. Although Ethereum borrows many ideas that were initially introduced by bitcoin, there are many divergences between the two.
The Ethereum virtual machine and smart contracts are key elements of Ethereum, and constitute its main attraction. In Ethereum, smart contracts represent a piece of code written in a high-level language (Solidity, LLL, Viper) and stored as bytecode in the blockchain, in order to run reliably in a stack-based virtual machine (Ethereum Virtual Machine), in each node, once invoked. The interactions with smart contract functions happen through transactions on the blockchain network, with their payloads being executed in the Ethereum virtual machine, and the shared blockchain state being updated accordingly.

For those who are not familiar with blockchain technology reading History and Evolution of Blockchain Technology from Bitcoin article is strongly recommended. Also, if you wish to learn and practice Hyperledger blockchain development, visit Comprehensive Hyperledger Training Tutorials page to get the outline of our Hyperledger tutorial articles.
We have written two sets of tutorials to explore Ethereum and Solidity programming in depth. First set covered the following nine recipes:

In short, you learned about how to set up and configure Ethereum and develop blockchain applications using Solidity programming language. We explored its key components, including smart contracts and Web3.JS API via an Auction Decentralized Application (DApp) step-by-step.
In second set, we discuss more advance topics in Ethereum blockchain development and solidity while building a Tontine DApp game step-by-step. Specifically, we cover Truffle and Drizzle. For instance, we show you how a tool such as Truffle can be an assistant in building, testing, debugging, and deploying DApps. In summary, we are going to cover four main topics:

The 2nd set consists of 8 recipes as follows:

IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.
In our first round of recipes, we learned a lot about the Ethereum ecosystem, but we are yet to realize the full potential of its different components. More precisely, we explored how Ethereum works, what a decentralized application (DApp) is and how to build one, and also covered the key concepts of Solidity and web3.js. We then introduced some of the most common smart contract design patterns (withdrawal from a contract, restricting access, state machines), before ending with a discussion of a contract’s cost optimization.
To brush up your knowledge and skills, on second round of recipes, we are going to build a Tontine DApp game. We will exploit this example to explore new tools that are going to change the way you build DApps, and introduce new Solidity features.

In this walkthrough, we will discover how a tool such as Truffle can aid in building, testing, debugging, and deploying our DApp.

As per our design, now we will define an interface for our Tontine game.

Interfaces are universal concepts in programming languages used to represent collections of abstract methods. They are useful since they implement an agreed set of functions that enable interactions without forcing a contract relationship (they might be considered as a protocol).


Nevertheless, in Solidity, unlike abstract contracts, interfaces present the following limitations:

Here is the definition of our Itontine interface:
interface Itontine {
function join() external payable returns (bool); function ping() external returns (bool);
function eliminate(address a) external returns (bool); function claimReward() external returns (bool);
event NewActivePlayerEv(address _address, uint time); event EliminatedPlayerEv(address _address);
}

As you can see, we define an interface in the same way as we do for an abstract contract, the difference being the use of the interface keyword. Let's have a look at what these methods are for:

 

The most important benefit of using interfaces is making the main smart contract upgradeable. Such design can facilitate rolling out future releases of the Tontine game (Ctontine) without introducing breaking changes.
At this point, we need to build out the implementation part.

Interface implementation – Ctontine contract

The next step is to define the main contract behind our game: Ctontine. In Solidity, when a contract implements an interface, the class agrees to implement all of its methods. Hence, if you miss implementing a function defined in the inherited interface, you’ll get the following error (in Remix):
Truffle and Ganache for Ethereum Smart Contract management 2

As we do for inheritance, we use the is keyword to implement the interface, as follows:
contract Ctontine is Itontine {..}

Now, let's fill this empty contract. Within the preceding bracket, we start by declaring the following contract states:

mapping (address => uint256 ) public Tpension; Cplayer.player[] public active_players; Cplayer.player[] public eleminated_players; mapping (address => uint) public ping_time; uint256 public Lindex;
Cplayer Tplayer:

Here are the contract states:

 

We will deal with an instance of the Cplayer contract and Cplayer.player objects to represent the players managed by the aforementioned contract. Therefore, we should find out how to access the remote states and call methods from a remote contract.

 

To conclude this recipe, we like to recommend our Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs, and Become Blockchain Certified Security Architect in 30 hours courses to those interested in pursuing a blockchain development career. This recipe is written by Brian Wu who is our senior Blockchain instructor in Washington DC. His Blockchain By Example book is highly recommended for learning more about blockchain development.

Related Training Courses

Hands-on Node.JS, MongoDB and Express.js Training
Advance JavaScript, jQuery Using JSON and Ajax
Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs
Learn Blockchain Hyperledger Development & Get Certified in 30 Hrs
Become Blockchain Certified Security Architect in 30 hours
Blockchain Certified Solution Architect in 30 hours
Introduction to Python Programming
Object Oriented Programming with UML


Private and Custom Tutoring

We provide private tutoring classes online and offline (at our DC site or your preferred location) with custom curriculum for almost all of our classes for $50 per hour online or $75 per hour in DC. Give us a call or submit our private tutoring registration form to discuss your needs.


View Other Classes!