Giter VIP home page Giter VIP logo

effiproxy's Introduction

EffiProxy

ZH-cn | EN

Overview

EffiProxy is a EIP-1967 standard gas optimization implementation.

Background

In the realm of upgradable contracts, especially for contract wallets, the gas efficiency of a contract plays a pivotal role in user experience. Notable examples include Safe-global::SafeProxy.sol and SoulWallet::SoulWalletProxy.sol.

Key Features

  • โœจGas Efficiency: Compared to Yul versions, EffiProxy reduces deployment cost by over 13k gas, achieving a 15% reduction.

    Note: Writing code that is hard to read is generally not a good practice. However, for contracts like proxy, which have fixed logic and are inherently minimalistic in code, it is indeed possible to improve gas efficiency by writing low-level code without adversely affecting any other components.

    The opcode for EffiProxy is as follows: 60518060225f395f73111111111111111111111111111111111111111160165155f373ffffffffffffffffffffffffffffffffffffffff7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5416365f5f375f5f365f845af43d5f5f3e604d573d5ffd5b3d5ff3

    For an explanation of the above opcode, please see EffiProxy_v2.asm.

    Note: 1111111111111111111111111111111111111111 in the code is a constructor parameter. You will need to replace it with your own specified logic contract address when using it.

    The functionality of the above opcode is equivalent to the following Solidity code:

    pragma solidity ^0.8.20;
    contract YulProxy {
        bytes32 private constant _IMPLEMENTATION_SLOT =
            0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
    
        constructor(address logic) {
            assembly ("memory-safe") {
                sstore(_IMPLEMENTATION_SLOT, logic)
            }
        }
    
        fallback() external payable {
            assembly {
                calldatacopy(0, 0, calldatasize())
                let _singleton := and(
                    sload(_IMPLEMENTATION_SLOT),
                    0xffffffffffffffffffffffffffffffffffffffff
                )
                let success := delegatecall(
                    gas(),
                    _singleton,
                    0,
                    calldatasize(),
                    0,
                    0
                )
                returndatacopy(0, 0, returndatasize())
                if iszero(success) {
                    revert(0, returndatasize())
                }
                return(0, returndatasize())
            }
        }
    }
    

GAS

  • Test
forge test -vv | grep 'deploy'
  • Test result:
Name GAS
Yul version [code] 83754 gas
asm version [code] 70570 gas

We can see that the Asm version reduces gas by more than 15% at deployment time compared to the Yul version.

note: Compared to the Yul version, the Asm version also reduces gas cost during call, but the reduction is very slight (-20 gas per call), so we will not list it here.

Usage

Reference: gasHelper_asm.sol

Note

The Asm is written with the PUSH0 opcode, so it can only be deployed with EVM version >= Shanghai.

effiproxy's People

Contributors

jayden-sudo avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.