ebook img

Ethereum Smart Contract Development in Solidity PDF

337 Pages·2021·7.645 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ethereum Smart Contract Development in Solidity

Gavin Zheng Longxiang Gao Liqun Huang Jian Guan Ethereum Smart Contract Development in Solidity Ethereum Smart Contract Development in Solidity (cid:129) (cid:129) (cid:129) Gavin Zheng Longxiang Gao Liqun Huang Jian Guan Ethereum Smart Contract Development in Solidity GavinZheng LongxiangGao NenglianTechnologyCo.Ltd. SchoolofInformationTechnology Beijing,China DeakinUniversity Burwood,VIC,Australia LiqunHuang JianGuan HuazhongUniversityofScience MuhuaTechnologyCo.Ltd. andTechnology Beijing,China Wuhan,Hubei,China ISBN978-981-15-6217-4 ISBN978-981-15-6218-1 (eBook) https://doi.org/10.1007/978-981-15-6218-1 ©SpringerNatureSingaporePteLtd.2021 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartofthe materialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation, broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionorinformation storageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilarmethodology nowknownorhereafterdeveloped. Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant protectivelawsandregulationsandthereforefreeforgeneraluse. The publisher, the authors, and the editorsare safeto assume that the adviceand informationin this bookarebelievedtobetrueandaccurateatthedateofpublication.Neitherthepublishernortheauthorsor theeditorsgiveawarranty,expressedorimplied,withrespecttothematerialcontainedhereinorforany errorsoromissionsthatmayhavebeenmade.Thepublisherremainsneutralwithregardtojurisdictional claimsinpublishedmapsandinstitutionalaffiliations. ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSingaporePteLtd. The registered company address is: 152 Beach Road, #21-01/04 Gateway East, Singapore 189721, Singapore Preface Smart contract isoneofthecornerstonesofblockchain technology. Amongallthe smart contract programming languages in market (such as Viper, Bamboo, etc.), Solidity running on Ethereum Virtual Machine (EVM) is the most popular one in termsofnumberofusers,developercommunity,scopeofuse,numberofcontracts in use, and the public recognition. This book introduces the Solidity programming languagefromscratchandexplainsthecorefeaturesofSolidityindetail. About the Book Structure Thebookisorganizedinanorderlywayasbelow: PartI:Preliminary Chapter1:ConceptsandtermsofEthereum Chapter2:ConfigurationandinstallationofSoliditydevelopmentenvironment PartII:SolidityBasics Chapter3:BasicsofSolidity:keywords,statements,modifiers,etc. Chapter4:PopularEthereumRequestforComments(ERC)protocols PartIII:SolidityAdvancedTopics Chapter5:ABIspecificationandcoding Chapter 6: Advanced topics of Solidity programming: design pattern, GAS, assembly Chapter7:Upgradeablesmartcontractdesignandimplementation Chapter8:SecurityofSolidityprogrammingandbestpractice PartIV:Application Chapter9:DecentralizedApplication(DApp)programmingtechnique Chapter10:Testinganddebugging PartV:Prospect Chapter 11: Primer of Web Assembly programming which is believed to be the future v vi Preface InPartI,Chap.1introducesallthebasicconceptsandtermsofEthereumusedin the book for understanding of subsequent content; you can skip it if you already know them well. And Chap. 2 tells you how to set up Solidity development and testing environment. After all the preparation work being done, Part II guides you throughSoliditylanguagedetailsandcodingsimplecontracts.Todevelopcomplex contracts, Part III collects all the information you need: interface, design pattern, GAS, assembly, upgrade, security, and best practices. In Part IV, the authors shed some light on developing DApp which showcases how to interact with Solidity contract.Atlast,thebookalsoexploresWASMalittlebit,whichisthoughttobea futurestarinsmartcontractprogramming. Beijing,China GavinZheng Burwood,VIC,Australia LongxiangGao Wuhan,Hubei,China LiqunHuang Beijing,China JianGuan April,2020 Intended Audience Thebookisintendedforreaderswithpriorexperienceofusingatleastoneobject- oriented programming language (e.g., C++, Java, etc.). If you have a solid under- standing of object-oriented concepts, such as inheritance, polymorphism, etc., and you desire to jump into the blockchain industry and ramp up on smart contract developmentonEthereumplatform,thisbookisthebestfitforyou.Steppingfrom thisbook,thereadersshoulddivedeepintoEthereumVirtualMachine(EVM)and WebAssembly(WASM)forcontinuingstudy. vii Acknowledgement I would like to thank the co-authors: Dr. Jian Guan—CTO of xuetangx.com, Dr. Longxiang Gao at Deakin University, Geelong, VIC, Australia, and Associate Professor Liqun Huang at Huazhong University of Science and Technology (HUST), Wuhan, PRC, for their valuable efforts and contributions in writing this book. Besides, I would like to thank the responsible editor of this book—Dr. Wei Zhuwhoorganizestoturntheinitiativeintoreality. ix Contents PartI Preliminary 1 BasicConcepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 Ethereum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.1 AsynchronizedCryptography. . . . . . . . . . . . . . . . . . . 4 1.1.2 CryptographicHashFunction. . . . . . . . . . . . . . . . . . . 7 1.1.3 Peer-to-PeerNetwork. . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.4 Blockchain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.5 EthereumVirtualMachine(EVM). . . . . . . . . . . . . . . . 9 1.1.6 Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.7 Miner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.8 ProofofWork(PoW). . . . . . . . . . . . . . . . . . . . . . . . . 10 1.1.9 DecentralizedApp(DApp). . . . . . . . . . . . . . . . . . . . . 10 1.1.10 Solidity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2 SmartContract. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3 GAS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3.1 WhyGAS?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.3.2 ComponentsofGAS. . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4 Ether(ETH). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.5 Account. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6 Transaction. .. . . . . . . .. . . . . . . . .. . . . . . . . .. . . . . . . .. . . 15 2 Preparation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.1 ASimpleExample. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2 ToolPreparation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2.1 DevelopmentEnvironment. . . . . . . . . . . . . . . . . . . . . 18 2.2.2 DevelopmentTools. . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2.3 BlockchainExplorer. . . . . . . . . . . . . . . . . . . . . . . . . . 38 xi xii Contents 2.3 TestingEnvironment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.3.1 MetamaskSwitchingBetweenTestingEnvironments. . 41 2.3.2 ObtainTestingCoins. . . . . . . . . . . . . . . . . . . . . . . . . 41 2.3.3 ConnecttoTestingEnvironment. . . . . . . . . . . . . . . . . 41 2.4 EthereumSourceCodeCompilation. . . . . . . . . . . . . . . . . . . . . 44 PartII SolidityBasics 3 SolidityBasics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.1 SolFileStructure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.1.1 Pragma. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.1.2 Import. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.1.3 Comment. . . . . . .. . . . . . . . .. . . . . . . .. . . . . . . .. . 50 3.1.4 Contract. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.1.5 Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.1.6 Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.2 StructureofContract. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.3 Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.3.1 ValueType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.3.2 ReferenceType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.3.3 Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.3.4 SpecialCase. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.4 Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.5 Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.5.1 ConditionalStatement. . . . . . . . . . . . . . . . . . . . . . . . 60 3.5.2 Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.5.3 Miscellaneous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.6 DataLocation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.7 Modifier. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.7.1 StandardModifier. . . . . . . . . . . . . . . . . . . . . . . . . . . 64 3.7.2 Self-definedModifier. . . . . . . . . . . . . . . . . . . . . . . . . 68 3.8 Event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 3.8.1 ReturnValuetoUI. . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.8.2 AsyncTriggerwithData. . . . . . .. . . . . . . . . . . . . .. . 72 3.8.3 CheapStorage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 3.8.4 IndexedParameterinEvent. . . . . . . . . . . . . . . . . . . . 74 3.9 Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.9.1 SingleInheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.9.2 Multi-Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.10 Miscellaneous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.10.1 Built-inVariable. . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.10.2 SpecialUnit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 3.10.3 TypeCastandInference. . . . . . . . . . . . . . . . . . . . . . . 80 3.10.4 Exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.10.5 Assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.