Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logic to strategy #69

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion contract_lambdas/Admin_lambdas.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
},
{
"index": 7,
"name": "add_pool"
"name": "approve_spending"
},
{
"index": 8,
"name": "add_pool"
},
{
"index": 9,
"name": "set_strategy_factory"
}
]
12 changes: 0 additions & 12 deletions contract_lambdas/Strategy_lambdas.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
},
{
"index": 1,
"name": "connect_token_to_strategy"
},
{
"index": 2,
"name": "update_token_strategy_params"
},
{
"index": 3,
"name": "set_rebalance"
},
{
"index": 4,
"name": "strategy_rebalance"
}
]
221 changes: 63 additions & 158 deletions contracts/compiled/dex4factory.tz

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions contracts/partials/admin/lambdas.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,24 @@ function claim_dev(
]
} with (operations, s)

(* [Dis-]Approve spending of pool tokens *)
function approve_spending(
const p : admin_action_t;
var s : storage_t)
: return_t is
block {
var operations: list(operation) := Constants.no_operations;
case p of [
| Approve_spending(params) -> {
const tokens : tokens_map_t = unwrap(s.tokens[params.pool_id], Errors.Dex.pool_not_listed);

operations := typed_approve(
Tezos.get_self_address(),
params.spender,
params.amount,
unwrap(tokens[params.token_id], Errors.Dex.wrong_index)
) # operations;
}
| _ -> unreachable(Unit)
]
} with (operations, s)
5 changes: 3 additions & 2 deletions contracts/partials/admin/methods.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
| Stop_ramp_A(_) -> 4n
| Set_fees(_) -> 5n
| Set_default_referral(_) -> 6n
| Approve_spending(_) -> 7n
#if !FACTORY
| Add_pool(_) -> 7n
| Set_strategy_factory(_) -> 8n
| Add_pool(_) -> 8n
| Set_strategy_factory(_) -> 9n
#endif
];

Expand Down
8 changes: 8 additions & 0 deletions contracts/partials/admin/types.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type init_param_t is [@layout:comb] record [
]
#endif

type approve_spending_param_t is [@layout:comb] record [
pool_id : nat;
token_id : nat;
spender : address;
amount : nat;
]

type admin_action_t is
| Add_rem_managers of set_cand_param_t
| Set_admin of address
Expand All @@ -31,6 +38,7 @@ type admin_action_t is
| Stop_ramp_A of nat
| Set_fees of set_fee_param_t
| Set_default_referral of address
| Approve_spending of approve_spending_param_t
#if !FACTORY
| Add_pool of init_param_t (* sets initial liquidity *)
| Set_strategy_factory of set_cand_param_t
Expand Down
6 changes: 3 additions & 3 deletions contracts/partials/constants.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
const dex_func_count : nat = 7n;
const dev_func_count : nat = 2n;
const token_func_count : nat = 5n;
const strat_func_count : nat = 5n;
const strat_func_count : nat = 2n;
#if !FACTORY
const admin_func_count : nat = 9n;
const admin_func_count : nat = 10n;
#else
const admin_func_count : nat = 7n;
const admin_func_count : nat = 8n;
#endif

