From 2433a18b8cd29c2e33962eb3d6ab807ecea6c7d1 Mon Sep 17 00:00:00 2001 From: kstasi Date: Fri, 25 Jun 2021 12:09:11 +0300 Subject: [PATCH 1/3] fix addPair & migrations --- contracts/partials/TTMethodDex.ligo | 15 +++++ migrations/2_deploy_token_to_token_dex.js | 74 +++++++---------------- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/contracts/partials/TTMethodDex.ligo b/contracts/partials/TTMethodDex.ligo index 2db8e171..cd305bbb 100644 --- a/contracts/partials/TTMethodDex.ligo +++ b/contracts/partials/TTMethodDex.ligo @@ -185,6 +185,17 @@ function typed_transfer( contract_address) end; +(* Helper function to transfer the asset based on its standard *) +function check_token_id( + const token_id : nat; + const standard: token_type) : unit is + case standard of + Fa12 -> if token_id = 0n + then unit + else (failwith("Dex/non-zero-token-id") : unit) + | Fa2 -> unit + end; + #include "../partials/TTMethodFA2.ligo" (* Initialize exchange after the previous liquidity was drained *) @@ -207,6 +218,10 @@ function initialize_exchange( if params.pair.token_a_address > params.pair.token_b_address then failwith("Dex/wrong-pair") else skip; + (* check fa1.2 token ids *) + check_token_id(params.pair.token_a_id, params.pair.token_a_type); + check_token_id(params.pair.token_b_id, params.pair.token_b_type); + (* read pair info*) const res : (pair_info * nat) = get_pair(params.pair, s); const pair : pair_info = res.0; diff --git a/migrations/2_deploy_token_to_token_dex.js b/migrations/2_deploy_token_to_token_dex.js index 9608a4dc..95bc84dd 100644 --- a/migrations/2_deploy_token_to_token_dex.js +++ b/migrations/2_deploy_token_to_token_dex.js @@ -104,21 +104,6 @@ module.exports = async (deployer, network, accounts) => { .approve(dexInstance.address.toString(), initialTokenAmount) .send(); await operation.confirmation(); - - operation = await dex.methods - .use( - "addPair", - ordered - ? token0Instance.address.toString() - : token1Instance.address.toString(), - ordered - ? token1Instance.address.toString() - : token0Instance.address.toString(), - initialTokenAmount, - initialTokenAmount - ) - .send(); - await operation.confirmation(); } else { let operation = await token0Instance.methods .update_operators([ @@ -150,44 +135,27 @@ module.exports = async (deployer, network, accounts) => { .send(); } await operation.confirmation(); - - if (standard === "MIXED") { - operation = await dex.methods - .use( - "addPair", - ordered - ? token0Instance.address.toString() - : token1Instance.address.toString(), - 0, - "fa12", - ordered - ? token1Instance.address.toString() - : token0Instance.address.toString(), - 0, - "fa2".initialTokenAmount, - initialTokenAmount - ) - .send(); - } else { - operation = await dex.methods - .use( - "addPair", - ordered - ? token0Instance.address.toString() - : token1Instance.address.toString(), - 0, - standard.toLocaleLowerCase(), - ordered - ? token1Instance.address.toString() - : token0Instance.address.toString(), - 0, - standard.toLocaleLowerCase(), - initialTokenAmount, - initialTokenAmount - ) - .send(); - } - await operation.confirmation(); } + operation = await dex.methods + .use( + "addPair", + ordered + ? token0Instance.address.toString() + : token1Instance.address.toString(), + 0, + standard == "MIXED" ? "fa2" : standard.toLocaleLowerCase(), + null, + ordered + ? token1Instance.address.toString() + : token0Instance.address.toString(), + 0, + standard == "MIXED" ? "fa12" : standard.toLocaleLowerCase(), + null, + initialTokenAmount, + initialTokenAmount + ) + .send(); + + await operation.confirmation(); } }; From d319ff5d2e0a2f07ecd51280dbe2efad46a145c1 Mon Sep 17 00:00:00 2001 From: kstasi Date: Thu, 15 Jul 2021 11:24:31 +0300 Subject: [PATCH 2/3] slipage for token to token --- contracts/partials/ITTDex.ligo | 11 ++++++++++- contracts/partials/TTMethodDex.ligo | 24 +++++++----------------- test/helpers/ttdexFA2.ts | 4 ++-- 3 files changed, 19 insertions(+), 20 deletions(-) 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); From 07b0b3a3d1a60bc9cab73c3e314f8ac640c96829 Mon Sep 17 00:00:00 2001 From: kstasi Date: Sat, 17 Jul 2021 15:12:53 +0300 Subject: [PATCH 3/3] fix test --- test/InvestTTLiquidity.spec.ts | 19 +++++++++++++++---- test/helpers/ttdexFA2.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/test/InvestTTLiquidity.spec.ts b/test/InvestTTLiquidity.spec.ts index 5398a80c..593ca2a7 100644 --- a/test/InvestTTLiquidity.spec.ts +++ b/test/InvestTTLiquidity.spec.ts @@ -142,6 +142,10 @@ contract("InvestTTLiquidity()", function () { it("success in case of min shares of 1", async function () { const pairAddress = context.dex.contract.address; + const maxTokenAAmount = 1000; + const maxTokenBAmount = 1000; + const tokenAAmount = 1; + const tokenBAmount = 100; await context.tokens[0].updateStorage({ ledger: [aliceAddress, pairAddress], }); @@ -170,7 +174,12 @@ contract("InvestTTLiquidity()", function () { pairAddress ].balance; const initDexPair = context.dex.storage.pairs[0]; - await context.dex.investLiquidity("0", tokenAAmount, tokenBAmount, 1); + await context.dex.investLiquidity( + "0", + maxTokenAAmount, + maxTokenBAmount, + 1 + ); await context.tokens[0].updateStorage({ ledger: [aliceAddress, pairAddress], }); @@ -321,8 +330,10 @@ contract("InvestTTLiquidity()", function () { describe("Test purchased shares", () => { before(async () => {}); - it("success in case of more then 0 tokens purchesed", async function () { + it("success in case of more then 0 shares purchesed", async function () { const pairAddress = context.dex.contract.address; + const tokenAAmount = 1000; + const tokenBAmount = 100000; await context.tokens[0].updateStorage({ ledger: [aliceAddress, pairAddress], }); @@ -351,7 +362,7 @@ contract("InvestTTLiquidity()", function () { pairAddress ].balance; const initDexPair = context.dex.storage.pairs[0]; - await context.dex.investLiquidity("0", tokenAAmount, tokenBAmount, 1); + await context.dex.investLiquidity("0", tokenAAmount, tokenBAmount, 1000); await context.tokens[0].updateStorage({ ledger: [aliceAddress, pairAddress], }); @@ -416,7 +427,7 @@ contract("InvestTTLiquidity()", function () { 1, bobAddress ); - await rejects(context.dex.investLiquidity("0", 1, 1, 1), (err) => { + await rejects(context.dex.investLiquidity("0", 1, 1, 0), (err) => { ok(err.message == "Dex/wrong-params", "Error message mismatch"); return true; }); diff --git a/test/helpers/ttdexFA2.ts b/test/helpers/ttdexFA2.ts index e5db4b03..754c6589 100644 --- a/test/helpers/ttdexFA2.ts +++ b/test/helpers/ttdexFA2.ts @@ -294,13 +294,13 @@ 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, + minShares, tokenAAmount, tokenBAmount )