Skip to content

Commit

Permalink
Merge pull request #43 from madfish-solutions/token-to-token-improvem…
Browse files Browse the repository at this point in the history
…ents

pure token to token removed
  • Loading branch information
KStasi authored Jun 23, 2021
2 parents 2f6c9f0 + 3c60530 commit 25ce829
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 545 deletions.
5 changes: 3 additions & 2 deletions contracts/main/TTDex.ligo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#define FA2_STANDARD_ENABLED
#define TOKEN_TO_TOKEN_ENABLED
#include "../partials/TTDex.ligo"
#include "../partials/ITTDex.ligo"
#include "../partials/TTMethodDex.ligo"
#include "../partials/TTDex.ligo"

(* DexFA2 - Contract for exchanges for XTZ - FA2 token pair *)
function main (const p : full_action; const s : full_dex_storage) : full_return is
Expand All @@ -14,6 +15,6 @@ function main (const p : full_action; const s : full_dex_storage) : full_return
| Update_operators(params) -> call_token(IUpdate_operators(params), this, 1n, s)
| Get_reserves(params) -> get_reserves(params, s)
| Close -> ((nil:list(operation)), close(s))
| SetDexFunction(params) -> ((nil:list(operation)), if params.index > 4n then (failwith("Dex/wrong-index") : full_dex_storage) else set_dex_function(params.index, params.func, s))
| SetDexFunction(params) -> ((nil:list(operation)), if params.index > 3n then (failwith("Dex/wrong-index") : full_dex_storage) else set_dex_function(params.index, params.func, s))
| SetTokenFunction(params) -> ((nil:list(operation)), if params.index > 2n then (failwith("Dex/wrong-index") : full_dex_storage) else set_token_function(params.index, params.func, s))
end
116 changes: 53 additions & 63 deletions contracts/partials/ITTDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

(* record that represents account shares *)
type account_info is record [
balance : nat; (* liquid tokens *)
balance : nat; (* LP tokens *)
allowances : set (address); (* accounts allowed to act on behalf of the user *)
]

Expand All @@ -23,119 +23,110 @@ type token_type is
| Fa2

type pair_info is record [
token_a_pool : nat; (* tez reserves in the pool *)
token_b_pool : nat; (* token reserves in the pool *)
token_a_pool : nat; (* token A reserves in the pool *)
token_b_pool : nat; (* token B reserves in the pool *)
total_supply : nat; (* total shares count *)
]

type tokens_info is record [
token_a_address : address;
token_b_address : address;
token_a_id : nat;
token_b_id : nat;
token_a_type : token_type;
token_b_type : token_type;
token_a_address : address; (* token A address *)
token_b_address : address; (* token B address *)
token_a_id : nat; (* token A identifier *)
token_b_id : nat; (* token B identifier *)
token_a_type : token_type; (* token A standard *)
token_b_type : token_type; (* token B standard *)
]

type token_pair is bytes

(* record for the dex storage *)
type dex_storage is record [
entered : bool;
entered : bool; (* reentrancy protection *)
pairs_count : nat; (* total shares count *)
tokens : big_map(nat, tokens_info); (* all the tokens list *)
token_to_id : big_map(token_pair, nat); (* all the tokens list *)
pairs : big_map(nat, pair_info); (* account info per address *)
ledger : big_map((address * nat), account_info); (* account info per address *)
]
type swap_type is Buy | Sell
(* operation type *)
type swap_type is
| Sell (* exchange token A to token B *)
| Buy (* exchange token B to token A *)

type swap_slice_type is record [
pair : tokens_info;
operation : swap_type;
pair : tokens_info; (* exchange pair info *)
operation : swap_type; (* exchange operation *)
]

type swap_side is record [
pool : nat;
token : address;
id: nat;
standard: token_type;
pool : nat; (* pair identifier*)
token : address; (* token address*)
id : nat; (* token aidentifier *)
standard : token_type; (* token standard *)
]

type swap_data is record [
to_: swap_side;
from_: swap_side;
to_ : swap_side; (* info about sold asset *)
from_ : swap_side; (* info about bought asset *)
]

