I want a robust way of updating my UI optimistically for optimal ux.
Solution
Use Tevm to simulate putting the EVM in an optimistic state
Example
Let’s say we are making an onchain game and my contract has a movePlayer(direction) method.
The problem with this code is we will have to both wait for the tx hash to come back in the writeContract and then also wait for the receipt before rendering the new player.
Now we could keep track of state ourselves. But this gets hairy if for example we want to make another move transaction, or we want to call another contract method that might depend on position.
Use Tevm as an optimistic client
We can create a second client using Tevm that can act as our optimistic client.
:::warning [Not production code]
For simplicity of the demonstration we do not handle errors in the above code. We highly recomend using an abstraction like tansatck query, zustand, effect.ts or any other option to help manage the async state here.
:::