Skip to content

Commit

Permalink
pure token to token removed
Browse files Browse the repository at this point in the history
  • Loading branch information
KStasi committed Jun 22, 2021
1 parent 2f6c9f0 commit 939e488
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 422 deletions.
2 changes: 1 addition & 1 deletion contracts/main/TTDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
12 changes: 0 additions & 12 deletions contracts/partials/ITTDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ type token_to_token_route_params is
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 *)
receiver : address; (* tokens receiver *)
]

type invest_liquidity_params is
[@layout:comb]
record [
Expand All @@ -118,7 +107,6 @@ type divest_liquidity_params is
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 *)

Expand Down
7 changes: 3 additions & 4 deletions contracts/partials/TTDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ based on the argument type.
block {
const idx : nat = case p of
| InitializeExchange(n) -> 0n
| TokenToTokenPayment(n) -> 1n
| TokenToTokenRoutePayment(n) -> 2n
| InvestLiquidity(n) -> 3n
| DivestLiquidity(n) -> 4n
| TokenToTokenRoutePayment(n) -> 1n
| InvestLiquidity(n) -> 2n
| DivestLiquidity(n) -> 3n
end;
const res : return = case s.dex_lambdas[idx] of
Some(f) -> f(p, s.storage, this)
Expand Down
98 changes: 1 addition & 97 deletions contracts/partials/TTMethodDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -271,105 +271,12 @@ function initialize_exchange(
params.pair.token_b_type
) # operations;
}
| TokenToTokenPayment(n) -> skip
| TokenToTokenRoutePayment(n) -> skip
| InvestLiquidity(n) -> skip
| DivestLiquidity(n) -> skip
end
} with (operations, s)