type internal_swap_type is record [
s : dex_storage;
amount_in : nat;
token_address_in : address;
token_id_in : nat;
operation : option(operation);
sender : address;
receiver : address;
s : dex_storage; (* storage state *)
amount_in : nat; (* amount of tokens to be sold *)
token_address_in : address; (* address of sold token *)
token_id_in : nat; (* identifier of sold token *)
operation : option(operation); (* exchange operation type *)
sender : address; (* address of the sender *)
receiver : address; (* address of the receiver *)
]

(* Entrypoint arguments *)
type token_to_token_route_params is
[@layout:comb]
record [
swaps : list(swap_slice_type);
swaps : list(swap_slice_type); (* swap operations list*)
amount_in : nat; (* amount of tokens to be exchanged *)
min_amount_out : nat; (* min amount of XTZ received to accept exchange *)
receiver : address; (* tokens receiver *)
]

(* Entrypoint arguments *)
type token_to_token_payment_params is
[@layout:comb]
record [
pair : tokens_info;
operation : swap_type;
amount_in : nat; (* amount of tokens to be exchanged *)
min_amount_out : nat; (* min amount of XTZ received to accept exchange *)
min_amount_out : nat; (* min amount of tokens received to accept exchange *)
receiver : address; (* tokens receiver *)
]

type invest_liquidity_params is
[@layout:comb]
record [
pair : tokens_info;
token_a_in : nat; (* min amount of XTZ received to accept the divestment *)
token_b_in : nat; (* min amount of tokens received to accept the divestment *)
pair : tokens_info; (* exchange pair info *)
token_a_in : nat; (* min amount of tokens A invested *)
token_b_in : nat; (* min amount of tokens B invested *)
]

