From 3e05bca4c32ee7e7a0a9b2850a08ad01949ee1bf Mon Sep 17 00:00:00 2001 From: Attens1423 Date: Thu, 19 Oct 2023 10:45:42 +0800 Subject: [PATCH 1/4] fix lpRate+mtRate --- contracts/DODOV3MM/D3Pool/D3Trading.sol | 12 ++++++++---- contracts/DODOV3MM/lib/MakerTypes.sol | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/contracts/DODOV3MM/D3Pool/D3Trading.sol b/contracts/DODOV3MM/D3Pool/D3Trading.sol index c97f857..fc3ea8a 100644 --- a/contracts/DODOV3MM/D3Pool/D3Trading.sol +++ b/contracts/DODOV3MM/D3Pool/D3Trading.sol @@ -111,7 +111,9 @@ contract D3Trading is D3Funding { // external call & swap callback IDODOSwapCallback(msg.sender).d3MMSwapCallBack(fromToken, fromAmount, data); // transfer mtFee to maintainer - _transferOut(state._MAINTAINER_, toToken, mtFee); + if(mtFee > 0) { + _transferOut(state._MAINTAINER_, toToken, mtFee); + } require( IERC20(fromToken).balanceOf(address(this)) - state.balances[fromToken] >= fromAmount, @@ -151,7 +153,9 @@ contract D3Trading is D3Funding { // external call & swap callback IDODOSwapCallback(msg.sender).d3MMSwapCallBack(fromToken, payFromAmount, data); // transfer mtFee to maintainer - _transferOut(state._MAINTAINER_, toToken, mtFee); + if(mtFee > 0 ) { + _transferOut(state._MAINTAINER_, toToken, mtFee); + } require( IERC20(fromToken).balanceOf(address(this)) - state.balances[fromToken] >= payFromAmount, @@ -199,7 +203,7 @@ contract D3Trading is D3Funding { uint256 mtFeeRate = D3State.fromTokenMMInfo.mtFeeRate + D3State.toTokenMMInfo.mtFeeRate; mtFee = DecimalMath.mulFloor(receiveToAmount, mtFeeRate); - return (payFromAmount, receiveToAmount - swapFee, vusdAmount, swapFee, mtFee); + return (payFromAmount, receiveToAmount - mtFee, vusdAmount, swapFee, mtFee); } /// @notice user could query sellToken result deducted swapFee, assign toAmount @@ -222,7 +226,7 @@ contract D3Trading is D3Funding { swapFee = DecimalMath.mulFloor(toAmount, swapFeeRate); uint256 mtFeeRate = D3State.fromTokenMMInfo.mtFeeRate + D3State.toTokenMMInfo.mtFeeRate; mtFee = DecimalMath.mulFloor(toAmount, mtFeeRate); - toAmountWithFee = toAmount + swapFee; + toAmountWithFee = toAmount + mtFee; } require(toAmountWithFee <= state.balances[toToken], Errors.BALANCE_NOT_ENOUGH); diff --git a/contracts/DODOV3MM/lib/MakerTypes.sol b/contracts/DODOV3MM/lib/MakerTypes.sol index b80db18..7c72431 100644 --- a/contracts/DODOV3MM/lib/MakerTypes.sol +++ b/contracts/DODOV3MM/lib/MakerTypes.sol @@ -78,7 +78,7 @@ library MakerTypes { uint256 bidDownRate = priceInfo & 0xffff; // swap fee rate standarlize - swapFee = swapFeeRate * (10 ** 14) + mtFeeRate; + swapFee = swapFeeRate * (10 ** 14); uint256 swapFeeSpread = DecimalMath.mul(midPriceWithDecimal, swapFee); // ask price standarlize From 46c51b43c266544901d50cd6b0f8763abd8a1224 Mon Sep 17 00:00:00 2001 From: traceurl <25892474+traceurl@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:42:36 +0800 Subject: [PATCH 2/4] fix tests --- contracts/DODOV3MM/D3Pool/D3Maker.sol | 2 +- contracts/DODOV3MM/lib/MakerTypes.sol | 2 +- test/DODOV3MM/D3MM/D3MM.t.sol | 6 +- test/DODOV3MM/D3MM/D3Maker.t.sol | 40 ++++----- test/DODOV3MM/D3MM/D3Trading.t.sol | 123 +++++++++++++++++--------- test/DODOV3MM/lib/MakerType.t.sol | 27 +++--- test/DODOV3MM/lib/PMMRangeOrder.t.sol | 50 +++++------ test/DODOV3MM/periphery/D3Proxy.t.sol | 10 +-- 8 files changed, 151 insertions(+), 109 deletions(-) diff --git a/contracts/DODOV3MM/D3Pool/D3Maker.sol b/contracts/DODOV3MM/D3Pool/D3Maker.sol index ab3e14c..bdd05cc 100644 --- a/contracts/DODOV3MM/D3Pool/D3Maker.sol +++ b/contracts/DODOV3MM/D3Pool/D3Maker.sol @@ -51,7 +51,7 @@ contract D3Maker is InitializableOwnable { tokenMMInfo.bidUpPrice, tokenMMInfo.bidDownPrice, tokenMMInfo.swapFeeRate - ) = MakerTypes.parseAllPrice(priceInfo, mtFeeRate); + ) = MakerTypes.parseAllPrice(priceInfo); // lpfee add mtFee tokenMMInfo.mtFeeRate = mtFeeRate; uint64 amountInfo = state.tokenMMInfoMap[token].amountInfo; diff --git a/contracts/DODOV3MM/lib/MakerTypes.sol b/contracts/DODOV3MM/lib/MakerTypes.sol index 7c72431..b5f8a32 100644 --- a/contracts/DODOV3MM/lib/MakerTypes.sol +++ b/contracts/DODOV3MM/lib/MakerTypes.sol @@ -63,7 +63,7 @@ library MakerTypes { amountWithDecimal = bidAmount * (10 ** bidAmountDecimal); } - function parseAllPrice(uint80 priceInfo, uint256 mtFeeRate) + function parseAllPrice(uint80 priceInfo) internal pure returns (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) diff --git a/test/DODOV3MM/D3MM/D3MM.t.sol b/test/DODOV3MM/D3MM/D3MM.t.sol index 08f4199..7688bc9 100644 --- a/test/DODOV3MM/D3MM/D3MM.t.sol +++ b/test/DODOV3MM/D3MM/D3MM.t.sol @@ -89,10 +89,10 @@ contract D3MMTest is TestContext { //console.log(receiveToToken); assertEq(beforeBalance2 - afterBalance2, 1 ether); assertEq(afterBalance3 - beforeBalance3, receiveToToken); - assertEq(afterBalance3 - beforeBalance3, 11959881980233813532); // 11.9, suppose 12 + assertEq(afterBalance3 - beforeBalance3, 11978524479259449453); // 11.9, suppose 12 (,,,,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); assertEq(cumulativeAsk, 0); - assertEq(cumulativeBid, 11988727083375218064); + assertEq(cumulativeBid, 11990650771415848322); uint256 allFlag = d3MM.allFlag(); assertEq(allFlag, 20); @@ -121,6 +121,6 @@ contract D3MMTest is TestContext { // check price (,uint256 receiveAmount , , ,) = d3MM.querySellTokens(address(token2), address(token3), 1 ether); - assertEq(receiveAmount, 996640179180765387); // 0.99, suppose 1 + assertEq(receiveAmount, 998194397280843196); // 0.99, suppose 1 } } \ No newline at end of file diff --git a/test/DODOV3MM/D3MM/D3Maker.t.sol b/test/DODOV3MM/D3MM/D3Maker.t.sol index 8a3f924..6a6cb9c 100644 --- a/test/DODOV3MM/D3MM/D3Maker.t.sol +++ b/test/DODOV3MM/D3MM/D3Maker.t.sol @@ -69,9 +69,9 @@ contract MakerTest is TestContext { (tokenMMInfo, tokenIndex) = d3Maker.getTokenMMInfoForPool(address(token1)); assertEq(tokenMMInfo.askUpPrice, 130156 * (10 ** 16)); - assertEq(tokenMMInfo.askDownPrice, 130104 * (10 ** 16)); + assertEq(tokenMMInfo.askDownPrice, 130078 * (10 ** 16)); assertEq(tokenMMInfo.bidUpPrice, 770000770000771); //1298.7 - assertEq(tokenMMInfo.bidDownPrice, 769846646548008); // 1298.96 + assertEq(tokenMMInfo.bidDownPrice, 769692584781639); // 1299.22 assertEq(tokenMMInfo.askAmount, 30 * (10 ** 18)); assertEq(tokenMMInfo.bidAmount, 30 * (10 ** 18)); @@ -83,9 +83,9 @@ contract MakerTest is TestContext { (tokenMMInfo, tokenIndex) = d3Maker.getTokenMMInfoForPool(address(token4)); assertEq(tokenMMInfo.askUpPrice, 130156 * (10 ** 16)); - assertEq(tokenMMInfo.askDownPrice, 130104 * (10 ** 16)); + assertEq(tokenMMInfo.askDownPrice, 130078 * (10 ** 16)); assertEq(tokenMMInfo.bidUpPrice, 770000770000771); //1298.7 - assertEq(tokenMMInfo.bidDownPrice, 769846646548008); // 1298.96 + assertEq(tokenMMInfo.bidDownPrice, 769692584781639); // 1299.22 assertEq(tokenMMInfo.askAmount, 30 * (10 ** 18)); assertEq(tokenMMInfo.bidAmount, 30 * (10 ** 18)); @@ -127,9 +127,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token1)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 flag = d3MM.getTokenFlag(address(token1)); assertEq(flag, 0); @@ -148,9 +148,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token2)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 flag = d3MM.getTokenFlag(address(token1)); assertEq(flag, 0); @@ -173,9 +173,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token4)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 flag = d3MM.getTokenFlag(address(token1)); assertEq(flag, 0); @@ -220,9 +220,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token1)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 flag = d3MM.getTokenFlag(address(token1)); assertEq(flag, 0); @@ -245,9 +245,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token2)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 flag = d3MM.getTokenFlag(address(token1)); assertEq(flag, 0); @@ -287,9 +287,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token3)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 } // set 2 token @@ -309,9 +309,9 @@ contract MakerTest is TestContext { // check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token4)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 } // prices and slots not match @@ -414,9 +414,9 @@ contract MakerTest is TestContext { //check (Types.TokenMMInfo memory tokenMMInfo,) = d3Maker.getTokenMMInfoForPool(address(token1)); assertEq(tokenMMInfo.askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(tokenMMInfo.askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(tokenMMInfo.askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(tokenMMInfo.bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(tokenMMInfo.bidDownPrice, 35896723117375); // 1/27857.696 + assertEq(tokenMMInfo.bidDownPrice, 35889539462559); // 1/27863.272 assertEq(tokenMMInfo.askAmount, 40 * (10 ** 18)); assertEq(tokenMMInfo.bidAmount, 40 * (10 ** 18)); diff --git a/test/DODOV3MM/D3MM/D3Trading.t.sol b/test/DODOV3MM/D3MM/D3Trading.t.sol index bd4552a..6573d25 100644 --- a/test/DODOV3MM/D3MM/D3Trading.t.sol +++ b/test/DODOV3MM/D3MM/D3Trading.t.sol @@ -31,11 +31,11 @@ contract D3TradingTest is TestContext { function testReadFunctions() public { (uint256 askDownPrice, uint256 askUpPrice, uint256 bidDownPrice, uint256 bidUpPrice, uint256 swapFee) = d3MM.getTokenMMPriceInfoForRead(address(token2)); - assertEq(askDownPrice, 12009600000000000000); + assertEq(askDownPrice, 12007200000000000000); assertEq(askUpPrice, 12027600000000000000); - assertEq(bidDownPrice, 83400053376034161); + assertEq(bidDownPrice, 83383363351344140); assertEq(bidUpPrice, 83458521115005843); - assertEq(swapFee, 800000000000000); + assertEq(swapFee, 600000000000000); //console.log(askDownPrice); //console.log(askUpPrice); @@ -82,7 +82,7 @@ contract D3TradingTest is TestContext { //console.log(receiveToToken); assertEq(beforeBalance2 - afterBalance2, 1 ether); assertEq(afterBalance3 - beforeBalance3, receiveToToken); - assertEq(afterBalance3 - beforeBalance3, 11959881980233813532); + assertEq(afterBalance3 - beforeBalance3, 11978524479259449453); } function testNormalBuyTokens() public { @@ -115,7 +115,7 @@ contract D3TradingTest is TestContext { //console.log(afterBalance3 - beforeBalance3); assertEq(beforeBalance2 - afterBalance2, receiveToToken); - assertEq(beforeBalance2 - afterBalance2, 83601350012314569); // 0.08 + assertEq(beforeBalance2 - afterBalance2, 83468096707748715); // 0.08 assertEq(afterBalance3 - beforeBalance3, 1 ether); } @@ -210,7 +210,7 @@ contract D3TradingTest is TestContext { (, , , , , cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeBid, 1002401946807995096); // 1.002 suppose 1 vusd + assertEq(cumulativeBid, 1001001236331550199); // 1.001 suppose 1 vusd //console.log("cumualativeBid:", cumulativeBid); beforeBalance2 = afterBalance2; @@ -234,7 +234,7 @@ contract D3TradingTest is TestContext { //console.log(receiveToToken); assertEq(beforeBalance2 - afterBalance2, 1000); assertEq(afterBalance3 - beforeBalance3, receiveToToken); - assertEq(afterBalance3 - beforeBalance3, 11959586831563309114); // suppose 12 + assertEq(afterBalance3 - beforeBalance3, 11978141873757971225); // suppose 12 } function testMinMaxRevert() public { @@ -377,14 +377,14 @@ contract D3TradingTest is TestContext { uint256 afterBalance2 = token2.balanceOf(user1); uint256 afterBalanceEx = tokenEx.balanceOf(user1); - assertEq(afterBalance2 - beforeBalance2, 1996802085427539929); // 1.99 near 2 - assertEq(beforeBalanceEx - afterBalanceEx, 1001602215640904029); // 1.00 near 1 + assertEq(afterBalance2 - beforeBalance2, 1998398178385100244); // 1.99 near 2 + assertEq(beforeBalanceEx - afterBalanceEx, 1000801105666936279); // 1.00 near 1 //console.log(afterBalance2 - beforeBalance2); //console.log(beforeBalanceEx - afterBalanceEx); uint256 token2Res = d3MM.getTokenReserve(address(token2)); //console.log("token2Res:", token2Res); - assertEq(token2Res, 11002398554762593269); // 11.0 > borrow 10 + assertEq(token2Res, 11000802302535914122); // 11.0 > borrow 10 uint256 colR = d3Vault.getCollateralRatio(address(d3MM)); //console.log(colR); assertEq(colR, type(uint256).max); @@ -449,11 +449,11 @@ contract D3TradingTest is TestContext { (uint256 askDownPrice, uint256 askUpPrice, uint256 bidDownPrice, uint256 bidUpPrice, uint256 swapFee) = d3MM.getTokenMMPriceInfoForRead(address(token1)); - assertEq(askDownPrice, 30455502800000000000000); + assertEq(askDownPrice, 30449419000000000000000); assertEq(askUpPrice, 30723190000000000000000); - assertEq(bidDownPrice, 32913686897337); + assertEq(bidDownPrice, 32907097570631); assertEq(bidUpPrice, 33206253003091); - assertEq(swapFee, 1200000000000000); + assertEq(swapFee, 1000000000000000); //console.log(askDownPrice); //console.log(askUpPrice); @@ -467,11 +467,11 @@ contract D3TradingTest is TestContext { (askDownPrice, askUpPrice, bidDownPrice, bidUpPrice, swapFee) = d3MM.getTokenMMPriceInfoForRead(address(token2)); - assertEq(askDownPrice, 999999960000000000); + assertEq(askDownPrice, 999800000000000000); assertEq(askUpPrice, 1000799800000000000); - assertEq(bidDownPrice, 1000400120032008002); + assertEq(bidDownPrice, 1000200040008001601); assertEq(bidUpPrice, 1001201241249250852); - assertEq(swapFee, 200000000000000); + assertEq(swapFee, 0); (,,kask, kbid,,) = d3MM.getTokenMMOtherInfoForRead(address(token2)); assertEq(kask, 0); @@ -506,13 +506,13 @@ contract D3TradingTest is TestContext { block.timestamp + 1000 ); // token2's balance max 100 ether - assertEq(receiveToToken, 99860000000000000000); + assertEq(receiveToToken, 99960000000000000000); (,,, ,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeAsk, 100 ether); + assertEq(cumulativeAsk, 100060000000000000000); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeBid, 303824951342633192532); + assertEq(cumulativeBid, 303885789307408018586); assertEq(cumulativeAsk, 0); token2.mint(address(d3MM), 400 ether); @@ -530,14 +530,14 @@ contract D3TradingTest is TestContext { block.timestamp + 1000 ); - assertEq(receiveToToken, 303399567233725215991); + assertEq(receiveToToken, 303824958558415332845); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeAsk, 403824922124699795704); + assertEq(cumulativeAsk, 404188905095588617491); //console.log(cumulativeAsk - 100 ether); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeBid, 607649861314336103249); + assertEq(cumulativeBid, 607771537173258008457); //console.log(cumulativeBid - 303823704036065356124); assertEq(cumulativeAsk, 0); } @@ -564,14 +564,14 @@ contract D3TradingTest is TestContext { block.timestamp + 1000 ); - assertEq(receiveToToken, 327757); //0.00327, near 0.00329 + assertEq(receiveToToken, 328216); //0.00328, near 0.00329 (,,, ,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeAsk, 3282160000000000); //0.00328 ether + assertEq(cumulativeAsk, 3285440000000000); //0.00328 ether //console.log(cumulativeAsk); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeBid, 99960003999999999900); + assertEq(cumulativeBid, 99979999999999999900); //console.log(cumulativeBid); assertEq(cumulativeAsk, 0); @@ -628,15 +628,15 @@ contract D3TradingTest is TestContext { //uint256 balance2 = d3MM.getTokenReserve(address(token1)); //console.log("pool balance token1:", balance2); - assertEq(receiveToToken, 99860000); + assertEq(receiveToToken, 99960000); (,,, , cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); //assertEq(cumulativeAsk, 3282160000000000); //0.00328 ether - assertEq(cumulativeAsk, 1000000000000000000); + assertEq(cumulativeAsk, 1000600000000000000); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); //assertEq(cumulativeBid, 99960003999999999900); - assertEq(cumulativeBid, 39984001599999999960000); + assertEq(cumulativeBid, 39991999999999999960000); //assertEq(cumulativeBid, ); assertEq(cumulativeAsk, 0); } @@ -665,14 +665,14 @@ contract D3TradingTest is TestContext { ); //console.log(payFromToken); - assertEq(payFromToken, 3295); + assertEq(payFromToken, 3291); (,,, ,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeAsk, 1001400000000000000); // 1 ether + assertEq(cumulativeAsk, 1001000000000000000); // 1 ether //console.log(cumulativeAsk); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeBid, 1001399959944000000); // 1 ether + assertEq(cumulativeBid, 1000199920000000000); // 1 ether //console.log(cumulativeBid); assertEq(cumulativeAsk, 0); } @@ -729,14 +729,14 @@ contract D3TradingTest is TestContext { ); //console.log(payFromToken); - assertEq(payFromToken, 305103461545737399619); + assertEq(payFromToken, 304676950368205693279); (,,, ,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeAsk, 10014000000000000); // 0.01 ether + assertEq(cumulativeAsk, 10010000000000000); // 0.01 ether //console.log(cumulativeAsk); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token2)); - assertEq(cumulativeBid, 304981432365257566465); // 304 ether + assertEq(cumulativeBid, 304616014978132051934); // 304 ether //console.log(cumulativeBid); assertEq(cumulativeAsk, 0); } @@ -815,14 +815,14 @@ contract D3TradingTest is TestContext { block.timestamp + 1000 ); //console.log(payFromToken); - assertEq(payFromToken, 771702); //0.077 + assertEq(payFromToken, 770475); //0.077 (,,, ,uint256 cumulativeAsk, uint256 cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token24)); - assertEq(cumulativeAsk, 10016000000000000000); // 10 ether + assertEq(cumulativeAsk, 10012000000000000000); // 10 ether //console.log(cumulativeAsk); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeBid, 10024079484791858627); // 10 ether + assertEq(cumulativeBid, 10010102042080981350); // 10 ether //console.log(cumulativeBid); assertEq(cumulativeAsk, 0); @@ -839,16 +839,57 @@ contract D3TradingTest is TestContext { block.timestamp + 1000 ); //console.log(receiveToToken); - assertEq(receiveToToken, 12_9580_23095_55504_85733_37600); //12.9 token24 + assertEq(receiveToToken, 12978504141578036786079600 ); //12.9 token24 (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token24)); - assertEq(cumulativeAsk, 22994789158208181664); // 22 ether, this time = 12.9 ether + assertEq(cumulativeAsk, 23006084578722788330); // 22 ether, this time = 12.9 ether //console.log(cumulativeAsk); assertEq(cumulativeBid, 0); (,,, ,cumulativeAsk, cumulativeBid) = d3MM.getTokenMMOtherInfoForRead(address(token1)); - assertEq(cumulativeBid, 23013536496933983488); // 23 ether, this time = 13 ether + assertEq(cumulativeBid, 23002016635982823165); // 23 ether, this time = 13 ether //console.log(cumulativeBid); assertEq(cumulativeAsk, 0); } - + + function testExtraFee() public { + vm.prank(owner); + feeRateModel.setFeeRate(0); + + token2ChainLinkOracle.feedData(212 * 1e18); + token3ChainLinkOracle.feedData(1 * 1e18); + + address[] memory tokens = new address[](2); + uint64[] memory amounts = new uint64[](2); + uint80[] memory prices = new uint80[](2); + uint32[] memory ks = new uint32[](2); + + tokens[0] = address(token3); //usdt + tokens[1] = address(token2); //bnb + amounts[0] = stickAmount(60000, 18, 60000, 18); + amounts[1] = stickAmount(400, 18, 60000, 18); + prices[0] = stickPrice(10000, 14, 1, 2, 2); + prices[1] = stickPrice(21195, 16, 2, 100, 100); + ks[0] = stickKs(1, 1); + ks[1] = stickKs(1, 1); + + vm.startPrank(maker); + d3MakerWithPool.setTokensAmounts(tokens, amounts); + d3MakerWithPool.setTokensKs(tokens, ks); + d3MakerWithPool.setTokensPrice(tokens, prices); + vm.stopPrank(); + + Types.TokenMMInfo memory tokenMMInfo; + uint256 tokenIndex; + (tokenMMInfo, tokenIndex) = d3MakerWithPool.getTokenMMInfoForPool(address(token3)); + console.log(tokenMMInfo.askUpPrice); + console.log(tokenMMInfo.askDownPrice); + console.log(tokenMMInfo.bidUpPrice); + console.log(tokenMMInfo.bidDownPrice); + + console.log("query"); + (uint256 fromAmount, uint256 toAmount, uint256 vusd, ,) = d3MM.querySellTokens(address(token2), address(token3), 10000); + console.log(fromAmount); + console.log(toAmount); + console.log(vusd); + } } diff --git a/test/DODOV3MM/lib/MakerType.t.sol b/test/DODOV3MM/lib/MakerType.t.sol index a1f257f..f225679 100644 --- a/test/DODOV3MM/lib/MakerType.t.sol +++ b/test/DODOV3MM/lib/MakerType.t.sol @@ -22,12 +22,12 @@ contract MakerTypesHelper { return bidAmount; } - function parseAllPrice(uint80 priceInfo, uint256 tokenDecimal, uint256 mtFeeRate) + function parseAllPrice(uint80 priceInfo) external pure returns (uint256 , uint256 , uint256 , uint256 , uint256) { - (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = MakerTypes.parseAllPrice(priceInfo, mtFeeRate); + (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = MakerTypes.parseAllPrice(priceInfo); return (askUpPrice, askDownPrice, bidUpPrice, bidDownPrice, swapFee); } @@ -63,28 +63,29 @@ contract MakerTypeTest is TestContext { function testParseAllPrice() public { uint80 priceInfo = stickPrice(27880, 18, 6, 12, 10); - (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = makerTypes.parseAllPrice(priceInfo, 18, (2*10**14)); //0.02% + (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = makerTypes.parseAllPrice(priceInfo); //0.02% assertEq(askUpPrice, 27913456 * (10 ** 15)); // 27913.456 - assertEq(askDownPrice, 27902304 * (10 ** 15)); // 27902.304 + assertEq(askDownPrice, 27896728 * (10 ** 15)); // 27896.728 assertEq(bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 - assertEq(bidDownPrice, 35896723117375); // 1/27857.696, 27880 - 27880 * 0.08% = 27867.696 - assertEq(swapFee, 8 * (10 ** 14)); //0.08% + assertEq(bidDownPrice, 35889539462559); // 1/27863.272, 27880 - 27880 * 0.06% = 27863.272 + assertEq(swapFee, 6 * (10 ** 14)); //0.06% - (askUpPrice, askDownPrice, bidUpPrice, bidDownPrice, swapFee) = makerTypes.parseAllPrice(priceInfo, 8, (2*10**14)); + (askUpPrice, askDownPrice, bidUpPrice, bidDownPrice, swapFee) = makerTypes.parseAllPrice(priceInfo); assertEq(askUpPrice, 27913456 * (10 ** 15)); //decimal is still 18 - assertEq(askDownPrice, 27902304 * (10 ** 15)); // decimal is still 18 + assertEq(askDownPrice, 27896728 * (10 ** 15)); // decimal is still 18 assertEq(bidUpPrice, 35903909648530); // decimal is 18, 0.00003590 - assertEq(bidDownPrice, 35896723117375); // decimal is 18 - assertEq(swapFee, 8 * (10 ** 14)); + assertEq(bidDownPrice, 35889539462559); // decimal is 18 + assertEq(swapFee, 6 * (10 ** 14)); } function testPriceInvalid() public { - uint80 priceInfo = stickPrice(27880, 18, 6, 12, 10); + uint80 priceInfo = stickPrice(27880, 18, 13, 12, 14); vm.expectRevert(bytes("ask price invalid")); - (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = makerTypes.parseAllPrice(priceInfo, 18, 10**15); + (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = makerTypes.parseAllPrice(priceInfo); + priceInfo = stickPrice(27880, 18, 11, 12, 10); vm.expectRevert(bytes("bid price invalid")); - (askUpPrice, askDownPrice, bidUpPrice, bidDownPrice, swapFee) = makerTypes.parseAllPrice(priceInfo, 8, (5*10**14)); + (askUpPrice, askDownPrice, bidUpPrice, bidDownPrice, swapFee) = makerTypes.parseAllPrice(priceInfo); } } \ No newline at end of file diff --git a/test/DODOV3MM/lib/PMMRangeOrder.t.sol b/test/DODOV3MM/lib/PMMRangeOrder.t.sol index f811408..3b5215d 100644 --- a/test/DODOV3MM/lib/PMMRangeOrder.t.sol +++ b/test/DODOV3MM/lib/PMMRangeOrder.t.sol @@ -142,15 +142,15 @@ contract PMMRangeOrderTest is TestContext { (uint256 fromAmount, uint256 receiveToToken, uint256 vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token1), address(token2), 10 ** 18); assertEq(fromAmount, 10**18); - assertEq(receiveToToken, 24964525068078312916); // 24.96, because max vusdAmount = 300, suppose 25 + assertEq(receiveToToken, 24967456577251163707); // 24.96, because max vusdAmount = 300, suppose 25 assertEq(vusdAmount, 300 * (10** 18)); (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token1), address(token2), 10 ** 17); assertEq(fromAmount, 10**17); //console.log(receiveToToken); //console.log(vusdAmount); - assertEq(receiveToToken, 10812649799795029422); // 10.81, suppose near 10.83 - assertEq(vusdAmount, 129890376538907193976); // 129.8, suppose near 130 + assertEq(receiveToToken, 10816124163234959962); // 10.81, suppose near 10.83 + assertEq(vusdAmount, 129910761528419159777); // 129.8, suppose near 130 // with cumulative roState.fromTokenMMInfo.cumulativeAsk = 4 * (10 ** 18); @@ -159,13 +159,13 @@ contract PMMRangeOrderTest is TestContext { (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token1), address(token2), 10 ** 18); assertEq(fromAmount, 10**18); - assertEq(receiveToToken, 21633407960311327528); // 21.63, because max vusdAmount = 260, suppose near 21.67 + assertEq(receiveToToken, 21635614416486451483); // 21.63, because max vusdAmount = 260, suppose near 21.67 assertEq(vusdAmount, 260 * (10** 18)); (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token1), address(token2), 10 ** 17); assertEq(fromAmount, 10**17); - assertEq(receiveToToken, 10810214517700684559); // 10.81, suppose near 10.83 < 10812 - assertEq(vusdAmount, 129886911976839434749); // 129.8, suppose near 130, < 12989 + assertEq(receiveToToken, 10813115431872979233); // 10.81, suppose near 10.83 < 10812 + assertEq(vusdAmount, 129903836603817301717); // 129.8, suppose near 130, < 12989 // change fromToken and toToken, fromToken = 12, toToken = 1300 Types.TokenMMInfo memory tokenMMInfo = roState.toTokenMMInfo; @@ -174,13 +174,13 @@ contract PMMRangeOrderTest is TestContext { (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token2), address(token1), (100)*10 ** 18); assertEq(fromAmount, (100)*10 ** 18); - assertEq(receiveToToken, 19982947988629054); // 0.019, suppose near 0.02 + assertEq(receiveToToken, 19986411463079296); // 0.019, suppose near 0.02 assertEq(vusdAmount, 26 * (10** 18)); // suppose 1200, max vusdAmount = 26 (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.querySellTokens(roState, address(token2), address(token1), 10 ** 18); assertEq(fromAmount, 10 ** 18); - assertEq(receiveToToken, 9213376791555881); // 0.0092, suppose near 0.00923 - assertEq(vusdAmount, 11987609614474078065); // 11.9, suppose 12, + assertEq(receiveToToken, 9216208031790700); // 0.0092, suppose near 0.00923 + assertEq(vusdAmount, 11989214950558305806); // 11.9, suppose 12, } function testQueryBuyTokens() public { @@ -188,9 +188,9 @@ contract PMMRangeOrderTest is TestContext { // fromToken is 1300, toToken is 12 (uint256 fromAmount, uint256 receiveToToken, uint256 vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2), 24964525068078312916); - assertEq(fromAmount, 230977100923055001); // 0.2309, suppose near(>) 0.2304 + assertEq(fromAmount, 230926815372467868); // 0.2309, suppose near(>) 0.2304 assertEq(receiveToToken, 24964525068078312916); // 24.96 - assertEq(vusdAmount, 299999999999999998460); // 299.99, suppose near 300 + assertEq(vusdAmount, 299964751121110757874); // 299.99, suppose near 300 //console.log(fromAmount); //console.log(vusdAmount); @@ -199,9 +199,9 @@ contract PMMRangeOrderTest is TestContext { (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2),40 * (10 ** 18)); (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokensLoc(roState, address(token1), address(token2), 10812725271096067414); - assertEq(fromAmount, 100000698208426075); // suppose nead 1e18 + assertEq(fromAmount, 99968563416391785); // suppose nead 1e18 assertEq(receiveToToken, 10812725271096067414); // 0.01 - assertEq(vusdAmount, 129891283405183499173); // 12.9, suppose near 129891283405183499858 + assertEq(vusdAmount, 129869925557578937327); // 12.9, suppose near 129891283405183499858 // with cumulative roState.fromTokenMMInfo.cumulativeAsk = 4 * (10 ** 18); @@ -209,14 +209,14 @@ contract PMMRangeOrderTest is TestContext { roState.toTokenMMInfo.cumulativeBid = roState.toTokenMMInfo.cumulativeAsk = 4 * (10 ** 18); (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2), 21633407960311327528); - assertEq(fromAmount, 200182824626646523); // suppose near(>) 0.02 + assertEq(fromAmount, 200145004590210191); // suppose near(>) 0.02 assertEq(receiveToToken, 21633407960311327528); // 21.63, because max vusdAmount = 260, suppose near 21.67 - assertEq(vusdAmount, 259999999999999954026); // suppose 260 + assertEq(vusdAmount, 259973468248643989292); // suppose 260 (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2), 10810278971047276557); - assertEq(fromAmount, 100000596412654143); // suppose 1e18 + assertEq(fromAmount, 99973758057015627); // suppose 1e18 assertEq(receiveToToken, 10810278971047276557); // 10.81, suppose near 10.83 < 10812 - assertEq(vusdAmount, 129887686605257200476); // suppose 129887686605257245795, 129.8 + assertEq(vusdAmount, 129869750265425736303); // suppose 129887686605257245795, 129.8 // change fromToken and toToken, fromToken = 12, toToken = 1300 Types.TokenMMInfo memory tokenMMInfo = roState.toTokenMMInfo; @@ -224,14 +224,14 @@ contract PMMRangeOrderTest is TestContext { roState.fromTokenMMInfo = tokenMMInfo; (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token2), address(token1), 19982947988629054); - assertEq(fromAmount, 2169261355031671770); //21.6, suppose near(>) 21.6 + assertEq(fromAmount, 2168696336180917380); //21.6, suppose near(>) 21.6 assertEq(receiveToToken, 19982947988629054); // 0.019, suppose near 0.02 - assertEq(vusdAmount, 25999999999989768292); //suppose near 26 * (10** 18)), max vusdAmount = 26 + assertEq(vusdAmount, 25995494421095758256); //suppose near 26 * (10** 18)), max vusdAmount = 26 (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token2), address(token1), 9213376791555881); - assertEq(fromAmount, 999999999999146822); // suppose 1e18 + assertEq(fromAmount, 999692742457309249); // suppose 1e18 assertEq(receiveToToken, 9213376791555881); // 0.0092, suppose near 0.00923 - assertEq(vusdAmount, 11987609614463856804); // suppose 11987609614474078065, 11.9 + assertEq(vusdAmount, 11985531835891652912); // suppose 11987609614474078065, 11.9 } function testKisZero() public { @@ -239,17 +239,17 @@ contract PMMRangeOrderTest is TestContext { // fromToken is 1300, toToken is 12 (uint256 fromAmount, uint256 receiveToToken, uint256 vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2), 1e19); - assertEq(fromAmount, 92482101050675776); + assertEq(fromAmount, 92451834441199885); assertEq(receiveToToken, 1e19); // 10 - assertEq(vusdAmount, 120125740394024407940); + assertEq(vusdAmount, 120105666938664261055); //console.log(fromAmount); //console.log(vusdAmount); roState.fromTokenMMInfo.kBid = 0; (fromAmount, receiveToToken, vusdAmount) = pmmRangeOrderHelper.queryBuyTokens(roState, address(token1), address(token2), 1e19); - assertEq(fromAmount, 92478398406436275); + assertEq(fromAmount, 92444441232943138); assertEq(receiveToToken, 1e19); // 10 - assertEq(vusdAmount, 120125740394024407940); + assertEq(vusdAmount, 120105666938664261055); } function testPriceIsZero() public { diff --git a/test/DODOV3MM/periphery/D3Proxy.t.sol b/test/DODOV3MM/periphery/D3Proxy.t.sol index ba79a03..9177a5a 100644 --- a/test/DODOV3MM/periphery/D3Proxy.t.sol +++ b/test/DODOV3MM/periphery/D3Proxy.t.sol @@ -135,7 +135,7 @@ contract D3ProxyTest is TestContext { block.timestamp + 1000 ); - assertEq(receiveToToken, 919998); //0.00919, weth is 12, token1 is 1300, near 0.00923 + assertEq(receiveToToken, 921436); //0.00919, weth is 12, token1 is 1300, near 0.00923 //console.log("sell directly:", receiveToToken); uint256 beforeBalance2 = user1.balance; @@ -179,7 +179,7 @@ contract D3ProxyTest is TestContext { //console.log("weth:", beforeBalance2 - afterBalance2); //console.log(afterBalance3 - beforeBalance3); assertEq(beforeBalance2 - afterBalance2, 1 ether); - assertEq(afterBalance3 - beforeBalance3, 11956536767856879680); // 11.9, token3 is 1, near 12 + assertEq(afterBalance3 - beforeBalance3, 11974218097561551094); // 11.9, token3 is 1, near 12 // if msg.value mismatch fromAmount, should revert vm.deal(user1, 2 ether); @@ -249,7 +249,7 @@ contract D3ProxyTest is TestContext { //console.log(payFromAmount); //console.log(beforeBalance2 - afterBalance2); - assertEq(beforeBalance2 - afterBalance2, 1003369134268833132); + assertEq(beforeBalance2 - afterBalance2, 1001809084464781831); assertEq(beforeBalance2 - afterBalance2, payFromAmount); assertEq(afterBalance3 - beforeBalance3, 1 ether); } @@ -312,7 +312,7 @@ contract D3ProxyTest is TestContext { //console.log("eth:", afterBalance2 - beforeBalance2); //console.log(beforeBalance3 - afterBalance3 ); - assertEq(afterBalance2 - beforeBalance2, 996775265755655500); // 0.99, suppose 1 + assertEq(afterBalance2 - beforeBalance2, 998365344116153351); // 0.99, suppose 1 assertEq(beforeBalance3 - afterBalance3, 12 ether); } @@ -375,7 +375,7 @@ contract D3ProxyTest is TestContext { //console.log("eth:", afterBalance2 - beforeBalance2); //console.log(beforeBalance3 - afterBalance3 ); assertEq(afterBalance2 - beforeBalance2, 1 ether); - assertEq(beforeBalance3 - afterBalance3, 12038792297894767191); + assertEq(beforeBalance3 - afterBalance3, 12019646772033312087); } function testSwapCallBack() public { From 1fab4bb131828a78392e520a06aba04723dcaac8 Mon Sep 17 00:00:00 2001 From: traceurl <25892474+traceurl@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:59:37 +0800 Subject: [PATCH 3/4] add test for 0 lp fee --- test/DODOV3MM/D3MM/D3Trading.t.sol | 88 ++++++++++++++++++++++++++++++ test/DODOV3MM/lib/MakerType.t.sol | 10 ++++ 2 files changed, 98 insertions(+) diff --git a/test/DODOV3MM/D3MM/D3Trading.t.sol b/test/DODOV3MM/D3MM/D3Trading.t.sol index 6573d25..f0f7e90 100644 --- a/test/DODOV3MM/D3MM/D3Trading.t.sol +++ b/test/DODOV3MM/D3MM/D3Trading.t.sol @@ -85,6 +85,49 @@ contract D3TradingTest is TestContext { assertEq(afterBalance3 - beforeBalance3, 11978524479259449453); } + function testNormalSellTokens_LpFeeRateZero() public { + ( , ,address poolMaker, , ) = d3MM.getD3MMInfo(); + d3MakerWithPool = D3Maker(poolMaker); + vm.startPrank(maker); + + address[] memory tokenList = new address[](1); + tokenList[0] = address(token2); + uint80[] memory priceList = new uint80[](1); + priceList[0] = stickPrice(12, 18, 0, 23, 15); + d3MakerWithPool.setTokensPrice(tokenList, priceList); + vm.stopPrank(); + + uint256 beforeBalance2 = token2.balanceOf(user1); + uint256 beforeBalance3 = token3.balanceOf(user1); + + SwapCallbackData memory swapData; + swapData.data = ""; + swapData.payer = user1; + + uint256 gasleft1 = gasleft(); + vm.prank(user1); + uint256 receiveToToken = d3Proxy.sellTokens( + address(d3MM), + user1, + address(token2), + address(token3), + 1 ether, + 0, + abi.encode(swapData), + block.timestamp + 1000 + ); + uint256 gasleft2 = gasleft(); + console.log("sellToken1stTime gas\t", gasleft1 - gasleft2); + + uint256 afterBalance2 = token2.balanceOf(user1); + uint256 afterBalance3 = token3.balanceOf(user1); + + //console.log(receiveToToken); + assertEq(beforeBalance2 - afterBalance2, 1 ether); + assertEq(afterBalance3 - beforeBalance3, receiveToToken); + assertEq(afterBalance3 - beforeBalance3, 11984294018423130017); + } + function testNormalBuyTokens() public { uint256 beforeBalance2 = token2.balanceOf(user1); uint256 beforeBalance3 = token3.balanceOf(user1); @@ -119,6 +162,51 @@ contract D3TradingTest is TestContext { assertEq(afterBalance3 - beforeBalance3, 1 ether); } + function testNormalBuyTokens_LpFeeRateZero() public { + ( , ,address poolMaker, , ) = d3MM.getD3MMInfo(); + d3MakerWithPool = D3Maker(poolMaker); + vm.startPrank(maker); + + address[] memory tokenList = new address[](1); + tokenList[0] = address(token2); + uint80[] memory priceList = new uint80[](1); + priceList[0] = stickPrice(12, 18, 0, 23, 15); + d3MakerWithPool.setTokensPrice(tokenList, priceList); + vm.stopPrank(); + + uint256 beforeBalance2 = token2.balanceOf(user1); + uint256 beforeBalance3 = token3.balanceOf(user1); + + SwapCallbackData memory swapData; + swapData.data = ""; + swapData.payer = user1; + + uint256 gasleft1 = gasleft(); + vm.prank(user1); + uint256 receiveToToken = d3Proxy.buyTokens( + address(d3MM), + user1, + address(token2), + address(token3), + 1 ether, + 30 ether, + abi.encode(swapData), + block.timestamp + 1000 + ); + uint256 gasleft2 = gasleft(); + console.log("buyToken1stTime gas\t", gasleft1 - gasleft2); + + uint256 afterBalance2 = token2.balanceOf(user1); + uint256 afterBalance3 = token3.balanceOf(user1); + + //console.log(beforeBalance2 - afterBalance2); + //console.log(afterBalance3 - beforeBalance3); + + assertEq(beforeBalance2 - afterBalance2, receiveToToken); + assertEq(beforeBalance2 - afterBalance2, 83418837585988938); // 0.08 + assertEq(afterBalance3 - beforeBalance3, 1 ether); + } + function testTransferInNotEnough() public { vm.startPrank(user1); token2.approve(address(dodoApprove), 10**14); diff --git a/test/DODOV3MM/lib/MakerType.t.sol b/test/DODOV3MM/lib/MakerType.t.sol index f225679..c7aa861 100644 --- a/test/DODOV3MM/lib/MakerType.t.sol +++ b/test/DODOV3MM/lib/MakerType.t.sol @@ -78,6 +78,16 @@ contract MakerTypeTest is TestContext { assertEq(swapFee, 6 * (10 ** 14)); } + function testParseAllPriceWithZeroFeeRate() public { + uint80 priceInfo = stickPrice(27880, 18, 0, 12, 10); + (uint256 askUpPrice, uint256 askDownPrice, uint256 bidUpPrice, uint256 bidDownPrice, uint256 swapFee) = makerTypes.parseAllPrice(priceInfo); //0.02% + assertEq(askUpPrice, 27913456 * (10 ** 15)); // 27913.456 + assertEq(askDownPrice, 27880 * (10 ** 18)); // 27896.728 + assertEq(bidUpPrice, 35903909648530); // 1/27852.12, 27880 - 27880 * 0.1% =27852.12 + assertEq(bidDownPrice, 35868005738881); // 1/27880 + assertEq(swapFee, 0); //0.06% + } + function testPriceInvalid() public { uint80 priceInfo = stickPrice(27880, 18, 13, 12, 14); vm.expectRevert(bytes("ask price invalid")); From 9272b74d7c9a13048948a161b4152e99f34d590f Mon Sep 17 00:00:00 2001 From: Attens1423 Date: Sat, 21 Oct 2023 12:31:31 +0800 Subject: [PATCH 4/4] complish shadow test --- test/DODOV3MM/D3MM/D3Trading.t.sol | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/test/DODOV3MM/D3MM/D3Trading.t.sol b/test/DODOV3MM/D3MM/D3Trading.t.sol index f0f7e90..d913f4b 100644 --- a/test/DODOV3MM/D3MM/D3Trading.t.sol +++ b/test/DODOV3MM/D3MM/D3Trading.t.sol @@ -969,15 +969,25 @@ contract D3TradingTest is TestContext { Types.TokenMMInfo memory tokenMMInfo; uint256 tokenIndex; (tokenMMInfo, tokenIndex) = d3MakerWithPool.getTokenMMInfoForPool(address(token3)); - console.log(tokenMMInfo.askUpPrice); - console.log(tokenMMInfo.askDownPrice); - console.log(tokenMMInfo.bidUpPrice); - console.log(tokenMMInfo.bidDownPrice); + assertEq(tokenMMInfo.askUpPrice, 1000200000000000000); + assertEq(tokenMMInfo.askDownPrice, 1000100000000000000); + assertEq(tokenMMInfo.bidUpPrice, 1000200040008001601); + assertEq(tokenMMInfo.bidDownPrice, 1000100010001000101); console.log("query"); (uint256 fromAmount, uint256 toAmount, uint256 vusd, ,) = d3MM.querySellTokens(address(token2), address(token3), 10000); - console.log(fromAmount); - console.log(toAmount); - console.log(vusd); + assertEq(toAmount, 2118866); + assertEq(vusd, 2119077); + + // test lp fee =0 + prices[0] = stickPrice(10000, 14, 0, 2, 2); + prices[1] = stickPrice(21195, 16, 0, 100, 100); + vm.startPrank(maker); + d3MakerWithPool.setTokensPrice(tokens, prices); + vm.stopPrank(); + + (fromAmount, toAmount, vusd, ,) = d3MM.querySellTokens(address(token2), address(token3), 10000); + assertEq(toAmount, 2119501); + assertEq(vusd, 2119501); } }