144 lines
4.5 KiB
Plaintext
144 lines
4.5 KiB
Plaintext
#pragma version ^0.4.1;
|
|
#include "../imports/stdlib.fc";
|
|
#include "../utils/Constants.fc"; ;; Add Constants import
|
|
|
|
;; Storage Variables
|
|
global cell pools_dict;
|
|
global cell tokens_dict;
|
|
const int WTON_ID = 0;
|
|
|
|
;; Reserve calculation function - fixed contract state access
|
|
(int, int) get_reserves(slice pool_addr, int token_in, int token_out) inline {
|
|
;; Get contract data using get_data
|
|
cell state = get_data();
|
|
if (state.begin_parse().slice_empty?()) {
|
|
return (0, 0);
|
|
}
|
|
|
|
slice state_data = state.begin_parse();
|
|
|
|
int reserve0 = state_data~load_coins();
|
|
int reserve1 = state_data~load_coins();
|
|
|
|
if (token_in < token_out) {
|
|
return (reserve0, reserve1);
|
|
} else {
|
|
return (reserve1, reserve0);
|
|
}
|
|
}
|
|
|
|
;; Output amount calculation using x*y=k formula
|
|
int calc_out_amount(int amount_in, int reserve_in, int reserve_out) inline {
|
|
throw_unless(error::invalid_amount, amount_in > 0);
|
|
throw_unless(error::insufficient_liquidity, reserve_in > 0);
|
|
throw_unless(error::insufficient_liquidity, reserve_out > 0);
|
|
|
|
int amount_with_fee = muldiv(amount_in, FEE_DENOMINATOR - DEFAULT_SWAP_FEE, FEE_DENOMINATOR);
|
|
int numerator = muldiv(amount_with_fee, reserve_out, 1);
|
|
int denominator = reserve_in + amount_with_fee;
|
|
return muldiv(numerator, 1, denominator);
|
|
}
|
|
|
|
() load_data() impure {
|
|
slice ds = get_data().begin_parse();
|
|
pools_dict = ds~load_dict();
|
|
tokens_dict = ds~load_dict();
|
|
}
|
|
|
|
() save_data() impure {
|
|
set_data(begin_cell()
|
|
.store_dict(pools_dict)
|
|
.store_dict(tokens_dict)
|
|
.end_cell());
|
|
}
|
|
|
|
slice get_pool_addr(int token0, int token1) inline {
|
|
int key = (min(token0, token1) << 128) | max(token0, token1);
|
|
(slice addr, int found) = pools_dict.udict_get?(256, key);
|
|
throw_unless(error::pool_not_found, found); ;; Using proper error constant
|
|
return addr;
|
|
}
|
|
|
|
;; Rest of path finding functions remain same but with proper error handling
|
|
(int, cell) calc_direct_swap(int token_in, int token_out, int amount) inline {
|
|
;; Added validation
|
|
throw_unless(error::invalid_token, token_in != token_out);
|
|
|
|
slice pool = get_pool_addr(token_in, token_out);
|
|
(int reserve_in, int reserve_out) = get_reserves(pool, token_in, token_out);
|
|
|
|
throw_unless(error::insufficient_liquidity, reserve_in > 0);
|
|
throw_unless(error::insufficient_liquidity, reserve_out > 0);
|
|
|
|
int out_amount = calc_out_amount(amount, reserve_in, reserve_out);
|
|
|
|
cell path = begin_cell()
|
|
.store_uint(token_in, 32)
|
|
.store_uint(token_out, 32)
|
|
.store_slice(pool)
|
|
.end_cell();
|
|
|
|
return (out_amount, path);
|
|
}
|
|
|
|
;; Rest of the code remains the same
|
|
() execute_swap(slice in_msg_body, cell path) impure {
|
|
int amount = in_msg_body~load_coins();
|
|
throw_unless(error::invalid_amount, amount > MIN_POOL_AMOUNT);
|
|
|
|
int min_out = in_msg_body~load_coins();
|
|
slice sender = in_msg_body~load_msg_addr();
|
|
|
|
slice ps = path.begin_parse();
|
|
while (ps.slice_bits() > 0) {
|
|
int token_in = ps~load_uint(32);
|
|
int token_out = ps~load_uint(32);
|
|
slice pool = ps~load_msg_addr();
|
|
|
|
send_raw_message(begin_cell()
|
|
.store_uint(0x18, 6)
|
|
.store_slice(pool)
|
|
.store_coins(amount)
|
|
.store_uint(0, 107)
|
|
.store_uint(op::swap, 32)
|
|
.store_uint(token_in, 32)
|
|
.store_uint(token_out, 32)
|
|
.store_slice(sender)
|
|
.end_cell(), 64);
|
|
}
|
|
}
|
|
|
|
;; Get methods
|
|
cell find_best_path(int token_in, int token_out, int amount) method_id {
|
|
load_data();
|
|
|
|
(int direct_out, cell direct_path) = calc_direct_swap(token_in, token_out, amount);
|
|
(int wton_out, cell wton_path) = calc_wton_path(token_in, token_out, amount);
|
|
|
|
return (direct_out >= wton_out) ? direct_path : wton_path;
|
|
}
|
|
|
|
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
|
|
if (in_msg_body.slice_empty?()) { return (); }
|
|
|
|
slice cs = in_msg_full.begin_parse();
|
|
cs~skip_bits(4);
|
|
slice sender = cs~load_msg_addr();
|
|
int op = in_msg_body~load_uint(32);
|
|
|
|
load_data();
|
|
|
|
if (op == op::swap) {
|
|
int token_in = in_msg_body~load_uint(32);
|
|
int token_out = in_msg_body~load_uint(32);
|
|
int amount = in_msg_body~load_coins();
|
|
cell path = find_best_path(token_in, token_out, amount);
|
|
execute_swap(in_msg_body, path);
|
|
}
|
|
|
|
save_data();
|
|
}
|
|
|
|
() main() {
|
|
;; Main function for router
|
|
} |