type divest_liquidity_params is
[@layout:comb]
record [
pair : tokens_info;
min_token_a_out : nat; (* min amount of XTZ received to accept the divestment *)
min_token_b_out : nat; (* min amount of tokens received to accept the divestment *)
pair : tokens_info; (* exchange pair info *)
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 dex_action is
| InitializeExchange of invest_liquidity_params (* sets initial liquidity *)
| TokenToTokenRoutePayment of token_to_token_route_params (* exchanges XTZ to tokens and sends them to receiver *)
| TokenToTokenPayment of token_to_token_payment_params (* exchanges XTZ to tokens and sends them to receiver *)
| InvestLiquidity of invest_liquidity_params (* mints min shares after investing tokens and XTZ *)
| DivestLiquidity of divest_liquidity_params (* burns shares and sends tokens and XTZ to the owner *)
| AddPair of invest_liquidity_params (* sets initial liquidity *)
| Swap of token_to_token_route_params (* exchanges token to another token and sends them to receiver *)
| Invest of invest_liquidity_params (* mints min shares after investing tokens *)
| Divest of divest_liquidity_params (* burns shares and sends tokens to the owner *)

type use_params is dex_action
type get_reserves_params is record [
receiver : contract(nat * nat);
token_id : nat;
receiver : contract(nat * nat); (* response receiver *)
pair_id : nat; (* pair identifier *)
]

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

type token_action is
| ITransfer of transfer_params
| IBalance_of of balance_params
| IUpdate_operators of update_operator_params
| ITransfer of transfer_params (* transfer asset from one account to another *)
| IBalance_of of balance_params (* returns the balance of the account *)
| IUpdate_operators of update_operator_params (* updates the token operators *)

type return is list (operation) * dex_storage
type dex_func is (dex_action * dex_storage * address) -> return
Expand All @@ -151,19 +142,20 @@ type set_dex_function_params is record [
index : nat; (* the key in functions map *)
]

(* full list of dex entrypoints *)
type full_action is
| Use of use_params
| Transfer of transfer_params
| Balance_of of balance_params
| Update_operators of update_operator_params
| Get_reserves of get_reserves_params
| Close of unit
| Transfer of transfer_params (* transfer asset from one account to another *)
| Balance_of of balance_params (* returns the balance of the account *)
| Update_operators of update_operator_params (* updates the token operators *)
| Get_reserves of get_reserves_params (* returns the underlying token reserves *)
| Close of unit (* entrypoint to prevent reentrancy *)
| SetDexFunction of set_dex_function_params (* sets the dex specific function. Is used before the whole system is launched *)
| SetTokenFunction of set_token_function_params (* sets the FA function, is used before the whole system is launched *)

(* real dex storage *)
type full_dex_storage is record
storage : dex_storage;
storage : dex_storage; (* real dex storage *)
metadata : big_map(string, bytes); (* metadata storage according to TZIP-016 *)
dex_lambdas : big_map(nat, dex_func); (* map with exchange-related functions code *)
token_lambdas : big_map(nat, token_func); (* map with token-related functions code *)
Expand All @@ -173,5 +165,3 @@ type full_return is list (operation) * full_dex_storage

const fee_rate : nat = 333n; (* exchange fee rate distributed among the liquidity providers *)

const token_func_count : nat = 2n;

42 changes: 29 additions & 13 deletions contracts/partials/TTDex.ligo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "./ITTDex.ligo"

(* Route exchange-specific action
Due to the fabulous storage, gas and operation size limits
Expand All @@ -11,20 +9,26 @@ The function is responsible for fiding the appropriate method
based on the argument type.
*)
[@inline] function call_dex (const p : dex_action; const this : address; const s : full_dex_storage) : full_return is
[@inline] function call_dex(
const p : dex_action;
const this : address;
const s : full_dex_storage) : full_return is
block {
const idx : nat = case p of
| InitializeExchange(n) -> 0n
| TokenToTokenPayment(n) -> 1n
| TokenToTokenRoutePayment(n) -> 2n
| InvestLiquidity(n) -> 3n
| DivestLiquidity(n) -> 4n
| AddPair(n) -> 0n
| Swap(n) -> 1n
| Invest(n) -> 2n
| Divest(n) -> 3n
end;
const res : return = case s.dex_lambdas[idx] of
Some(f) -> f(p, s.storage, this)
| None -> (failwith("Dex/function-not-set") : return)
end;
s.storage := res.1;
res.0 := Tezos.transaction(
unit,
0mutez,
get_close_entrypoint(this)) # res.0;
} with (res.0, s)

(* Route token-specific action
Expand All @@ -38,7 +42,11 @@ The function is responsible for fiding the appropriate method
based on the provided index.
*)
[@inline] function call_token (const p : token_action; const this : address; const idx : nat; const s : full_dex_storage) : full_return is
[@inline] function call_token(
const p : token_action;
const this : address;
const idx : nat;
const s : full_dex_storage) : full_return is
block {
const res : return = case s.token_lambdas[idx] of
Some(f) -> f(p, s.storage, this)
Expand All @@ -59,9 +67,11 @@ block {
} with s

(* Return the reserves to the contracts. *)
[@inline] function get_reserves (const params : get_reserves_params; const s : full_dex_storage) : full_return is
[@inline] function get_reserves(
const params : get_reserves_params;
const s : full_dex_storage) : full_return is
block {
const pair : pair_info = case s.storage.pairs[params.token_id] of
const pair : pair_info = case s.storage.pairs[params.pair_id] of
None -> record [
token_a_pool = 0n;
token_b_pool = 0n;
Expand All @@ -78,7 +88,10 @@ block {
], s)

(* Set the dex function code to factory storage *)
[@inline] function set_dex_function (const idx : nat; const f : dex_func; const s : full_dex_storage) : full_dex_storage is
[@inline] function set_dex_function(
const idx : nat;
const f : dex_func;
const s : full_dex_storage) : full_dex_storage is
block {
case s.dex_lambdas[idx] of
Some(n) -> failwith("Dex/function-set")
Expand All @@ -87,7 +100,10 @@ block {
} with s

(* Set the token function code to factory storage *)
[@inline] function set_token_function (const idx : nat; const f : token_func; const s : full_dex_storage) : full_dex_storage is
[@inline] function set_token_function(
const idx : nat;
const f : token_func;
const s : full_dex_storage) : full_dex_storage is
block {
case s.token_lambdas[idx] of
Some(n) -> failwith("Dex/function-set")
Expand Down
Loading

0 comments on commit 25ce829

Please sign in to comment.