You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

;; =============== storage =============================
2 years ago
;; storage binary format is defined as TL-B in companion .tlb file
2 years ago
#pragma version >=0.2.0;
#include "imports/stdlib.fc";
#include "imports/constants.fc";
#include "imports/utils.fc";
2 years ago
(slice, int) load_data() inline {
var ds = get_data().begin_parse();
return (
ds~load_msg_addr(), ;; owner_address
ds~load_uint(64) ;; counter
);
}
() save_data(slice owner_address, int counter) impure inline {
set_data(begin_cell()
.store_slice(owner_address)
.store_uint(counter, 64)
.end_cell());
}
;; =============== messages =============================
;; message binary format is defined as TL-B in companion .tlb file
2 years ago
() op_withdraw(int withdraw_amount, slice owner_address) impure;
2 years ago
() recv_internal(int msg_value, cell in_msg, slice in_msg_body) impure {
2 years ago
;; parse incoming internal message
slice cs = in_msg.begin_parse();
2 years ago
int flags = cs~load_uint(4); ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
slice sender_address = cs~load_msg_addr();
;; handle bounced messages
if (flags & 1) {
return (); ;; ignore
}
;; load from contract storage
var (owner_address, counter) = load_data();
;; handle operations
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
2 years ago
if (op == op::increment) {
2 years ago
save_data(owner_address, counter + 1);
return ();
}
if (op == op::deposit) {
;; empty since ton received (msg_value) is added automatically to contract balance
;; ~dump msg_value; ;; an example of debug output, requires running contract in debug mode
2 years ago
return ();
}
if (op == op::withdraw) {
throw_unless(error::access_denied, equal_slices(sender_address, owner_address));
int withdraw_amount = in_msg_body~load_coins();
2 years ago
op_withdraw(withdraw_amount, owner_address);
return ();
}
if (op == op::transfer_ownership) {
throw_unless(error::access_denied, equal_slices(sender_address, owner_address));
slice new_owner_address = in_msg_body~load_msg_addr();
2 years ago
save_data(new_owner_address, counter);
return ();
}
throw(error::unknown_op);
2 years ago
}
() op_withdraw(int withdraw_amount, slice owner_address) impure {
2 years ago
var [balance, _] = get_balance();
throw_unless(error::insufficient_balance, balance >= withdraw_amount);
int return_value = min(withdraw_amount, balance - const::min_tons_for_storage);
2 years ago
send_grams(owner_address, return_value);
}
;; =============== getters =============================
2 years ago
int meaning_of_life() method_id {
return 42;
}
slice owner_address() method_id {
var (owner_address, _) = load_data();
return owner_address;
}
int counter() method_id {
var (_, counter) = load_data();
return counter;
}
int balance() method_id {
var [balance, _] = get_balance();
return balance;
}