How to design smart contracts

July 6, 2022

5 steps and 13 warnings for making smart contracts!

Image version
Text version

How to design smart contracts.

The article is inspired by @kazsatamai.

Follow him for more web3 design insights.

A smart contract is a program that is stored on a blockchain.

These programs are digital and will execute automatically when certain set conditions and terms are met.

It’s like a digital vending machine.You select a snack and put the agreed-upon money into the vending machine.The vending machine will dispense the snack.

Only you and the vending machine are involved in this transaction.As it’s automated, it is faster and more trustworthy.

5 steps involved in the design of smart contracts:

  • Defining the smart contract
  • Smart contract design
  • Coding the business logic
  • Execution
  • Updating the network

1. Defining the smart contract

The first step is to define the scope of the agreement & establish the terms of the agreement by all stakeholders.
Next, identify all the parties involved in executing the contract and determine the consensus strategy.

2. Smart contract design

Start by determining the events which have the potential to trigger the implementation of the contract.
Think of any inputs that can impact the execution.

Identify the limitation(s) of the underlying blockchain platform.

Define the data elements.
Set conditions of execution based on the business logic.

3. Coding the business logic

A set of codes is developed and tested to represent the expected business contract.
In addition, the secure messaging and authentication protocol is set up between the parties to the contract.

4. Execution

Contract terms dictate the value flow.
Output is stored on a blockchain register which is immutable.

5. Updating the network

Once the contract gets executed, every ledger node gets updated with the newly acquired state.
Any new upgrades cannot be added.

By its design, a smart contract can go from a simple contract (a simple exchange of numerical value) to a very complex contract (a distributed autonomous organization)

Below are 12 design considerations to keep in mind when designing smart contracts

1. Simple and coherent smart contract code

Make sure your contract addresses a single problem.
You can keep the code & functions small by modularizing the code

2. Keep only the necessary data

Analyze and separate application data into on-chain & off-chain data.
Design the state variables so that it is efficient in storing on-chain data, while off-chain data can be handled by high-level applications.
Sensitive information should not be stored in a smart contract.

3. Avoid gas costly patterns (Only for Ethereum)

Every interaction on Ethereum requires users to pay a gas fee.
Look for ways to minimize this during the design so that users and even developers do not consume more gas than necessary.

4. Expect and prepare for failure

Your smart contract code should be able to respond to errors and vulnerabilities.
If errors occur:
- Rules/code to interrupt the contract whenever a deviation is observed.
- limiting how much money the contract manages, thereby limiting risk.
- Provisions for updating, troubleshooting, and improvement.

5. Oracl-ize the smart contract

Oracles help the contract improve itself by receiving external streams from trusted sources.
Be cautious as external contract calls may allow malicious code execution.

6. Design for reentrancy attack

You should avoid external calls whenever possible.
Try to complete all internal work first before calling the external function.

7. Design for denial of service (DoS) attack

Use pull-over push for external calls to prevent a denial of service attack.
Especially when it comes to payment, it is recommended that a pull or withdrawal mechanism be established to obtain funds.

8. Timestamps are imprecise on a blockchain & can be influenced

Do not depend on the timestamp to execute a smart contract.
Especially risky for smart contracts related to transferring funds.
Do not use them for setting a contract's expiry period.

9. Appropriate data types selection

Conduct a review of the type of data before using it in your smart contract.

10. Understand the visibility modifier for data

While there are 4 visibility modifiers on Solidity, anything that is in a contract can be observed on the blockchain, even with the “private” modifier.

11. Pay attention to the order of statements within a function

You need closely observe the functions that include any statements.

12. Use secure hashing for protecting data

Data on the blockchain is visible to anyone, so a secure hash must be used to protect its visibility.
Example: Ethereum uses KECCAK-256

Sources

Community Highlight

Join the Smoothie Newsletter

This is a special newsletter. Every week, we deconstruct the best crypto trends and share those insights with you.

Thanks for subscribing

We will send you an email shortly
Oops! Something went wrong while submitting the form.