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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI 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?
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()
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