2 Smart contracts

 

This chapter covers

  • Understanding smart contracts
  • Applying design principles to develop smart contracts
  • Coding smart contracts with the Solidity language
  • Running and transacting with smart contracts by using the Remix IDE
  • Designing, developing, deploying, and testing smart contracts for two use cases

The smart contract is a significant component of the blockchain technology that has been instrumental in transforming a cryptocurrency framework into a trust framework enabling broad range of decentralized applications. This chapter provides details on the concept, design, and development of a smart contract, and also examines the power of executable code on the blockchain.

Structurally, a smart contract is a standalone piece of code similar to a class in an object-oriented program. It is a deployable module of code with data and functions. Functions serve the specific purposes of verification, validation, and enabling recording of the messages sent. A contract in the real world involves rules, conditions, laws, regulations to be enforced, criteria, contingencies, and items for provenance such as dates and signatures. Similarly, the smart contract in a blockchain context implements the contract rules for solving a decentralized problem. It functions as a rules engine as well as a gatekeeper, so understandably, the smart contract design requires careful consideration. Following is an explanation of a smart contract modified to include the code aspects.

2.1 The concept of a smart contract

2.1.1 Bitcoin transactions versus smart contract transactions

2.1.2 What does a smart contract do?

2.2 Design of a smart contract

2.2.1 A use case diagram for the counter

2.2.2 Data assets, peer participants, roles, rules, and transactions

2.2.3 From class diagram to contract diagram

2.3 Development of a smart contract code

2.3.1 Solidity language

2.3.2 Smart contract code for Counter

2.4 Deploying and testing the smart contract

2.4.1 The Remix IDE

2.4.2 Deployment and testing

2.4.3 Key takeaways