万本电子书0元读

万本电子书0元读

顶部广告

Mastering Blockchain Programming with Solidity电子书

售       价:¥

74人正在读 | 0人评论 6.6

作       者:Jitendra Chittoda

出  版  社:Packt Publishing

出版时间:2019-08-02

字       数:61.8万

所属分类: 进口书 > 外文原版书 > 电脑/网络

温馨提示:此类商品不支持退换货,不支持下载打印

为你推荐

  • 读书简介
  • 目录
  • 累计评论(0条)
  • 读书简介
  • 目录
  • 累计评论(0条)
Discover the advanced features of Solidity that will help you write high-quality code and develop secure smart contracts with the latest ERC standards Key Features * Delve into Solidity and understand control structures, function calls, and variable scopes * Explore tools for developing, testing, and debugging your blockchain applications * Learn advanced design patterns and best practices for writing secure smart contracts Book Description Solidity is among the most popular and contract-oriented programming languages used for writing decentralized applications (DApps) on Ethereum blockchain. If you’re looking to perfect your skills in writing professional-grade smart contracts using Solidity, this book can help. You will get started with a detailed introduction to blockchain, smart contracts, and Ethereum, while also gaining useful insights into the Solidity programming language. A dedicated section will then take you through the different Ethereum Request for Comments (ERC) standards, including ERC-20, ERC-223, and ERC-721, and demonstrate how you can choose among these standards while writing smart contracts. As you approach later chapters, you will cover the different smart contracts available for use in libraries such as OpenZeppelin. You’ll also learn to use different open source tools to test, review and improve the quality of your code and make it production-ready. Toward the end of this book, you’ll get to grips with techniques such as adding security to smart contracts, and gain insights into various security considerations. By the end of this book, you will have the skills you need to write secure, production-ready smart contracts in Solidity from scratch for decentralized applications on Ethereum blockchain. What you will learn * Test and debug smart contracts with Truffle, Ganache, Remix, and MetaMask * Gain insights into maintaining code quality with different tools * Get up to speed with ERC standards such as ERC-20 and ERC-721 * Become adept at using design patterns while writing smart contracts * Use MultiSignature (MultiSig) wallets and improve the security of contracts * Use Oracle services to fetch information from outside the blockchain Who this book is for This book is for developers and data scientists who want to learn Ethereum, blockchain, and Solidity to write smart contracts and develop production-ready code. Basic knowledge of Solidity is assumed.
目录展开

Dedication

About Packt

Why subscribe?

Foreword

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Code in action

Conventions used

Get in touch

Reviews

Section 1: Getting Started with Blockchain, Ethereum, and Solidity

Introduction to Blockchain

Understanding blockchain

Blockchain solves the double-spending problem

Properties of blockchain

Distributed ledger

Fault tolerance

Attack resistance

Remove intermediaries

Consensus protocol

Faster settlement

Lower transaction fees

Transparency

Immutability

Irreversible transactions

Trust in the network

Artificial trust

Trustless systems

Availability

Empower individuals

Chronological order of transactions

Timestamped

Sealed with cryptography

When to use blockchain

When not to use blockchain

Blockchain is slow

Blockchain depends on off-chain oracles

Existing implementations of blockchain

Cross-border payments

Decentralized cloud storage

Decentralized computing

Introduction to Ethereum

Ethereum is for writing decentralized applications

Ethereum architecture

P2P networks

Nodes

Full nodes

Lightweight nodes

Miners

Blocks

Ethereum Virtual Machine (EVM)

Ether currency

Smallest unit – wei

Gas

Gas limit

Gas price

Formulas

Example

Ethereum accounts

Externally owned accounts

Contract accounts

The difference between an EOA and a contract

Ethereum transaction

Transaction fields

From

To

Value

Gas limit

Gas price

Nonce

Data

Transaction hash

Transaction status

Pending status

Success status

Fail status

Dropped status

Transaction operations

Replace/update

Cancel

Testnets

Smart contracts

Immutable code

Irreversible transactions

Think twice before deploying

Limited storage

Every transaction consumes gas in ether

Playing with ether or tokens

Summary

Questions

Getting Started with Solidity

Introduction to the Solidity language

The layout of a Solidity source file

Solidity version with pragma

Importing other source files

Structure of a contract

Declaring state variables

Writing function definitions

Creating a custom modifier using function modifiers

Using events for logging and callback

Custom data types with struct

Custom types for constants with enum

Solidity data types

Understanding Solidity value types

Integer value type

Boolean value type

Address value type

Reading a contract's ether balance

Sending ether using transfer

Sending ether using send

Understanding call and delegatecall functions

Understanding the staticcall function

Adjust gas for a transaction using gas

Forwarding ether to another contract

Changes in Solidity version 0.5.0

Fixed size byte arrays

Dynamically sized byte arrays

Understanding the bytes data type

Understanding the string type

Passing functions using function types

Get a function type with function selector

Using internal function types

Using external function types

Solidity reference types

Understanding variables' data locations in Solidity

Using arrays in Solidity

