A compilation of patterns and best practices for the smart contract programming language Solidity

Franz Volland af9da52f99 Merge pull request #15 from timhaines/patch-1 3 months ago
AccessRestriction b265b36a9f Add disclaimer 3 years ago
ChecksEffectsInteraction 7a67dc65d4 Add disclaimer 3 years ago
EmergencyStop 4b1ad10668 Add disclaimer 3 years ago
EternalStorage 931c06b56b Add disclaimer 3 years ago
GuardCheck de86d4384b Add disclaimer 3 years ago
MemoryArrayBuilding 9fa59ffb14 Fixed minor typo in getItemsbyOwner function header 3 years ago
Oracle aa63e16580 Update oracle.sol 2 years ago
ProxyDelegate e820083cb0 Add disclaimer 3 years ago
PullOverPush 3623206c64 Update PullOverPush.sol 3 years ago
Randomness 15ff103368 Update randomness for 0.4.22 and close bets at setSeed 3 years ago
SecureEtherTransfer 1279eb93ec Add disclaimer 3 years ago
StateMachine 9270cc26b4 Add disclaimer 3 years ago
StringEqualityComparison 4473017387 Add disclaimer 3 years ago
TightVariablePacking cbebe68d81 Add disclaimer 3 years ago
docs 503a0db626 Fix typo. fist -> first. 4 months ago
.gitattributes 755ffdbe7c Create .gitattributes 3 years ago
LICENSE 9172b2ef2a Initial commit 3 years ago
README.md 3b08bb50ea Update README.md 1 year ago


Solidity Patterns

This repository contains a collection of design and programming patterns for the smart contract programming language Solidity in version 0.4.20. Note that newer versions might have changed some of the functionalities. Each pattern consists of a code sample and a detailed explanation, including background, implications and additional information about the patterns.

Have a look at the documentation site: https://fravoll.github.io/solidity-patterns/


  • Behavioral Patterns
    • Guard Check: Ensure that the behavior of a smart contract and its input parameters are as expected.
    • State Machine: Enable a contract to go through different stages with different corresponding functionality exposed.
    • Oracle: Gain access to data stored outside of the blockchain.
    • Randomness: Generate a random number of a predefined interval in the deterministic environment of a blockchain.
  • Security Patterns
    • Access Restriction: Restrict the access to contract functionality according to suitable criteria.
    • Checks Effects Interactions: Reduce the attack surface for malicious contracts trying to hijack control flow after an external call.
    • Secure Ether Transfer: Secure transfer of ether from a contract to another address.
    • Pull over Push: Shift the risk associated with transferring ether to the user.
    • Emergency Stop: Add an option to disable critical contract functionality in case of an emergency.
  • Upgradeability Patterns
    • Proxy Delegate: Introduce the possibility to upgrade smart contracts without breaking any dependencies.
    • Eternal Storage: Keep contract storage after a smart contract upgrade.
  • Economic Patterns
    • String Equality Comparison: Check for the equality of two provided strings in a way that minimizes average gas consumption for a large number of different inputs.
    • Tight Variable Packing: Optimize gas consumption when storing or loading statically-sized variables.
    • Memory Array Building: Aggregate and retrieve data from contract storage in a gas efficient way.


This repository is not under active development anymore and some (if not most) sections might be outdated. There is no liability for any damages caused by the use of one of these patterns.