Sometimes we do not need to execute something on blockchain and change its state but want to get some information: stored value or calculation results. There are get methods for this purpose. They are part of the smart contract but they are used outside of blockchain. The code is loaded and executed locally without notifications to the network.
Such methods mostly have returned values and have modifier method_id
. For example:
(int, int, int, int, int, int) getpayment() method_id {
var (seqno, dnst, pk, std_time, std_payment, std_change_payment, ext_tm_payment, ext_bit_payment, ext_ref_payment) = load_data();
return (std_time, 1 << std_payment, 1 << std_change_payment, 1 << ext_tm_payment, 1 << ext_bit_payment, 1 << ext_ref_payment);
}
int balance() method_id {
var ds = get_data().begin_parse().skip_bits(32 + 256);
var rdict = ds~load_dict();
ds.end_parse();
var ts = days_passed();
var balance = get_balance().first();
var (_, value, found) = rdict.idict_get_preveq?(16, ts);
if (found) {
balance = max(balance - value~load_grams(), 0);
}
return balance;
}
Pilots do not like to remember a lot of service information. So you would probably prefer not to remember the sequence number
of your spaceship but get it whenever you need.
seqno
that returns the sequence number
.<aside> 💡 Not visible for the user
</aside>
() recv_internal(slice in_msg) impure {
;; do nothing for internal messages
}
() recv_external(slice in_msg) impure {
slice signature = in_msg~load_bits(512);
slice in_msg_tmp = in_msg;
int msg_seqno = in_msg_tmp~load_uint(32);
int valid_until = in_msg_tmp~load_uint(32);
throw_if(35, valid_until <= now());
cell data_cell = get_data();
slice data = data_cell.begin_parse();
var (stored_seqno, public_key) = (data~load_uint(32), data~load_uint(256));
data.end_parse();
throw_unless(33, msg_seqno == stored_seqno);
throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key));
accept_message();
in_msg_tmp~touch();
while (in_msg_tmp.slice_refs()) {
var mode = in_msg_tmp~load_uint(8);
send_raw_message(in_msg_tmp~load_ref(), mode);
}
in_msg_tmp.end_parse();
set_data(begin_cell().store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell());
}
;; add your code here
int seqno() method_id {
return get_data().begin_parse().preload_uint(32);
}
;;