To my understanding address(addr).code and memory[...] are in different places and since code memory is read-only, a usual call doesn't seem to be able to achieve this, but is there any other way to e.g. execute code passed as function parameter?

function execute(bytes calldata _code) {

I mean theoretically one could implement an EVM bytecode interpreter, but that would be quite an expensive contract...

2 Answers 2


Amplifying @Ismael's answer,you could achieve the effect by deploying contracts with the code to run, and running in a proxy that accepts a contract address and uses delegate call to execute the arbitrary function. There would be a deployment cost for each arbitrary code instance and, of course, the main contract would plainly do literally anything, casting more than a little doubt on any assurances about what it actually does.

Check out "diamond pattern" for inspiration.

Hope it helps.

  • Great, thanks! That sounds better than an on-chain interpreter :D Note to self: Diamond Pattern
    – Zommuter
    May 7, 2023 at 16:57

No, it is not possible.

You could deploy the bytecode as a new contract and then execute it.

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.