Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save farukterzioglu/a627c3c873e1c444f2deac082d02b6b0 to your computer and use it in GitHub Desktop.
Save farukterzioglu/a627c3c873e1c444f2deac082d02b6b0 to your computer and use it in GitHub Desktop.
call-then-sendtx pattern for Ethereum Dapps
/*
In Ethereum, a contract can be written so that it returns a value for eth_call.
A Dapp can then check for success or error value of eth_call, before calling eth_sendTransaction,
to take advantage of eth_call effectively being a "preview" of the code flow that the transaction
will take. In traditional client-server, clients can't ask servers beforehand what's going to
happen when the client makes a call; with Dapps contracts can be written so that clients can ask
for a "preview" of what is going to happen, before any funds/ethers are actually utilized
(eth_call does not cost any ethers).
Note: it is possible that in between eth_call and when eth_sendTransaction is actually mined,
there could be state changes which means eth_sendTransaction will not behave according to the
"preview" behavior seen by eth_call. Like most patterns, some discretion is needed about use.
Here's an example of the pattern:
*/
var callResult = contractInstance.functionName.call(arg1, arg2, ...);
if (callResult.toNumber() === <success code>) {
console.log('eth_call succeeds so now sendTx...')
}
else {
return;
}
contractInstance.functionName.sendTransaction(arg1, arg2, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment