0

I have been using minimal proxy and I understand how it works. However, I would like to know how it looks like in Solidity as I plan to make some slight modifications to it while wanting to keep it as small as it is and remain safe to use.

Does a minimal proxy solidity code going to a contract with a delegate function? Something like this?

contract MinimalProxy {
  address internal constant impl = 0xabcd;

  // From OpenZeppelin's Proxy contract
  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())
            }
        }
    }

  fallback() external payable virtual {
    _delegate(_impl);
  }
}

When I tried to compile this, it looks larger than what the usual minimal proxy bytecode is. How should a minimal proxy look like in Solidity code?

2 Answers 2

0

Basically there! EIP-1167 had the minimal one, but I don't know if it's achievable with this solidity pattern. Take a look: https://eips.ethereum.org/EIPS/eip-1167

2
  • But the page doesn't have the Solidity version of the minimal proxy, only the bytecode of it. I wanted to see how the bytecodes would look in solidity.
    – xenon
    Mar 23, 2022 at 17:53
  • 1
    Nope, not possible unfortunately, this goes into it a little bit, but you can see the bytecode of the minimal contract (since its created and not deployed) looks really different. blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract
    – thefett
    Mar 23, 2022 at 18:19
0

In Solidity, creating a mini proxy that works involves using the CREATE2 opcode to deploy a new contract that delegates all calls to an existing contract. Below is a simple example of a minimal proxy contract in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MinimalProxy {
    address public target;

    constructor(address _target) {
        target = _target;
    }

    fallback() external payable {
        assembly {
            // Copy the data sent to the proxy to memory
            let ptr := mload(0x40)
            calldatacopy(ptr, 0, calldatasize())

            // Delegatecall to the target contract with the copied calldata
            let result := delegatecall(gas(), sload(target.slot), ptr, calldatasize(), 0, 0)

            // Propagate any returned data
            let size := returndatasize()
            returndatacopy(ptr, 0, size)

            // Revert if the call to the target reverted
            if iszero(result) {
                revert(ptr, size)
            }

            // Return the result of the delegatecall
            return(ptr, size)
        }
    }
}
1
  • 1
    Your latest edits look suspicious. Why are you linking to those sites? What is the significance of the long unique identifier in the URL?
    – tripleee
    Feb 27 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.