This repo is an ongoing collection of useful (and sometimes clever) solidity/EVM patterns, actually used in the wild, ranging from basic to advanced, demonstrated with concise, working examples and tests. I (@merklejerk) will be adding new patterns weekly but contributions are absolutely welcome!
The code examples herein are meant to be educational. Most have not been audited and are not intended to be deployed as-is without an independent security review.
- Advanced Error Handling
- Write resilient code that intercepts and reacts to errors thrown by other contracts.
eth_call
Tricks- Perform fast, complex queries of on-chain data and simulations with zero deployment cost using
eth_call
.
- Perform fast, complex queries of on-chain data and simulations with zero deployment cost using
- ERC20 (In)Compatibility
- Working with both compliant and non-compliant (which are more common than you think) ERC20 tokens.
- Explicit Storage Buckets
- Safer, guaranteed non-overlapping storage for upgradeable contracts.
- Factory Proofs
- Proving on-chain that a contract was deployed by a trusted deployer.
- Merkle Proofs
- Storage efficient method of proving membership to a potentially large fixed set.
- Off-Chain Storage
- Reduce gas costs tremendously by moving contract state off-chain.
- Packing Storage
- Arranging your storage variables to minimize expensive storage access.
- Stay tuned for more 😉
Make sure you have foundry installed and up-to-date first.
# Clone the repo
$> git clone [email protected]:dragonfly-xyz/useful-solidity-patterns.git
# Install foundry dependencies
$> forge install
# Run tests
$> forge test -vvv