Smart Contract Patterns
Adopt best practice patterns.
See example Audit Report from DeFi Safety
Read best practices in solidity
- Memorise the most common code patterns that lead to hacks
- Use established standards from established best practice auditors as much as possible
Common Patterns
Sources for standardised patterns that are proven to be secure?
📄️ Commit Reveal
As data in a blockchain is public, it is impossible to store them secretly.
📄️ Factory Clone
Blockchain Academy
📄️ Payment Splitter
Split payments in a smart contract.
📄️ Reactive Patterns
<iframe
📄️ Require Revert Assert
Best security practices for using Assert, Require and Revert in Solidity.
📄️ Events
Smart Contract Events.
📄️ Gas Optimization
Strategies to Save Gas
📄️ Staking
Ethereum allows a Dev to create a simple set of rules that an adversarial group of players can use to work together.
📄️ Contract Layout
Best practice layout for Solidity contracts.
📄️ Time Lock
Time locks are commonly employed in governance to add a delay to administrative procedures and are widely regarded as a strong indicator that a project is legal and reflects project owners' commitment to the project.
📄️ Upgradable
EIP-2535 Diamond Smart Contracts
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.
Characteristics
Smart Contract Characteristics
- Verifiable
- Trustless
- Self-governing
- Permissionless
- Distributed and robust
- Stateful
- Native built-in payments
Development Flow
Smart Contracts must be very throughly tested and audited before deployment to mainnet.
Re-use standard contracts that have passed the test of time.
- Checks Effects and Interactions
- Do checks to revert first to save gas.
Approach
- Use Scaffold Eth to experiment with proof of concept ideas quickly
- Polish and test Smart Contract business rules with Foundry
- Use Nextjs for Product UI/UX interface to Smart Contract ABIs
A Smart Contract's ABI is the product of compiling Solidity code (in the case of Ethereum).
Process
Basic steps in Ethereum development:
- Write a Contract's business logic in Solidity
- Compile Solidity to produce the ABI
- Load the ABI in a frontend application
- Use libaries to interact with the ABI
Execute Functions
To execute functions on a smart contract, you need to have:
- Address
- Contract ABI (Interaction Blueprint)
- Function (to be called)
- node connection (metamask)
Rules
Contracts exist on a single network, but EVM code can be copied and pasted
Deploying a contract is a one way decision. You can't change the contract after it's deployed. Therefore Security/Trust is vitally important in the blockchain programming.
The best way to pick up good security habits is to read code of Established Ventures that have withstood the test of time and routinely review common hacks.
Transactions
- Cost (Ether Gas) money
- Take time to execute - UX Implications
- Returns a transaction hash
Structure
Anatomy of a Smart Contract:
Stack | Description |
---|---|
Balance | Amount of ether this account owns |
Storage | Data stored on the contract |
Code | Machine Code logic for how to process transactions that change the balance |