Skip to content

Commit

Permalink
Merge pull request #2 from DODOEX/feat/fixRate
Browse files Browse the repository at this point in the history
fix lpRate+mtRate
  • Loading branch information
traceurl authored Oct 24, 2023
2 parents cd530e7 + 9272b74 commit 287648c
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 114 deletions.
2 changes: 1 addition & 1 deletion contracts/DODOV3MM/D3Pool/D3Maker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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;
Expand Down
12 changes: 8 additions & 4 deletions contracts/DODOV3MM/D3Pool/D3Trading.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions contracts/DODOV3MM/lib/MakerTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/DODOV3MM/D3MM/D3MM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,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);
Expand Down Expand Up @@ -126,6 +126,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
}
}
40 changes: 20 additions & 20 deletions test/DODOV3MM/D3MM/D3Maker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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));

Expand Down
Loading

0 comments on commit 287648c

Please sign in to comment.