Skip to content

Commit

Permalink
fixed strats
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 19, 2024
1 parent 5e41601 commit 3e85769
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
27 changes: 6 additions & 21 deletions src/solvers/ConstantSum/ConstantSumSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,12 @@ contract ConstantSumSolver {
);
uint256 amountOut;
if (swapXIn) {
uint256 fees = amountIn.mulWadUp(poolParams.swapFee);
uint256 deltaL =
fees.mulWadUp(startReserves.L.divWadDown(startReserves.rx));

// amountOut = amountIn.mulWadDown(
// poolParams.price.mulWadDown(ONE - poolParams.swapFee)
// - poolParams.swapFee
// );
uint256 deltaL = amountIn.mulWadUp(poolParams.swapFee);

amountOut = amountIn.mulWadDown(
(ONE + poolParams.price).mulWadDown(poolParams.swapFee) - ONE
amountOut = amountIn.mulWadDown(poolParams.price).mulWadDown(
ONE - poolParams.swapFee
);

console2.log("Fees: ", fees);

endReserves.rx = startReserves.rx + amountIn;
endReserves.L = startReserves.L + deltaL;

Expand All @@ -75,17 +66,11 @@ contract ConstantSumSolver {
if (startReserves.ry < amountOut) revert NotEnoughLiquidity();
endReserves.ry = startReserves.ry - amountOut;
} else {
uint256 fees = amountIn.mulWadUp(poolParams.swapFee);
uint256 deltaL =
fees.mulWadUp(startReserves.L.divWadDown(startReserves.ry));
uint256 effectiveAmountIn = amountIn - fees;

console2.log("Effective amount in: ", effectiveAmountIn);

amountOut =
effectiveAmountIn.mulWadDown(ONE.divWadDown(poolParams.price));
amountIn.mulWadUp(poolParams.swapFee).divWadUp(poolParams.price);

console2.log("Fees: ", fees);
amountOut = (ONE - poolParams.swapFee).mulWadDown(amountIn)
.divWadDown(poolParams.price);

endReserves.ry = startReserves.ry + amountIn;
endReserves.L = startReserves.L + deltaL;
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ contract ConstantSum is IStrategy {
console2.log("amountIn in validate: ", amountIn);
fees = amountIn.mulWadUp(params.swapFee);
console2.log("fees in validate: ", fees);
minLiquidityDelta += fees.mulWadUp(startL).divWadUp(startRx);
minLiquidityDelta += fees;
} else if (nextRy > startRy) {
amountIn = nextRy - startRy;
console2.log("amountIn in validate: ", amountIn);
fees = amountIn.mulWadUp(params.swapFee);
console2.log("fees in validate: ", fees);
minLiquidityDelta += fees.mulWadUp(startL).divWadUp(startRy);
minLiquidityDelta += fees.divWadUp(params.price);
} else {
revert("invalid swap: inputs x and y have the same sign!");
}
Expand Down
67 changes: 54 additions & 13 deletions src/test/ConstantSum/ConstantSumTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ contract ConstantSumTest is Test {
vm.warp(0);

ConstantSum.ConstantSumParams memory params = ConstantSum
.ConstantSumParams({
price: ONE * 2,
swapFee: TEST_SWAP_FEE,
controller: address(0)
});
.ConstantSumParams({ price: ONE * 2, swapFee: 0, controller: address(0) });

uint256 init_x = ONE * 1;
uint256 init_y = ONE * 1;
Expand Down Expand Up @@ -106,7 +102,7 @@ contract ConstantSumTest is Test {
assertEq(initL, 1.5 ether);
}

function test_constant_sum_swap_x_in() public basic_feeless {
function test_constant_sum_swap_x_in_no_fee() public basic_feeless {
bool xIn = true;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
Expand All @@ -115,11 +111,26 @@ contract ConstantSumTest is Test {
console2.log("Valid: ", valid);
console2.log("AmountOut: ", amountOut);
assert(valid);
assert(amountOut == 200_000_000_000_000_000);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 0.2 ether);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 1.1 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 0.8 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.5 ether);

dfmm.swap(poolId, swapData);
}

function test_constant_sum_swap_y_in() public basic_feeless {
function test_constant_sum_swap_y_in_no_fee() public basic_feeless {
bool xIn = false;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
Expand All @@ -128,7 +139,22 @@ contract ConstantSumTest is Test {
console2.log("Valid: ", valid);
console2.log("AmountOut: ", amountOut);
assert(valid);
assert(amountOut == 50_000_000_000_000_000);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 0.05 ether);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 0.95 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 1.1 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.5 ether);

dfmm.swap(poolId, swapData);
}

Expand Down Expand Up @@ -159,7 +185,7 @@ contract ConstantSumTest is Test {
assert(valid);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 199_100_000_000_000_000);
assertEq(amountOut, 0.1994 ether);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));
Expand All @@ -168,10 +194,10 @@ contract ConstantSumTest is Test {
assertEq(endReservesRx, 1.1 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 0.8009 ether);
assertEq(endReservesRy, 0.8006 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.50045 ether);
assertEq(endReservesL, 1.5003 ether);

dfmm.swap(poolId, swapData);
}
Expand All @@ -185,7 +211,22 @@ contract ConstantSumTest is Test {
console2.log("Valid: ", valid);
console2.log("AmountOut: ", amountOut);
assert(valid);
assert(amountOut == 50_000_000_000_000_000);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 0.04985 ether);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 0.95015 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 1.1 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.50015 ether);

dfmm.swap(poolId, swapData);
}
}

0 comments on commit 3e85769

Please sign in to comment.