diff --git a/contracts/pingpong.fc b/contracts/pingpong.fc new file mode 100644 index 0000000..dc55f5f --- /dev/null +++ b/contracts/pingpong.fc @@ -0,0 +1,64 @@ +#include "imports/stdlib.fc"; + +const op::toggle = "op::toggle"c; + +;; storage variables + +;; id is required to be able to create different instances of counters +;; since addresses in TON depend on the initial state of the contract +global slice str = "ping"; + +;; load_data populates storage variables using stored data +() load_data() impure { + var ds = get_data().begin_parse(); + str = ds~load_slice(); + ds.end_parse(); +} + +;; save_data stores storage variables as a cell into persistent storage +() save_data() impure { + set_data( + begin_cell() + .store_slice(str) + .end_cell() + ); +} + +;; recv_internal is the main function of the contract and is called when it receives a message from other contracts +() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { + if (in_msg_body.slice_empty?()) { ;; ignore all empty messages + return (); + } + + slice cs = in_msg_full.begin_parse(); + int flags = cs~load_uint(4); + if (flags & 1) { ;; ignore all bounced messages + return (); + } + + load_data(); ;; here we populate the storage variables + + int op = in_msg_body~load_uint(32); ;; by convention, the first 32 bits of incoming message is the op + int query_id = in_msg_body~load_uint(64); ;; also by convention, the next 64 bits contain the "query id", although this is not always the case + + if (op == op::increase) { + slice str = = begin_cell() + .store_slice("pong") + .store_ref(null()) + .end_cell() + .begin_parse(); + save_data(); + return (); + } + + throw(0xffff); ;; if the message contains an op that is not known to this contract, we throw +} + +;; get methods are a means to conveniently read contract data using, for example, HTTP APIs +;; they are marked with method_id +;; note that unlike in many other smart contract VMs, get methods cannot be called by other contracts + +int get_str() method_id { + load_data(); + return str; +}