Creating a key value map using mapping

Resetting variables using the delete keyword

Assigning variables with units

Specifying ether amounts using ether units

Supported units for time

Global special variables and functions

Using block and transaction properties

Getting block information using the block variable

Getting sender transaction info using the msg variable

Getting the current time using the now variable

Getting transaction info using the tx variable

Special functions

Application Binary Interface encoding functions

Error handling in Solidity

Cryptographic functions

Contract-related functions

Get the contract address using this keyword

Destroying contracts using the selfdestruct function

Destroying contracts using the suicide function

Topics for self-study

Summary

Questions

Further reading

Control Structures and Contracts

Understanding Solidity control structures

Returning multiple values from function

Expression evaluation order

Solidity contracts

Creating contracts

Creating child contracts using the new keyword

Using variable and function visibility

Getter functions for state variables

Creating custom function modifiers

Creating constant state variables

Understanding types of Solidity functions

Using view functions to read state variables

Using pure functions to perform calculations

Using the default fallback function

Overloading functions

Overriding function definition

Using emit and events for event logging

Inheriting contracts

Passing arguments for the base constructor

Understanding inheritance linearization

Creating abstract contracts

Creating interfaces

Creating custom reusable libraries

Using libraries with – using...for directive

Summary

Questions

Section 2: Deep Dive into Development Tools

Learning MetaMask and Remix

Technical requirements

Using the MetaMask plugin

Installing and setting up the MetaMask plugin

Connecting to different Ethereum networks

Getting test ether from faucets

Other features of the MetaMask plugin

Using the Remix Solidity IDE

The Remix IDE overview

Compiler tools present under the Compile tab

Understanding the Run tab

Selecting the environment to connect with

Choosing different wallet accounts

Transaction parameters

Selecting the contract to use

Using deploy and attach

Deploying a contract

Initiating a transaction to execute the function

Initiating a call to a view function and state variables

Connecting the contract folder using remixd

Setting up a local instance of the Remix IDE

Using the blockchain explorer at etherscan.io

Ethereum wallet software

Using myetherwallet.com

Summary

Questions

Using Ganache and the Truffle Framework

Technical requirements

Local blockchain with Ganache

Starting a local blockchain

Creating workspaces for projects

Ganache advance configuration

The command-line version of Ganache

Understanding Truffle framework

Setting up a Truffle project

Truffle configuration files

Configuring the Solidity compiler

Configuring networks

Choosing which blockchain client to use

Personal blockchain client

Running blockchain nodes

Using Infura

Writing contract migration scripts

Trigger migration using the migrate option

Sample migration script

Using artifacts.require() to get the contract instance

Using module.exports

Deployer

Network considerations

Available accounts

Writing test cases

Writing test cases in JavaScript using Mocha

Writing test cases in Solidity

Debug transactions

Summary

Questions

Taking Advantage of Code Quality Tools

Technical requirements

Using the surya tool

Installing surya

Using surya describe

Generating an inheritance graph

Generating a function call graph

Parsing contracts

Generating function traces

Listing inheritance dependencies

Generating the markdown report

Understanding Solidity linters

Using the solhint linter

Installing the solhint linter

Using solhint

Using the ethlint linter

Installing ethlint

Using solium

The solidity-coverage tool

Installing solidity-coverage

Using solidity-coverage

Summary

Questions

Section 3: Mastering ERC Standards and Libraries

ERC20 Token Standard

Technical requirements

Overview of the ERC20 token standard

Use of ERC20 in crowdfunding

The motivation behind the ERC20 standard

ERC20 standard API

ERC20 implementation

Contract state variables

The balances variable stores account balance

The allowed variable stores approved balances

The totalSupply variable stores the total token supply

The transfer function

Difference between the ether and token transfer functions

Token transfer does not notify the contact

Tokens can be locked

The transfer transaction details

The approve function

Front-running attack on the approve function

Preventing a front-running attack

The transferFrom function

Two-step process for contracts

The allowance function

The balanceOf function

The totalSupply function

Events

The Transfer event

The Approval event

Optional functions

The name function

The symbol function

The decimals function

Advanced functions

The increaseApproval function

The decreaseApproval function

Summary

Questions

ERC721 Non-Fungible Token Standard

Technical requirements

Overview of the ERC721 NFT standard

The ERC721 NFT standard API interface

Understanding the ERC721 implementation

ERC721 inherits from IERC721 and ERC165

ERC721 inherits from ERC165

ERC721 inherits from IERC721

Understanding ERC721 state variables

Token owner mapping kept in _tokenOwner

Approved address mapping kept in _tokenApprovals

The number of tokens per owner kept in _ownedTokensCount

Operator approvals kept in _operatorApprovals

The ERC165 interface code for the ERC721, _INTERFACE_ID_ERC721

The ERC165 interface code for the ERC721Receiver, _ERC721_RECEIVED

The constructor of ERC721

The balanceOf function

The ownerOf function

The approve function

The getApproved function

The setApprovalForAll function

The isApprovedForAll function

The transferFrom function

The safeTransferFrom function

