mockprovider icon indicating copy to clipboard operation
mockprovider copied to clipboard

Add method to allow for arbitrary execution

Open cleanunicorn opened this issue 3 years ago • 1 comments

Context

Provide a way to allow for arbitrary execution. The execution can be delegated to an external contract with a predefined interface.

Details

The methods can be defined as:

function givenQueryExecute((bytes memory query_, IMockProviderExec exec_) public
function givenSignatureExecute((bytes4 signature_, IMockProviderExec exec_) public

IMockProviderExec can be defined as:

interface IMockProviderExec {
    function execute(bytes args_) external return (bytes return);
}

Alternatives

A contract could inherit the MockProvider and add the methods is requires with the code it could specify as the implementation for IMockProviderExec.

Has the feature been requested before?

No and not sure if it's useful.

cleanunicorn avatar Apr 27 '22 22:04 cleanunicorn

A proxy forwarder inspired by EIP-2535

contract ProxyForward {

    mapping (bytes4 => address) facets; 

    function setFacet(bytes4 signature_, address facet_) public {
        facets[signature_] = facet_;
    }

    fallback() external {

        address facet = facets[msg.sig];

        // Execute external function from facet using delegatecall and return any value.
        assembly {
            // copy function selector and any arguments
            calldatacopy(0, 0, calldatasize())
            // execute function call using the facet
            let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
            // get any return value
            returndatacopy(0, 0, returndatasize())
            // return any return value or error back to the caller
            switch result
            case 0 {revert(0, returndatasize())}
            default {return (0, returndatasize())}
        }
    }
}

cleanunicorn avatar Aug 16 '22 12:08 cleanunicorn