2024-12-08 21:57:35 +03:00

118 lines
3.6 KiB
Plaintext

#pragma version ^0.4.1;
#include "../imports/stdlib.fc";
#include "../utils/Constants.fc";
#include "../utils/Math.fc";
#include "../utils/Security.fc";
;; Operation codes
const int op_deposit = 1;
const int op_withdraw = 2;
const int op_allocate_budget = 3;
const int op_execute_proposal = 4;
const int op_sweep_tokens = 5;
const int op_set_strategy = 6;
;; Error codes
const int error_unauthorized = 101;
const int error_insufficient_balance = 102;
const int error_invalid_amount = 103;
const int error_invalid_allocation = 104;
const int error_strategy_exists = 105;
const int error_invalid_strategy = 106;
const int error_execution_failed = 107;
;; Treasury allocation types
const int allocation_reserves = 1; ;; Emergency reserves
const int allocation_operations = 2; ;; Operations budget
const int allocation_development = 3; ;; Development funding
const int allocation_marketing = 4; ;; Marketing budget
const int allocation_staking = 5; ;; Staking rewards
;; Storage variables
global slice owner; ;; DAO contract address
global int total_balance; ;; Total treasury balance
global int last_report_time; ;; Last financial report timestamp
global slice dao_contract; ;; DAO contract address
global slice executor; ;; Executor contract address
;; Helper functions for dictionary operations
(cell, int) load_data_dict() {
slice ds = get_data().begin_parse();
if (ds.slice_empty?()) {
return (new_dict(), 0);
}
cell dict = ds~load_dict();
int dict_size = ds~load_uint(32);
return (dict, dict_size);
}
() save_data_dict(cell allocations, cell strategies, cell approved_tokens) impure {
set_data(begin_cell()
.store_slice(owner)
.store_coins(total_balance)
.store_uint(last_report_time, 64)
.store_slice(dao_contract)
.store_slice(executor)
.store_dict(allocations)
.store_dict(strategies)
.store_dict(approved_tokens)
.end_cell());
}
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) {
return (); ;; Accept plain transfers
}
slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
if (flags & 1) {
return ();
}
slice sender_address = cs~load_msg_addr();
int op = in_msg_body~load_uint(32);
;; Access control
throw_unless(error_unauthorized,
equal_slices(sender_address, owner) |
equal_slices(sender_address, dao_contract) |
equal_slices(sender_address, executor));
;; Handle operations
if (op == op_deposit) {
handle_deposit(msg_value, sender_address);
} elseif (op == op_withdraw) {
handle_withdraw(sender_address, in_msg_body);
} elseif (op == op_allocate_budget) {
handle_allocate_budget(sender_address, in_msg_body);
} elseif (op == op_set_strategy) {
handle_set_strategy(sender_address, in_msg_body);
}
}
() handle_deposit(int amount, slice sender) impure {
throw_unless(error_invalid_amount, amount > 0);
total_balance += amount;
emit_log_deposit(sender, amount);
}
() handle_withdraw(slice sender, slice msg) impure {
int amount = msg~load_coins();
throw_unless(error_invalid_amount, amount > 0);
throw_unless(error_insufficient_balance, amount <= total_balance);
total_balance -= amount;
;; Send coins
send_raw_message(begin_cell()
.store_uint(0x18, 6)
.store_slice(sender)
.store_coins(amount)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.end_cell(), 3);
}
(int) get_total_balance() method_id {
return total_balance;
}