Giter VIP home page Giter VIP logo

useful-solidity-patterns's Introduction

useful-solidity-patterns


This repo is an ongoing collection of useful, and occasionally clever, solidity/EVM patterns that actually get used in the wild. These bite-sized guides are written in approachable terms so engineers of all skill levels can understand them. Every guide comes with a concise, self-contained, working code example and tests to demonstrate the pattern. New patterns are added regularly.

The code examples herein are meant to be educational. While the patterns are sound, the examples are not always designed with utmost security or robustness in mind, and sometimes will even forgo best practices in order to best illustrate a concept. They should not be deployed without an independent security review.

  • ABI Decode With Selector
    • Techniques for decoding function call data and revert errors.
  • Advanced Error Handling
    • Write resilient code that intercepts and reacts to errors thrown by other contracts.
  • Assembly Tricks (Part 1)
    • Short, useful assembly tricks to save some gas and make up for solidity shortcomings.
  • Basic Proxies
    • Contracts with upgradeable logic.
  • Big Data Storage (SSTORE2)
    • Cost efficient on-chain storage of multi-word data accessible to contracts.
  • Bitmap Nonces
    • Efficiently tracking on-chain the state of frequent, consumable operations identifiable by a unique nonce.
  • Commit + Reveal
    • A two-step process for performing partially obscured on-chain actions that can't be front or back runned.
  • EIP712 Signed Messages
    • Human-readable off-chain messages that can be consumed on-chain.
  • ERC20 (In)Compatibility
    • Working with both compliant and non-compliant (which are more common than you think) ERC20 tokens.
  • ERC20 (EIP-2612) Permit
    • Perform an ERC20 approve and transfer in a single transaction.
  • eth_call Tricks
    • Perform fast, complex queries of on-chain data and simulations with zero deployment cost using eth_call.
  • Explicit Storage Buckets
    • Safer, guaranteed non-overlapping storage for upgradeable contracts.
  • Externally Owned Account Checks
    • The consequences of interacting with contracts vs regular wallets, and how to identify them.
  • Factory Proofs
    • Proving on-chain that a contract was deployed by a trusted deployer.
  • Flash Loans
    • Designing a basic flash loan mechanism.
  • Initializing Upgradeable Contracts
    • Methods to safely and efficiently initialize state for proxy contracts.
  • Merkle Proofs
    • Storage efficient method of proving membership to a potentially large fixed set.
  • Multicall
    • Allow users to arbitrarily compose and perform multiple operations on your contract in a single transaction.
  • NFT Receive Hooks
    • Use ERC721/ERC1155 transfer callbacks to avoid having users set an allowance in advance.
  • Off-Chain Storage
    • Reduce gas costs tremendously by moving contract state off-chain.
  • OnlyDelegateCall / NoDelegateCall
    • Restrict functions from being called from only within in a delegatecall context or not.
  • Packing Storage
    • Arranging your storage variables to minimize expensive storage access.
  • Permit2
    • Transfer tokens securely without a direct allowance, in a way that works for all (legacy and modern) ERC20s.
  • Read-Only Delegatecall
    • Execute arbitrary delegatecalls in your contract in a read-only manner, without side-effects.
  • Reentrancy
    • Explaining reentrancy vulnerabilities and patterns for addressing them (Checks-Effects-Interactions and reentrancy guards).
  • Separate Allowance Targets
    • Avoid having to migrate user allowances between upgrades with a dedicated approval contract.
  • Stack-Too-Deep Workarounds
    • Clean solutions for getting around and avoiding stack-too-deep errors. So clean that you should do them regardless!
  • Stay tuned for more πŸ˜‰

Installing, Building, Testing

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
# Run forked tests
$> forge test -vvv --fork-url $YOUR_NODE_RPC_URL -m testFork

useful-solidity-patterns's People

Contributors

0x0918 avatar 0xble avatar delaaxe avatar joejordan avatar merklejerk avatar omahs avatar sean329 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

useful-solidity-patterns's Issues

permit2 diagram

Thank you for these excellent descriptions! They are super helpful.

I wanted to mention that the following image for the permit2 system is, for me, a tad hard to parse. I believe that Alice doesn't need to perform an interaction with a protocol contract; anyone with her permit can pass off her message and signature as long as they match to perform permitTransferFrom.

image

I believe a more accurate photo should look something like the following to show that Alice doesn't need to perform any more on-chain interactions herself.

permit2-interaction

Add an example for `PermitBatchTransferFrom`

This has been an extremely useful example (as the name says it) for knowing how Permit2 works.

I believe it'd go full circe if you guys could add a working example for how it would be with PermitBatchTransferFrom.

Permit2: Example for PermitBatchTransferFrom

Hello πŸ‘‹

I've followed your patterns for PermitTransferFrom utilization and I've tried to apply it to the PermitBatchTransferFrom version.
But because the signature for the user, for "batched version" is different from the "original" version, I've modified the helpers functions : _signPermitt() and especially _getEIP712Hash in order to fit to the new version. Attached the modification I've done.
Capture d’écran 2023-01-03 aΜ€ 17 08 35

But I receive this error with I'm testing it : "InvalidSigner()". So I assume that the hash that I'm generating is wrong.
I've you can provide a quick example on how generate the hash for batched permit2 transfer, this could be really useful!

Thanks a lot in advance 😁

Add an example for Check-Effects-Interactions & FREI-PI

Hello! First of all, thank you for creating and maintaining this extremely valuable list of common Solidity patterns, personally that's something I was looking for (for a while πŸ˜„).

Secondly, in my opinion, it will be useful to add the latest Solidity pattern: "Function Requirements-Effects-Interactions + Protocol Invariants", proposed by @brockelmore in the following article.

Thanks in advance for your consideration.

Can i translate it into Chinese

This project is amazing and has been very helpful to me. I'm wondering if I can open another repository and translate it into Chinese. I will definitely credit the source. Or do you have any recommendations on how I can contribute Chinese translation right into to this repository ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.