Building Games with Ethereum Smart Contracts. Apress

Intermediate Projects for Solidity Developers

Kedar Iyer . Chris Dannen


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 5.00 USD
 Pages
 281 p
 File Size
 3,288 KB
 File Type
 PDF format
 ISBN-13 (electronic)
 ISBN-13 (pbk)
 978-1-4842-3492-1
 978-1-4842-3491-4
 Copyright   
 2018 by Kedar Iyer and Chris Dannen 

About the Author
Kedar Iyer is a software engineer who
runs Emergent Phenomena, a blockchain
consultancy. He is currently writing
blockchain software as a member of the
Everipedia team. He has a bachelor’s degree
in mechanical engineering from UCLA and
has worked in the past with microsatellites,
robotics, and multiple startups.

Chris Dannen is a cofounder and partner at
Iterative Capital, a large-scale cryptocurrency
miner, investment manager, and private digital
asset exchange. A self-taught programmer,
he has written three technical books and
holds one computer hardware patent. He was
formerly the technical editor at Fast Company.
He graduated from the University of Virginia
and lives in New York.

About the Technical Reviewer
Massimo Nardone has more than 23 years
of experience in security, web/mobile
development, and cloud and IT architecture.
His true IT passions are security and Android.
He holds a master of science degree in
computing science from the University of
Salerno, Italy.
He currently works as chief information
security officer (CISO) for Cargotec Oyj and is
a member of ISACA Finland Chapter Board.
Massimo has reviewed more than 40
IT books for various publishing companies and is the coauthor of Pro
Android Games (Apress, 2015).

Acknowledgments
Thank you to Chris Dannen and Solomon Lederer for getting me into
blockchain and introducing me to the NYC blockchain community. To
Nancy Chen and James Markham at Apress for putting together this book,
and Chris for offering me the opportunity to write it. And to my parents
and sister for being supportive of my odd career choices.
—Kedar
Thank you to my team at Iterative Capital for their hard work and support,
and to Kedar for traversing the smoky parlors of Las Vegas to make the
games in this book especially authentic.
—Chris

Table of Contents
About the Authors ................................................................................ix
About the Technical Reviewer .............................................................xi
Acknowledgments .............................................................................xiii
What Is Ethereum? .............................................................................xv
Chapter 1: Conceptual Introduction ......................................................1
Blocks ...................................................................................................................1
Mining ...................................................................................................................2
Transactions ..........................................................................................................3
Ethereum Virtual Machine (EVM) ...........................................................................3
State Tree ..............................................................................................................5
Web3 Explained ....................................................................................................6
What’s New with Ethereum ...................................................................................7
Bitcoin vs. Ethereum .............................................................................................8
Addresses and Keypairs ........................................................................................9
Contracts and External Accounts ........................................................................10
Programs in Ethereum ........................................................................................10
Digging into Solidity ............................................................................................11
Staying Hack-Free ..............................................................................................12
Block Explorers ...................................................................................................13
Useful Smart Contracts .......................................................................................14
Pros and Cons of Ethereum Gaming ...................................................................14
People to Follow ..................................................................................................15
Summary ............................................................................................................17
Chapter 2: The Ethereum Development Environment .........................19
Getting Set Up .....................................................................................................19
Hardware Choices .........................................................................................20
Operating System ..........................................................................................21
Programmer’s Toolkit .....................................................................................23
Ethereum Clients ...........................................................................................26
Deployment ...................................................................................................28
Basic Geth Commands ..................................................................................29
Connecting to the Blockchain .............................................................................33
Network Synchronization ..............................................................................34
Faucets ..........................................................................................................36
Summary ............................................................................................................36
Chapter 3: First Steps with Ethereum .................................................37
Project 3-1: Creating Transactions ......................................................................37
Generating Wallets ........................................................................................37
Obtaining Ether ..............................................................................................38
Sending Fake Ether with the Geth Command Line ........................................40
Project 3-2: Deployment 101 ..............................................................................44
Hello World Contract ......................................................................................44
Manual Deployment .......................................................................................46
Deploying with Truffle ....................................................................................49
Summary ............................................................................................................56
Chapter 4: Smart Contracts in the Abstract .......................................57
Truffle Theory ......................................................................................................57
Configuration .................................................................................................58
Migrations .....................................................................................................60
Development Environment ............................................................................63
Scripting ........................................................................................................64
Tests ..............................................................................................................66
Ethereum Virtual Machine ...................................................................................67
Gas Fees ........................................................................................................67
Solidity Theory ....................................................................................................68
Control Flow ..................................................................................................68
Function Calls in Solidity ...............................................................................69
Contract ABI ...................................................................................................72
Working with Data .........................................................................................73
Contract Structure .........................................................................................80
Logging and Events .......................................................................................82
Operators and Built-in Functions ...................................................................83
Error Handling ...............................................................................................87
Ethereum Protocol .........................................................................................89
Summary ............................................................................................................90
Chapter 5: Contract Security ..............................................................91
All Contract Data Is Public! .................................................................................91
Lost Ether ............................................................................................................95
Addresses ......................................................................................................95
Contracts .......................................................................................................96
Storing Ether in Contracts ...................................................................................97
Sending Ether .....................................................................................................98
Withdraw Methods ............................................................................................103
Calling External Contracts .................................................................................107
Re-entrancy Attack ......................................................................................107
Race Conditions ...........................................................................................111
Suspendable Contracts .....................................................................................111
Random-Number Generation ............................................................................113
Issues with Integers ..........................................................................................115
Underflow/Overflow .....................................................................................115
Truncated Division .......................................................................................118
Functions Are Public by Default ........................................................................119
Use msg.sender Instead of tx.origin .................................................................120
Everything Can Be Front-Run ............................................................................122
Previous Hacks and Attacks ..............................................................................122
The DAO .......................................................................................................123
Parity Multi-Sig ............................................................................................124
Coindash ......................................................................................................126
Governmental ..............................................................................................126
Summary ..........................................................................................................127
Chapter 6: Crypto-economics and Game Theory ..............................129
Securing the Blockchain ...................................................................................129
Proof-of-Work ..............................................................................................130
Proof-of-Stake .............................................................................................131
Proof-of-Authority ........................................................................................132
Forming Consensus ..........................................................................................132
Transaction Fees ...............................................................................................133
Incentives ..........................................................................................................133
Attack Vectors ...................................................................................................134
51 % Attacks ................................................................................................135
Network Spamming .....................................................................................136
Breaking Cryptography ................................................................................137
Replay Attacks .............................................................................................138
Testnet Attacks and Issues ..........................................................................139
Summary ..........................................................................................................140
Chapter 7: Ponzis and Pyramids .......................................................143
Schemes: Ponzi vs. Pyramid .............................................................................143
Verifiably Corrupt ..............................................................................................144
Simple Ponzi .....................................................................................................145
Realistic Ponzi ...................................................................................................150
Simple Pyramid .................................................................................................155
Governmental ....................................................................................................162
Summary ..........................................................................................................170
Chapter 8: Lotteries ..........................................................................171
Random-Number Generation ............................................................................171
Simple Lottery ...................................................................................................172
Recurring Lottery ..............................................................................................176
Constants and Variables ...................................................................................179
Gameplay ..........................................................................................................181
Cleanup and Deployment ..................................................................................186
RNG Lottery .......................................................................................................187
Powerball ..........................................................................................................194
Summary ..........................................................................................................209
Chapter 9: Prize Puzzles ...................................................................211
Obscuring Answers ...........................................................................................211
Simple Puzzle ....................................................................................................212
Commit-Reveal Puzzle ......................................................................................216
Additional Prize Challenges ..............................................................................223
Summary ..........................................................................................................224
Chapter 10: Prediction Markets ........................................................225
Contract Overview .............................................................................................226
Tracking State with Events ...............................................................................233
Trading Shares ..................................................................................................234
Resolving Markets ............................................................................................240
Single Oracle ...............................................................................................240
Multiple Oracle ............................................................................................242
Schelling Point Consensus ..........................................................................243
Summary ..........................................................................................................244
Chapter 11: Gambling .......................................................................245
Gameplay Limitations .......................................................................................245
Satoshi Dice ......................................................................................................245
Roulette .............................................................................................................252
Summary ..........................................................................................................260
References ........................................................................................261
Index .................................................................................................263