(* StableSwap constants *)
Expand Down
12 changes: 7 additions & 5 deletions contracts/partials/dex_core/helpers.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ function get_pool_info(
ref_f = 0n;
stakers_f = 0n;
];
strategy = record [
strat_contract = (None: option(address));
configuration = (map []: map(token_pool_idx_t, strategy_storage_t));
];
strategy = (None: option(address));
staker_accumulator = record [
accumulator_f = (map []: map(token_pool_idx_t, nat));
total_fees = (map []: map(token_pool_idx_t, nat));
Expand Down Expand Up @@ -249,4 +246,9 @@ function check_shares_and_reserves(
then failwith(Errors.Dex.supply_drained)
else if pool.total_supply > 0n and reserves_sum = 0n
then failwith(Errors.Dex.reserves_drained)
else Unit;
else Unit;

[@inline] function map_reserves(
const tokens_info : map(token_pool_idx_t, token_info_t))
: map(token_pool_idx_t, nat) is
Map.map(function (const _:token_pool_idx_t; const v: token_info_t) is v.reserves, tokens_info);
81 changes: 41 additions & 40 deletions contracts/partials/dex_core/lambdas.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ function swap(
require(i < tokens_count and j < tokens_count, Errors.Dex.wrong_index);
const receiver = unwrap_or(params.receiver, Tezos.get_sender());
var pool : pool_t := unwrap(s.pools[params.pool_id], Errors.Dex.pool_not_listed);
// dy - tokens from swap i to j calculation
const dy = perform_swap(i, j, dx, pool);
const pool_total_staked = pool.staker_accumulator.total_staked;
// slice swap fees
const after_fees = slice_fee(dy, pool.fee, get_dev_fee(s), pool_total_staked);
var to_stakers_f := 0n;
if pool_total_staked > 0n
Expand Down Expand Up @@ -52,29 +54,34 @@ function swap(
token_info_j.reserves := nat_or_error(token_info_j.reserves - after_fees.dy, Errors.Dex.no_liquidity);
pool.tokens_info[i] := token_info_i;
pool.tokens_info[j] := token_info_j;

// transfer tokens to user (operations stack, reverse ordering)
operations := typed_transfer(
Tezos.get_self_address(),
receiver,
after_fees.dy,
token_j
) # operations;
const rebalance = operate_with_strategy(
map[
i -> token_info_i;
j -> token_info_j
],
s.tokens[params.pool_id],
pool.strategy,
False
);
operations := concat_lists(rebalance.0, operations);

// check and rebalance if needed before sending to user (operations stack, reverse ordering)
operations := case check_rebalancing_strategy(
pool.strategy,
map [
i -> token_info_i.reserves;
j -> token_info_j.reserves
]
) of [
| Some(op) -> op # operations
| None -> operations
];

// transfer tokens from user (operations stack, reverse ordering)
operations := typed_transfer(
Tezos.get_sender(),
Tezos.get_self_address(),
dx,
unwrap(tokens[i], Errors.Dex.no_token)
) # operations;
pool.strategy := rebalance.1;
s.pools[params.pool_id] := pool;
const event_params: swap_event_t = record[
pool_id = params.pool_id;
Expand All @@ -85,6 +92,7 @@ function swap(
receiver = receiver;
referral = referral;
];
// emit swap event
operations := emit_event(SwapEvent(event_params)) # operations;
}
| _ -> unreachable(Unit)
Expand Down Expand Up @@ -163,17 +171,14 @@ function divest_liquidity(
const tokens : tokens_map_t = unwrap(s.tokens[params.pool_id], Errors.Dex.pool_not_listed);
const res = Map.fold(divest_reserves, tokens, record [ tok_inf = pool.tokens_info; op = operations; outs = (map[]: map(token_pool_idx_t, nat)) ]);
pool.tokens_info := res.tok_inf;
const rebalance = operate_with_strategy(
pool.tokens_info,
s.tokens[params.pool_id],
pool.strategy,
False
);
operations := concat_lists(
rebalance.0,
res.op
);
pool.strategy := rebalance.1;
operations := case check_rebalancing_strategy(
pool.strategy,
map_reserves(pool.tokens_info)
) of [
| Some(op) -> op # res.op
| None -> res.op
];

pool.total_supply := nat_or_error(pool.total_supply - params.shares, Errors.Dex.low_total_supply);

const key = (Tezos.get_sender(), params.pool_id);
Expand Down Expand Up @@ -303,14 +308,13 @@ function divest_imbalanced(
total_supply = nat_or_error(token_supply - burn_amount, Errors.Math.nat_error);
];
check_shares_and_reserves(pool);
const rebalance = operate_with_strategy(
balanced.tokens_info,
s.tokens[params.pool_id],
pool.strategy,
False
);
operations := concat_lists(rebalance.0, operations);
pool.strategy := rebalance.1;
operations := case check_rebalancing_strategy(
pool.strategy,
map_reserves(balanced.tokens_info)
) of [
| Some(op) -> op # operations
| None -> operations
];
s.pools[params.pool_id] := pool;
s.ledger[(Tezos.get_sender(), params.pool_id)] := new_shares;
const event_params: divest_imb_event_t = record[
Expand Down Expand Up @@ -384,22 +388,19 @@ function divest_one_coin(
pool.total_supply := result.ts;
check_shares_and_reserves(pool);
const receiver = unwrap_or(params.receiver, Tezos.get_sender());


operations := typed_transfer(
Tezos.get_self_address(),
receiver,
result.dy,
token
) # operations;
const rebalance = operate_with_strategy(
map[ params.token_index -> info ],
s.tokens[params.pool_id],
pool.strategy,
False
);
operations := concat_lists(rebalance.0, operations);
pool.strategy := rebalance.1;
operations := case check_rebalancing_strategy(
pool.strategy,
map [params.token_index -> info.reserves]
) of [
| Some(op) -> op # operations
| None -> operations
];
s.pools[params.pool_id] := pool;
const account_bal = unwrap_or(s.ledger[sender_key], 0n);

Expand Down
14 changes: 7 additions & 7 deletions contracts/partials/dex_core/math.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,13 @@ function add_liq(
else operations;

pool.total_supply := pool.total_supply + mint_amount;
const (rebalance_ops, strategy_store) = operate_with_strategy(
pool.tokens_info,
tokens[params.pool_id],
pool.strategy,
False
);
pool.strategy := strategy_store;
const rebalance_ops = case check_rebalancing_strategy(
pool.strategy,
map_reserves(pool.tokens_info)
) of [
| Some(op) -> op # Constants.no_operations
| None -> Constants.no_operations
];
s.pools[params.pool_id] := pool;

const receiver = unwrap_or(params.receiver, Tezos.get_sender());
Expand Down
2 changes: 1 addition & 1 deletion contracts/partials/dex_core/storage.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type pool_t is [@layout:comb] record [
tokens_info : map(token_pool_idx_t, token_info_t);
fee : fees_storage_t;

strategy : strategy_full_storage_t;
strategy : option(address);

staker_accumulator : staker_accum_t;

Expand Down
2 changes: 2 additions & 0 deletions contracts/partials/errors.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ module Strategy is {
const wrong_params : string = "wrong-connect-params-strategy";
const already_connected : string = "token-strategy-connected";
const nothing_to_rebalance : string = "nothing-to-rebalance";
const no_deposit_view : string = "no-deposit-view";
const no_should_rebalance_view : string = "no-should-rebalance-view";
}
5 changes: 1 addition & 4 deletions contracts/partials/factory/helpers.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ function form_pool_storage(
future_A_time = Tezos.get_now();
tokens_info = tokens_info;
fee = fees;
strategy = record[
strat_contract = (None: option(address));
configuration = (map[]: map(token_pool_idx_t, strategy_storage_t));
];
strategy = (None: option(address));
staker_accumulator = record [
accumulator_f = (map []: map(token_pool_idx_t, nat));
total_fees = (map []: map(token_pool_idx_t, nat));
Expand Down
Loading