(* Exchange tokens to tokens,
note: tokens should be approved before the operation *)
function token_to_token(
const p : dex_action;
const s : dex_storage;
const this : address) : return is
block {
var operations: list(operation) := list[
Tezos.transaction(
unit,
0mutez,
get_close_entrypoint(this)
)];
case p of
InitializeExchange(n) -> skip
| TokenToTokenRoutePayment(n) -> skip
| TokenToTokenPayment(params) -> {
if s.entered
then failwith("Dex/reentrancy")
else s.entered := True;

(* check preconditions *)
if params.pair.token_a_address = params.pair.token_b_address
and params.pair.token_a_id >= params.pair.token_b_id
then failwith("Dex/wrong-token-id") else skip;
if params.pair.token_a_address > params.pair.token_b_address
then failwith("Dex/wrong-pair") else skip;

(* get par info*)
const res : (pair_info * nat) = get_pair(params.pair, s);
const pair : pair_info = res.0;
const token_id : nat = res.1;

(* ensure there is liquidity *)
if pair.token_a_pool * pair.token_b_pool = 0n
then failwith("Dex/not-launched") else skip;
if params.amount_in = 0n (* non-zero amount of tokens exchanged *)
then failwith ("Dex/zero-amount-in") else skip;
if params.min_amount_out = 0n (* non-zero amount of tokens exchanged *)
then failwith ("Dex/zero-min-amount-out") else skip;


(* calculate amount out *)
const swap: swap_data = form_swap_data(
pair,
params.pair,
params.operation);
const from_in_with_fee : nat = params.amount_in * 997n;
const numerator : nat = from_in_with_fee * swap.to_.pool;
const denominator : nat = swap.from_.pool * 1000n + from_in_with_fee;

(* calculate swapped token amount *)
const out : nat = numerator / denominator;

(* ensure requirements *)
if out < params.min_amount_out (* minimal XTZ amount out is sutisfied *)
then failwith("Dex/wrong-min-out") else skip;
if out > swap.to_.pool / 3n (* the price impact isn't too high *)
then failwith("Dex/high-out") else skip;

(* update XTZ pool *)
swap.to_.pool := abs(swap.to_.pool - out);
swap.from_.pool := swap.from_.pool + params.amount_in;

const updated_pair : pair_info = form_pools(
swap.from_.pool,
swap.to_.pool,
pair.total_supply,
params.operation);
s.pairs[token_id] := updated_pair;

(* prepare operations to withdraw user's tokens and transfer XTZ *)
operations :=
(* from *)
typed_transfer(Tezos.sender, this, params.amount_in,
swap.from_.id,
swap.from_.token,
swap.from_.standard
) # operations;
operations :=
(* to *)
typed_transfer(this, params.receiver, out,
swap.to_.id,
swap.to_.token,
swap.to_.standard
) # operations;
}
| InvestLiquidity(n) -> skip
| DivestLiquidity(n) -> skip
end
} with (operations, s)

(* Intrenal functions for swap hops *)
function internal_token_to_token_swap(
const tmp : internal_swap_type;
Expand Down Expand Up @@ -449,14 +356,13 @@ function token_to_token_route(
)];
case p of
| InitializeExchange(n) -> skip
| TokenToTokenPayment(n) -> skip
| TokenToTokenRoutePayment(params) -> {
if s.entered
then failwith("Dex/reentrancy")
else s.entered := True;

(* validate input params *)
if List.size(params.swaps) < 2n (* non-zero amount of tokens exchanged *)
if List.size(params.swaps) < 1n (* non-zero amount of tokens exchanged *)
then failwith ("Dex/too-few-swaps") else skip;
if params.amount_in = 0n (* non-zero amount of tokens exchanged *)
then failwith ("Dex/zero-amount-in") else skip;
Expand Down Expand Up @@ -550,7 +456,6 @@ function invest_liquidity(
case p of
| InitializeExchange(n) -> skip
| TokenToTokenRoutePayment(n) -> skip
| TokenToTokenPayment(n) -> skip
| InvestLiquidity(params) -> {
if s.entered
then failwith("Dex/reentrancy")
Expand Down Expand Up @@ -660,7 +565,6 @@ function divest_liquidity(
)];
case p of
| InitializeExchange(token_amount) -> skip
| TokenToTokenPayment(n) -> skip
| TokenToTokenRoutePayment(n) -> skip
| InvestLiquidity(n) -> skip
| DivestLiquidity(params) -> {
Expand Down
8 changes: 2 additions & 6 deletions storage/TTFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ module.exports.dexFunctions = [
},
{
index: 1,
name: "token_to_token",
},
{
index: 2,
name: "token_to_token_route",
},
{
index: 3,
index: 2,
name: "invest_liquidity",
},
{
index: 4,
index: 3,
name: "divest_liquidity",
},
];
Expand Down
38 changes: 22 additions & 16 deletions test/helpers/ttdexFA2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,29 @@ export class TTDex extends TokenFA2 {
);
}
}
const swaps = [
{
pair: {
token_a_address: tokenAAddress,
token_b_address: tokenBAddress,
token_a_id: tokenAid,
token_b_id: tokenBid,
token_a_type: {
[standard.toLowerCase() == "mixed"
? "fa2"
: standard.toLowerCase()]: null,
},
token_b_type: {
[standard.toLowerCase() == "mixed"
? "fa12"
: standard.toLowerCase()]: null,
},
},
operation: { [opType]: null },
},
];
const operation = await this.contract.methods
.use(
"tokenToTokenPayment",
tokenAAddress,
tokenAid,
standard.toLowerCase() == "mixed" ? "fa2" : standard.toLowerCase(),
null,
tokenBAddress,
tokenBid,
standard.toLowerCase() == "mixed" ? "fa12" : standard.toLowerCase(),
null,
opType,
null,
amountIn,
minAmountOut,
receiver
)
.use("tokenToTokenRoutePayment", swaps, amountIn, minAmountOut, receiver)
.send();
await confirmOperation(tezos, operation.hash);
return operation;
Expand Down
Loading

0 comments on commit 939e488

Please sign in to comment.