Bookscreen
e-book shop

What Is Ethereum?
Ethereum is a trusted compute platform with a native currency built on
top of a decentralized network. A global network of nodes works together
to form a consensus on the state of a shared database.
If Bitcoin offers us a glimpse into the future of money, Ethereum offers
the equivalent for private property, financial assets, legal contracts, supply
chains, and personal data. Any digital unit that can be owned by someone
can be stored in an Ethereum smart contract and transferred between
owners without the need for a third party or middleman such as a bank,
exchange, or central government.

Ethereum works by successively executing a series of transactions,
each of which is a block of code. That code is written in a special language
named Solidity. This is the language we will be exploring in this book.
We will start by getting set up (Chapter 2), deploying simple contracts
(Chapter 3), and going over the basics of the Solidity language (Chapter 4).
Then we will take a brief detour into the theory behind contract security
(Chapter 5) and crypto-economics (Chapter 6) before spending the last
half of the book walking through a series of sample projects (Chapters 7–11).
By the end of this book, you will be comfortable reading and interpreting
existing Solidity contracts and ready to write your own original Solidity code.

Prerequisites
Working with Ethereum and Solidity requires some knowledge
of computer science concepts and prior experience with another
programming language. You don’t need to be an expert, though; just the
basics will do.

Computing Concepts
The best resource for learning the basics of computer science is the
Harvard CS50 lecture series on YouTube (www.youtube.com/user/cs50tv).
It’s a fast-paced, detailed course. If you can make it through all 10 weeks,
by all means do, but the first five lectures will teach you enough to tackle Solidity.

For learning about networking, Linux, or security and hacking,
check out the popular uploads for Eli the Computer Guy on YouTube
His videos are much more beginner-friendly than the CS50
lectures, so if you’re looking for a soft intro to ease you in, this the place to start.
We will be using UNIX (Linux or Mac) command lines throughout
the book. Instructions are given for how to make your Windows system
compatible with our commands, but we recommend learning Linux if you can.

Networking and security are less important concepts to know, and you
can make it through the book and become a Solidity developer without any
prior knowledge of either. Networking is important in the Ethereum protocol
under the hood, but is abstracted away at the application level, where we
will be writing our code. Security is important because the amount of money
passing through our contracts will make them lucrative targets. We spend
an entire chapter discussing contact security (Chapter 5), but any additional
knowledge you can obtain on the topic will serve you well.

Programming
Before diving into Solidity, you should have previous programming
experience with another language. The closest language to Solidity is C,
but it is neither beginner-friendly nor easy to set up. Your best bet for a
simple programming introduction is Codecademy. The simplest language
to learn is Python, and the simplest Codecademy course is Learn Python
JavaScript, while slightly more confusing with syntax, is still easy to
learn and more relevant to Ethereum programming because it is used by
most client software for interacting with the blockchain. We will be writing
and issuing simple JavaScript scripts and commands in this book. The
best resource for JavaScript is the Codecademy Introduction to JavaScript
Loading...
DMCA.com Protection Status