Another safeTransferFrom function

The _exists internal function

The _isApprovedOrOwner internal function

The _mint internal function

The _burn internal function

Another _burn internal function

The _transferFrom internal function

The _checkOnERC721Received internal function

The _clearApproval internal function

Events

The Transfer event

The Approval event

The ApprovalForAll event

The ERC721TokenReceiver interface

The ERC721Metadata interface

The ERC721 enumerable

The ERC721 full implementation

Summary

Questions

Deep Dive into the OpenZeppelin Library

Technical requirements

The OpenZeppelin project

Installation

Usage

Contract ownership

Contract ownership using Ownable.sol

Claimable ownership using Claimable.sol

Roles library

Managing roles using Roles.sol

Manage PauserRole using PauserRole.sol

Other roles

Life cycle contracts

Pause/unpause functions using Pausable.sol

The ERC20 token standard library

ERC20 interface – IERC20.sol

Full ERC20 implementation using ERC20.sol

Perform safe ERC20 function calls using SafeERC20.sol

Create tokens with metadata using DetailedERC20.sol

Create mintable tokens using ERC20Mintable.sol

Allow token burning using ERC20Burnable.sol

Create pausable tokens using ERC20Pausable.sol

Math-related libraries

Aggregation functions using Math.sol

Arithmetic calculation using SafeMath.sol

Crowdsale

Create crowdsale using Crowdsale.sol

Create whitelisted crowdsale using WhitelistCrowdsale.sol

Other crowdsale contracts

Utility contracts

Check for contracts using Address.sol

Prevent reentrancy attacks using ReentrancyGuard.sol

Summary

Questions

Using Multisig Wallets

Technical requirements

Understanding multisig wallets

Benefits of using multisig wallets

Precautions when using multisig wallets

Learning ConsenSys multisig implementation

Setting up your own multisig wallet

Deploying your multisig wallet contract

Sending ETH from a multisig contract

Controlling contracts with multisig

Summary

Questions

Upgradable Contracts Using ZeppelinOS

Technical requirements

Understanding upgradable contracts

Introduction to ZeppelinOS

Creating a ZeppelinOS project

Deploying the StableToken contract

Upgrading the contract

ZeppelinOS commands

Precautions while using ZeppelinOS

Precautions for state variables

Avoid changing the variable declaration order

Avoid changing variable data types

Avoid adding new variables before existing variables

Avoid removing existing state variables

Always add new variables at the end

Precautions when changing variable names

Avoid initial values in the field declaration

Precautions for contract inheritance

Avoid changing the inheritance order

Avoid adding new state variables in base contracts

Summary

Questions

Building Your Own Token

Technical requirements

Features of our token

Contract architecture design

Designing an ERC20 MST token

Designing an MST crowdsale contract

Setting up the Truffle project

Updating configuration

Creating Solidity contracts

The MSTToken.sol contract

The MSTCrowdsale.sol contract

Compiling contracts

Writing a migration script

Running Ganache

Running migration scripts

Writing test cases

Deploying contracts on testnet

Infura APIs

Updating the configuration

Setting up wallet mnemonics

Installing dependencies

Deploying contracts on testnet

Summary

Questions

Section 4: Design Patterns and Best Practices

Solidity Design Patterns

Security patterns

Withdrawal pattern

Applicability

Access restriction pattern

Applicability

Emergency stop pattern

Applicability

Creational patterns

Factory contract pattern

Applicability

Behavioral patterns

State machine pattern

Applicability

Iterable map pattern

Applicability

Indexed map pattern

Applicability

Address list pattern

Applicability

Subscription pattern

Applicability

Gas economic patterns

String equality comparison pattern

Applicability

Tight variable packing pattern

Applicability

Life cycle patterns

Mortal pattern

Applicability

Auto deprecate pattern

Applicability

Summary

Questions

Tips, Tricks, and Security Best Practices

Technical requirements

Smart contracts best practices

Avoiding floating pragma

Avoid sharing a secret on-chain

The commit-and-reveal scheme

Be careful while using loops

Avoid using tx.origin for authorization

Preventing an attack

The timestamp can be manipulated by miners

The 15-second blocktime rule

Carefully making external function calls

Avoid dependency on untrusted external calls

Avoid using delegatecall to untrusted contract code

Rounding errors with division

Using assert(), require(), and revert() properly

Gas consumption

Known attack patterns

Front-running attacks

Example of an ERC20 approve function

Preventing an attack on the approve function

Other front-running attacks

Reentrancy attacks

Preventing a reentrancy attack

Replay attack

Signature replay attacks

Preventing a signature replay attack

Integer overflow and underflow attacks

Ether can be sent forcibly to a contract

Prevention and precaution

Security analysis tools

Using the Securify tool

Summary

Questions

Assessments

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14

Other Books You May Enjoy

Leave a review - let other readers know what you think

累计评论(0条) 0个书友正在讨论这本书 发表评论

发表评论

发表评论,分享你的想法吧!

买过这本书的人还买过

读了这本书的人还在读

回顶部