162 lines
4.8 KiB
Plaintext
162 lines
4.8 KiB
Plaintext
#pragma version =0.4.0;
|
|
|
|
;; =============== Error Codes ===============
|
|
const int ERROR_UNAUTHORIZED = 100;
|
|
const int ERROR_INSUFFICIENT_AMOUNT = 101;
|
|
const int ERROR_BET_NOT_FOUND = 102;
|
|
const int ERROR_BET_CLOSED = 103;
|
|
const int ERROR_AMOUNT_MISMATCH = 104;
|
|
const int ERROR_INVALID_WINNER = 105;
|
|
const int ERROR_INVALID_FEE = 106;
|
|
const int ERROR_INVALID_OP = 107;
|
|
const int ERROR_PLATFORM_BUSY = 108;
|
|
|
|
;; =============== Operation Codes ===============
|
|
const int OP_CREATE_BET = 0x947c403e;
|
|
const int OP_JOIN_BET = 0x1d8129e2;
|
|
const int OP_RESOLVE_BET = 0xb4028984;
|
|
const int OP_PLATFORM_FEE = 0x8e3d1b27;
|
|
const int OP_BET_WON = 0x9c2c1f6a;
|
|
const int OP_UPDATE_FEE = 0x6f47d15c;
|
|
const int OP_UPDATE_MIN_BET = 0x4e93d612;
|
|
|
|
;; =============== Status Codes ===============
|
|
const int STATUS_ACTIVE = 0;
|
|
const int STATUS_COMPLETED = 1;
|
|
const int STATUS_CANCELLED = 2;
|
|
|
|
;; =============== Event Codes ===============
|
|
const int EVENT_BET_CREATED = 0x1;
|
|
const int EVENT_BET_JOINED = 0x2;
|
|
const int EVENT_BET_RESOLVED = 0x3;
|
|
const int EVENT_FEE_UPDATED = 0x4;
|
|
const int EVENT_MIN_BET_UPDATED = 0x5;
|
|
|
|
;; =============== Message Helpers ===============
|
|
() send_grams(slice address, int amount, int op) impure inline {
|
|
cell msg = begin_cell()
|
|
.store_uint(0x18, 6) ;; nobounce
|
|
.store_slice(address)
|
|
.store_coins(amount)
|
|
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
|
|
.store_uint(op, 32)
|
|
.store_uint(cur_lt(), 64)
|
|
.end_cell();
|
|
|
|
send_raw_message(msg, 1); ;; pay transfer fees separately
|
|
}
|
|
|
|
() emit_log_simple(int op, int query_id, slice data) impure inline {
|
|
cell msg = begin_cell()
|
|
.store_uint(0x10, 6) ;; nonbounce
|
|
.store_slice(my_address())
|
|
.store_coins(0)
|
|
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
|
|
.store_uint(op, 32)
|
|
.store_uint(query_id, 64)
|
|
.store_slice(data)
|
|
.end_cell();
|
|
|
|
send_raw_message(msg, 0);
|
|
}
|
|
|
|
;; =============== Math Helpers ===============
|
|
int calculate_fee(int amount, int fee_percent) inline {
|
|
return muldiv(amount, fee_percent, 100);
|
|
}
|
|
|
|
int min_of_two_ints(int a, int b) inline {
|
|
if (a > b) {
|
|
return b;
|
|
}
|
|
return a;
|
|
}
|
|
|
|
int max_of_two_ints(int a, int b) inline {
|
|
if (a > b) {
|
|
return a;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
;; =============== Storage Helpers ===============
|
|
cell pack_bet_data(int id, slice creator, int amount, cell description,
|
|
cell participants, int status, slice winner, int created_at) inline {
|
|
return begin_cell()
|
|
.store_uint(id, 32)
|
|
.store_slice(creator)
|
|
.store_coins(amount)
|
|
.store_ref(description)
|
|
.store_dict(participants)
|
|
.store_uint(status, 8)
|
|
.store_slice(winner)
|
|
.store_uint(created_at, 64)
|
|
.end_cell();
|
|
}
|
|
|
|
(int, slice, int, cell, cell, int, slice, int) unpack_bet_data(cell bet_data) inline {
|
|
slice ds = bet_data.begin_parse();
|
|
return (
|
|
ds~load_uint(32), ;; id
|
|
ds~load_msg_addr(), ;; creator
|
|
ds~load_coins(), ;; amount
|
|
ds~load_ref(), ;; description
|
|
ds~load_dict(), ;; participants
|
|
ds~load_uint(8), ;; status
|
|
ds~load_msg_addr(), ;; winner
|
|
ds~load_uint(64) ;; created_at
|
|
);
|
|
}
|
|
|
|
;; =============== Validation Helpers ===============
|
|
() validate_bet_status(int status, int expected_status) impure inline {
|
|
throw_unless(ERROR_BET_CLOSED, status == expected_status);
|
|
}
|
|
|
|
() validate_amount(int amount, int expected) impure inline {
|
|
throw_unless(ERROR_AMOUNT_MISMATCH, amount >= expected);
|
|
}
|
|
|
|
() validate_fee(int fee) impure inline {
|
|
throw_unless(ERROR_INVALID_FEE, fee <= 100);
|
|
}
|
|
|
|
() validate_owner(slice sender, slice owner) impure inline {
|
|
throw_unless(ERROR_UNAUTHORIZED, equal_slices(sender, owner));
|
|
}
|
|
|
|
;; =============== Participant Helpers ===============
|
|
int is_participant(cell participants, slice address) inline {
|
|
int is_found = false;
|
|
int key = -1;
|
|
do {
|
|
(key, slice participant, int success) = participants~udict_get_next?(8, key);
|
|
if (success & equal_slices(participant, address)) {
|
|
is_found = true;
|
|
}
|
|
} until (~ success);
|
|
return is_found;
|
|
}
|
|
|
|
int count_participants(cell participants) inline {
|
|
int count = 0;
|
|
int key = -1;
|
|
do {
|
|
(key, _, int success) = participants~udict_get_next?(8, key);
|
|
if (success) {
|
|
count += 1;
|
|
}
|
|
} until (~ success);
|
|
return count;
|
|
}
|
|
|
|
;; =============== Gas Calculations ===============
|
|
int calculate_storage_fee(int cells, int bits) inline {
|
|
return muldiv(cells * 500 + bits, 500000000, 1000);
|
|
}
|
|
|
|
int estimate_bet_fees(int participants) inline {
|
|
;; Base storage fee + participant storage + message fees
|
|
return calculate_storage_fee(2 + participants, 1024)
|
|
+ participants * 10000000; ;; 0.01 TON per participant for messages
|
|
} |