3

Ive seen that the web interface from Uniswap is making a "fake" call to the contract holding the liquidity positions, and claims all fees (.collect(..)) without actually claiming them. Meaning they fake a non static call to be executed without changing the actual blockchain state.

My question now, is this also possible in the EVM by a smart contract itself? In other words, is there a way to make a static call to a function that is not static to only get the desired return value without changing blockchain state?

2 Answers 2

4

By definition of EIP-214 any state change within a static call will cause an error. So if you define your function as view or pure this will not be possible, as this indicates the calling contract that they should use staticcall.

BUT if you want to write a method and don't care about the view or pure you can do a little trick.

You create a method that will perform the call, encode the response (success, data) into a bytes and then revert via assembly with these bytes, therefore reverting the state changes. You can now call this function from another function, catch this error and process the results.

Gnosis has written utils for this: https://github.com/gnosis/util-contracts/blob/main/contracts/storage/StorageAccessible.sol

Hopefully this can be simplified once Solidity allows catching custom errors. As of now you require assembly to do this without wasting too much gas.

1
  • Thanks for your answer, i came across this workaround as well and it seems to be a good option for certain actions, unfortunately not suitable for my cases. I ended up writing certain logic in node and then sending the recieved data back to the contract. Not that perfect because of the datas delay but it works
    – iOSonntag
    Jan 12, 2022 at 18:39
2

From the ethers documentation we can see that contract.callStatic is an utility that interacts with the node itself. It works by asking the node to simulate the transaction, without the tx being actually sent to be mined.

A similar behaviour isn't available at a smart contract level as any state change is permanent if not reverted. A workaround could be re-writing the logic of the necessary method as a view type and returning the results in variables at the end (instead of changing the state).

Your Answer

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

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