Git Product home page Git Product logo

uups-example's Introduction

Universal Upgradeable Proxies(UUPS)

The main difference between UUPS and Transparent Proxies is that Transparent Proxies needs to have a contract to manage admin and implementation address to avoid selector clash. UUPS moves update implementation address function into implementation contract which saves gas fee on the project side.

Proxy Contract

On proxy contract, it only implements that all function calls will be used delegation call to invoke implementation contract logic. you can see the details by OpenZeppelin Proxy Contract

/**
 * @dev Delegates the current call to `implementation`.
 *
 * This function does not return to its internal call site, it will return directly to the external caller.
 */
function _delegate(address implementation) internal virtual {
    assembly {
        // Copy msg.data. We take full control of memory in this inline assembly
        // block because it will not return to Solidity code. We overwrite the
        // Solidity scratch pad at memory position 0.
        calldatacopy(0, 0, calldatasize())

        // Call the implementation.
        // out and outsize are 0 because we don't know the size yet.
        let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)

        // Copy the returned data.
        returndatacopy(0, 0, returndatasize())

        switch result
        // delegatecall returns 0 on error.
        case 0 {
            revert(0, returndatasize())
        }
        default {
            return(0, returndatasize())
        }
    }
}

Deploy Procedure

Since proxy contract needs to set the implementation address, so we need to deploy implementation contract first. Then passing the implementation address with initalize function as arguments into constructor to deploy proxy contract.

The unit test is written on UUPS.t.sol, you can do the following steps to test on local side

ps. if you don't install foundry, please visit foundry to instal first.

  1. Clone the repo and navigate to the project folder
$ git clone [email protected]:chentihe/uups-example.git
$ cd uups-example
  1. Install the libs
$ forge install
  1. Run unit tests
$ forge test -v

You will see the output like below

Running 3 tests for test/UUPS.t.sol:UUPSTest
[PASS] testProxy() (gas: 14883)
[PASS] testSetProxy() (gas: 20519)
[PASS] testUpgradeTo() (gas: 910177)
Test result: ok. 3 passed; 0 failed; finished in 2.41ms

uups-example's People

Contributors

chentihe 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.