Skip to content

Commit

Permalink
slipage for token to token
Browse files Browse the repository at this point in the history
  • Loading branch information
KStasi committed Jul 15, 2021
1 parent 3c24041 commit d319ff5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
11 changes: 10 additions & 1 deletion contracts/partials/ITTDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,19 @@ type token_to_token_route_params is
receiver : address; (* tokens receiver *)
]

type initialize_params is
[@layout:comb]
record [
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 invest_liquidity_params is
[@layout:comb]
record [
pair : tokens_info; (* exchange pair info *)
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 *)
]
Expand All @@ -108,7 +117,7 @@ type divest_liquidity_params is
]

type dex_action is
| AddPair of invest_liquidity_params (* sets initial liquidity *)
| AddPair of initialize_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 *)
Expand Down
24 changes: 7 additions & 17 deletions contracts/partials/TTMethodDex.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -483,29 +483,19 @@ function invest_liquidity(
if pair.token_a_pool * pair.token_b_pool = 0n
then failwith("Dex/not-launched") else skip;

(* calculate purchased tokens *)
const shares_a_purchased : nat =
params.token_a_in * pair.total_supply / pair.token_a_pool;
const shares_b_purchased : nat =
params.token_b_in * pair.total_supply / pair.token_b_pool;
const shares_purchased : nat =
if shares_a_purchased < shares_b_purchased
then shares_a_purchased
else shares_b_purchased;

(* ensure purchsed shares satisfy required minimum *)
if shares_purchased = 0n
if params.shares = 0n
then failwith("Dex/wrong-params") else skip;

(* calculate tokens to be withdrawn *)
const tokens_a_required : nat =
shares_purchased * pair.token_a_pool / pair.total_supply;
if shares_purchased * pair.token_a_pool >
params.shares * pair.token_a_pool / pair.total_supply;
if params.shares * pair.token_a_pool >
tokens_a_required * pair.total_supply
then tokens_a_required := tokens_a_required + 1n else skip;
const tokens_b_required : nat =
shares_purchased * pair.token_b_pool / pair.total_supply;
if shares_purchased * pair.token_b_pool >
params.shares * pair.token_b_pool / pair.total_supply;
if params.shares * pair.token_b_pool >
tokens_b_required * pair.total_supply
then tokens_b_required := tokens_b_required + 1n else skip;

Expand All @@ -523,15 +513,15 @@ function invest_liquidity(
const share : nat = account.balance;

(* update user's shares *)
account.balance := share + shares_purchased;
account.balance := share + params.shares;
s.ledger[(Tezos.sender, token_id)] := account;

(* update reserves *)
pair.token_a_pool := pair.token_a_pool + tokens_a_required;
pair.token_b_pool := pair.token_b_pool + tokens_b_required;

(* update total number of shares *)
pair.total_supply := pair.total_supply + shares_purchased;
pair.total_supply := pair.total_supply + params.shares;
s.pairs[token_id] := pair;

(* prepare operations to get initial liquidity *)
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/ttdexFA2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@ export class TTDex extends TokenFA2 {
"invest",
pair.token_a_address,
pair.token_a_id,
minShares,
standard.toLowerCase() == "mixed" ? "fa2" : standard.toLowerCase(),
null,
pair.token_b_address,
pair.token_b_id,
standard.toLowerCase() == "mixed" ? "fa12" : standard.toLowerCase(),
null,
tokenAAmount,
tokenBAmount,
minShares
tokenBAmount
)
.send();
await confirmOperation(tezos, operation.hash);
Expand Down

0 comments on commit d319ff5

Please sign in to comment.