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

Refactoring #55

Open
wants to merge 4 commits into
base: renames-and-sell-ab
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
5 changes: 3 additions & 2 deletions contracts/main/Dex.ligo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "../partials/Errors.ligo"
#include "../partials/IDex.ligo"
#include "../partials/Utils.ligo"
#include "../partials/MethodsFA2.ligo"
Expand All @@ -19,11 +20,11 @@ function main(
| SetDexFunction(params) ->
((nil:list(operation)),
if params.index > dex_func_count
then (failwith("Dex/wrong-index") : full_storage_type)
then (failwith(err_high_func_index) : full_storage_type)
else set_dex_function(params.index, params.func, s))
| SetTokenFunction(params) ->
((nil:list(operation)),
if params.index > token_func_count
then (failwith("Dex/wrong-index") : full_storage_type)
then (failwith(err_high_func_index) : full_storage_type)
else set_token_function(params.index, params.func, s))
end
16 changes: 9 additions & 7 deletions contracts/partials/Dex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function call_dex(
var s : full_storage_type)
: full_return_type is
block {
s.storage.entered := check_reentrancy(s.storage.entered);

const idx : nat = case p of
AddPair(_) -> 0n
| Swap(_) -> 1n
Expand All @@ -20,13 +22,13 @@ function call_dex(
var res : return_type :=
case s.dex_lambdas[idx] of
Some(f) -> f(p, s.storage)
| None -> (failwith("Dex/function-not-set") : return_type)
| None -> (failwith(err_unknown_func) : return_type)
end;
s.storage := res.1;
res.0 := Tezos.transaction(
unit,
0mutez,
get_close_entrypoint(unit)) # res.0;
(Tezos.self("%close") : contract(unit))) # res.0;
} with (res.0, s)

(*
Expand All @@ -45,7 +47,7 @@ function call_token(
const res : return_type =
case s.token_lambdas[idx] of
Some(f) -> f(p, s.storage)
| None -> (failwith("Dex/function-not-set") : return_type)
| None -> (failwith(err_unknown_func) : return_type)
end;
s.storage := res.1;
} with (res.0, s)
Expand All @@ -56,10 +58,10 @@ function close(
: full_storage_type is
block {
if not s.storage.entered
then failwith("Dex/not-entered")
then failwith(err_not_entered)
else skip;
if Tezos.sender =/= Tezos.self_address
then failwith("Dex/not-self")
then failwith(err_sender_not_self)
else skip;
s.storage.entered := False;
} with s
Expand Down Expand Up @@ -95,7 +97,7 @@ function set_dex_function(
: full_storage_type is
block {
case s.dex_lambdas[idx] of
Some(_) -> failwith("Dex/function-set")
Some(_) -> failwith(err_func_set)
| None -> s.dex_lambdas[idx] := f
end;
} with s
Expand All @@ -108,7 +110,7 @@ function set_token_function(
: full_storage_type is
block {
case s.token_lambdas[idx] of
Some(_) -> failwith("Dex/function-set")
Some(_) -> failwith(err_func_set)
| None -> s.token_lambdas[idx] := f
end;
} with s
21 changes: 21 additions & 0 deletions contracts/partials/Errors.ligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const err_reentrancy : string = "Dex/reentrancy";
const err_func_set : string = "Dex/function-set";
const err_high_func_index : string = "Dex/wrong-index";
const err_unknown_func : string = "Dex/function-not-set";
const err_not_entered : string = "Dex/not-entered";
const err_sender_not_self : string = "Dex/not-self";
const err_wrong_token_entrypoint : string = "Dex/not-token";
const err_pair_not_listed : string = "Dex/not-launched";
const err_no_liquidity : string = "Dex/no-liquidity";
const err_zero_a_in : string = "Dex/no-token-a-in";
const err_zero_b_in : string = "Dex/no-token-b-in";
const err_pair_listed : string = "Dex/pair-exist";
const err_wrong_route : string = "Dex/wrong-route";
const err_zero_in : string = "Dex/zero-amount-in";
const err_insufficient_lp : string = "Dex/insufficient-shares";
const err_dust_out : string = "Dex/dust-output";
const err_high_min_out : string = "Dex/high-min-out";
const err_wrong_pair_order : string = "Dex/wrong-pair-order";
const err_empty_route : string = "Dex/empty-route";
const err_low_max_a_in : string = "Dex/low-max-token-a-in";
const err_low_max_b_in : string = "Dex/low-max-token-b-in";
19 changes: 5 additions & 14 deletions contracts/partials/IDex.ligo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "./TypesFA2.ligo"

(* Represents account info *)
type account_info is record [
balance : nat; (* LP tokens *)
allowances : set (address); (* accounts allowed to act on behalf of the user *)
Expand Down Expand Up @@ -38,24 +37,21 @@ type tokens_type is record [
token_b_type : token_type; (* token B standard *)
]


(* record for the dex storage_type *)
type storage_type is record [
entered : bool; (* reentrancy protection *)
pairs_count : nat; (* total shares count *)
tokens : big_map(nat, tokens_type); (* all the tokens list *)
token_to_id : big_map(bytes, nat); (* all the tokens list *)
pairs : big_map(nat, pair_type); (* account info per address *)
pairs : big_map(nat, pair_type); (* pair info per token id *)
ledger : big_map((address * nat), account_info); (* account info per address *)
]

(* operation type *)
type swap_type is
| A_to_b (* exchange token A to token B *)
| B_to_a (* exchange token B to token A *)

type swap_slice_type is record [
pair : tokens_type; (* exchange pair info *)
pair_id : nat; (* pair identifier *)
operation : swap_type; (* exchange operation *)
]

Expand All @@ -77,7 +73,6 @@ type tmp_swap_type is record [
receiver : address; (* address of the receiver *)
]

(* Entrypoint arguments *)
type route_type is [@layout:comb] record [
swaps : list(swap_slice_type); (* swap operations list*)
amount_in : nat; (* amount of tokens to be exchanged *)
Expand All @@ -92,21 +87,20 @@ type initialize_params is [@layout:comb] record [
]

type invest_type is [@layout:comb] record [
pair : tokens_type; (* exchange pair info *)
pair_id : nat; (* pair identifier *)
shares : nat; (* the amount of shares to receive *)
token_a_in : nat; (* min amount of tokens A invested *)
token_b_in : nat; (* min amount of tokens B invested *)
]

type divest_type is [@layout:comb] record [
pair : tokens_type; (* exchange pair info *)
pair_id : nat; (* pair identifier *)
min_token_a_out : nat; (* min amount of tokens A received to accept the divestment *)
min_token_b_out : nat; (* min amount of tokens B received to accept the divestment *)
shares : nat; (* amount of shares to be burnt *)
]

type action_type is
(* User's entrypoints *)
AddPair of initialize_params (* sets initial liquidity *)
| Swap of route_type (* exchanges token to another token and sends them to receiver *)
| Invest of invest_type (* mints min shares after investing tokens *)
Expand All @@ -117,7 +111,6 @@ type reserves_type is record [
pair_id : nat; (* pair identifier *)
]

(* Main function parameter types specific for FA2 standard*)
type transfer_type is list (transfer_param)
type operator_type is list (update_operator_param)

Expand All @@ -140,7 +133,6 @@ type set_dex_func_type is record [
index : nat; (* the key in functions map *)
]

(* full list of dex entrypoints *)
type full_action_type is
| Use of action_type
| Transfer of transfer_type (* transfer asset from one account to another *)
Expand All @@ -151,7 +143,6 @@ type full_action_type is
| SetDexFunction of set_dex_func_type (* sets the dex specific function. Is used before the whole system is launched *)
| SetTokenFunction of set_token_func_type (* sets the FA function, is used before the whole system is launched *)

(* real dex storage_type *)
type full_storage_type is record [
storage : storage_type; (* real dex storage_type *)
metadata : big_map(string, bytes); (* metadata storage_type according to TZIP-016 *)
Expand All @@ -165,4 +156,4 @@ const fee_rate : nat = 333n;
const dex_func_count : nat = 3n;
const token_func_count : nat = 2n;
const fee_denom : nat = 1000n;
const fee_num : nat = 997n;
const fee_num : nat = 997n;
Loading