Is it possible to emit event in view function (via event or logi)? Or is it possible to return value to javascript without(before) mining in a transaction function? My aim is to avoid wait due to mining with event logging.
3 Answers
Q1: An event is a state change. It's not possible to emit them from view
or pure
functions, or constant
in the old syntax.
Q2: It is not possible to get response values from a signed transaction. You will always get a transaction receipt.
The patterns take some getting used to. I'm a big believer in emitting auditable event logs for every important state change. When transactions are mined, the pertinent information arrives with the confirmation as event log arguments. Those are for off-chain clients.
The return
values or still important. They are the response values if the function is called from another function or contract.
For example:
bool result = myOtherContract.insertSomething(); // expecting "true"/success
If that's important to a client, something like:
emit LogResult(msg.sender, result);
Hope it helps.
Is it possible to emit event in view function (via event or logi)?
Since event logs are stored in transaction receipts, I do not believe it is possible to emit an event within a view function since no transaction occurs.
Or is it possible to return value to javascript without(before) mining in a transaction function?
I don't think this is possible either. The return value from a function which creates a transaction will be the transaction receipt. Within the receipt, you can find any events which were emitted within that function.
-
But receipt will require mining of the block and thus, we need to wait. I am writing a time critical function with logs. Commented Apr 19, 2018 at 20:36
-
You can execute the function locally using Call in web3. I think the second part of your answer is not correct. See my answer below.– JaimeCommented Apr 19, 2018 at 20:37
-
From what I observed, transaction submission returns transaction hash and transaction confirmation returns transaction receipt with emitted event details. Commented Apr 19, 2018 at 20:40
The first question, It is not possible because the event modifies the state. In Remix you will see a warning for doing this.
The second question. You can execute the functions locally without sending a transaction to the network using Call, it will not consume gas and will be executed immediately.
However, if what you are aiming at is to get transaction made by users of your system before the transactions are mined, this is not possible.
Hope this helps