3

I want to be able to see the state changes that would result from running a transaction without submitting the transaction to the network.

Is there a straightforward way to do this with geth?

5

Yes, there is.

You can use eth_call RPC method

eth_call will dry run a transaction and will return result if the function has return

uint public x = 0;
function writeSmth() public returns uint {
    x = 2;
    return x;
}

then using JS you can make the following:

async function example() {

  const instance = new web3.eth.Contract(ABI, address);
  const futureX = await instance.methods.writeSmth().call()
  let currentX = await instance.methods.x().call()
  console.log(futureX) // futureX = 2
  console.log(currentX) // currentX = 0
  await instance.methods.x().send()
  currentX = await instance.methods.x().call()
  console.log(currentX) // currentX = 2
}
example()
| improve this answer | |
  • cool! didn't realize eth_call dry-runs txes. I just assumed it only worked for read methods only. Will give this a shot – lsankar4033 Aug 1 '19 at 18:07
  • nvm, this isn't actually what I want. I'm hoping to be able to simulate tx-creating methods that don't necessarily return the state I want to check afterwards. For example, checking balance of other addresses post-call. – lsankar4033 Aug 1 '19 at 22:26
2

You can do this by forking the network and performing the transaction on your forked (private) chain.

To do this, have an instance of the network on port 8545 and run the following ganache-cli command:

ganache-cli --fork http://localhost:8545

Additionally, you can specify the block number to fork at with:

ganache-cli --fork http://localhost:8545@1599200

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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