Run on-chain
Last updated
Was this helpful?
Last updated
Was this helpful?
Learn how to run methods of a contract on-chain
.
Core api is more flexible than and you can perform a lot of complex logic using it. But you will need to write more code with it as well :)
You need to application before running its methods.
Full sample:
Run operation consists of few steps:
Creating a message;
Sending a message;
Receiving the message completion transaction;
Receiving external messages created by return
function;
Decoding the messages bodies according to the ABI.
process_message
methodprocess_message
performs all the run operation steps inside.
Note: This is an easy to implement pattern to run, but we do not recommend to use it because of network inconsistency and application crash probability. For more reliable approach - use the Pattern 2.
For example, a contract has such method:
Client method call:
See the full sample in the repository with sdk samples:
encode_message
-> send_message
-> wait_for_transaction
To run a contract method, first you need to create a run message:
Now the message should be sent. sendMessage
method returns the the last block created in the shard before the message was sent.
After the message was sent we need to wait for the transaction starting from the last shard block:
Retrieve result.fees
object of process_message
or wait_for_transaction
. You need result.fees.account_fees
for total account fees value.
Here is the structure of fees object.
storage_fee
: bigint – Fee for account storage
gas_fee
: bigint – Fee for processing
in_msg_fwd_fee
: bigint – Deprecated. Left for backward compatibility.
ext_in_msg_fee
: bigint – Fee for inbound external message import.
total_fwd_fees
: bigint – Total fees the account pays for message forwarding
account_fees
: _bigint_** – Total account fees for the transaction execution. Compounds of storage_fee + gas_fee + ext_in_msg_fee + total_fwd_fees**
out_msgs_fwd_fee
: bigint – Deprecated. Left for backward compatibility.
total_account_fees
: bigint – Deprecated. Left for backward compatibility.
This is the field that is named as total_fees
in GraphQL API Transaction type. total_account_fees
name is misleading, because it does not mean account fees, instead it means validators total fees received for the transaction execution. It does not include some forward fees that account actually pays now, but validators will receive later during value delivery to another account (not even in the receiving transaction but along the way of a chain of transactions processing).
Because of all of this, this field is not interesting for those who want to understand
the real account fees, this is why it is deprecated and left for backward compatibility.
total_output
: bigint – Deprecated. Left for backward compatibility.
The method runs ABI-compatible contract method on-chain and returns the result transaction along with the external messages bocs, and decoded external messages bodies.
See the full example in sdk samples repository:
Check out for this use case.