diff --git a/contracts/partials/ITTDex.ligo b/contracts/partials/ITTDex.ligo index d6e291f0..3eefe12e 100644 --- a/contracts/partials/ITTDex.ligo +++ b/contracts/partials/ITTDex.ligo @@ -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 *) ] @@ -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 *) diff --git a/contracts/partials/TTMethodDex.ligo b/contracts/partials/TTMethodDex.ligo index cd305bbb..d8fb092d 100644 --- a/contracts/partials/TTMethodDex.ligo +++ b/contracts/partials/TTMethodDex.ligo @@ -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; @@ -523,7 +513,7 @@ 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 *) @@ -531,7 +521,7 @@ function invest_liquidity( 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 *) diff --git a/test/helpers/ttdexFA2.ts b/test/helpers/ttdexFA2.ts index 45d195cf..e5db4b03 100644 --- a/test/helpers/ttdexFA2.ts +++ b/test/helpers/ttdexFA2.ts @@ -294,6 +294,7 @@ 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, @@ -301,8 +302,7 @@ export class TTDex extends TokenFA2 { standard.toLowerCase() == "mixed" ? "fa12" : standard.toLowerCase(), null, tokenAAmount, - tokenBAmount, - minShares + tokenBAmount ) .send(); await confirmOperation(tezos, operation.hash);