Skip to content

Commit

Permalink
fix: compute delta liquidity in ConstantSum with different rounding d…
Browse files Browse the repository at this point in the history
…irections
  • Loading branch information
clemlak committed Apr 9, 2024
1 parent 54d10bb commit 1e51776
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
FixedPointMathLib,
computeTradingFunction,
computeSwapDeltaLiquidity,
computeDeltaLiquidity
computeDeltaLiquidityRoundDown,
computeDeltaLiquidityRoundUp
} from "./ConstantSumMath.sol";
import {
decodePriceUpdate,
Expand Down Expand Up @@ -67,8 +68,9 @@ contract ConstantSum is PairStrategy {
ConstantSumParams memory params;

(reserves, params) = abi.decode(data, (uint256[], ConstantSumParams));
totalLiquidity =
computeDeltaLiquidity(reserves[0], reserves[1], params.price);
totalLiquidity = computeDeltaLiquidityRoundDown(
reserves[0], reserves[1], params.price
);

if (pool.reserves.length != 2 || reserves.length != 2) {
revert InvalidReservesLength();
Expand Down Expand Up @@ -106,8 +108,9 @@ contract ConstantSum is PairStrategy {
(uint256 deltaX, uint256 deltaY, uint256 minDeltaL) =
abi.decode(data, (uint256, uint256, uint256));

deltaLiquidity =
computeDeltaLiquidity(deltaX, deltaY, internalParams[poolId].price);
deltaLiquidity = computeDeltaLiquidityRoundDown(
deltaX, deltaY, internalParams[poolId].price
);
if (deltaLiquidity < minDeltaL) revert InvalidDeltaLiquidity();

deltas = new uint256[](2);
Expand Down Expand Up @@ -145,8 +148,9 @@ contract ConstantSum is PairStrategy {
(uint256 deltaX, uint256 deltaY, uint256 maxDeltaL) =
abi.decode(data, (uint256, uint256, uint256));

deltaLiquidity =
computeDeltaLiquidity(deltaX, deltaY, internalParams[poolId].price);
deltaLiquidity = computeDeltaLiquidityRoundUp(
deltaX, deltaY, internalParams[poolId].price
);
if (deltaLiquidity > maxDeltaL) revert InvalidDeltaLiquidity();

deltas = new uint256[](2);
Expand Down
5 changes: 3 additions & 2 deletions test/ConstantSum/unit/Allocate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.13;

import { ConstantSumSetUp } from "./SetUp.sol";
import {
computeDeltaLiquidity,
computeDeltaLiquidityRoundDown,
ConstantSumParams
} from "src/ConstantSum/ConstantSumMath.sol";

Expand All @@ -15,7 +15,8 @@ contract ConstantSumAllocateTest is ConstantSumSetUp {
ConstantSumParams memory params =
abi.decode(constantSum.getPoolParams(POOL_ID), (ConstantSumParams));

uint256 deltaL = computeDeltaLiquidity(deltaX, deltaY, params.price);
uint256 deltaL =
computeDeltaLiquidityRoundDown(deltaX, deltaY, params.price);
dfmm.allocate(POOL_ID, abi.encode(deltaX, deltaY, deltaL));
}
}

0 comments on commit 1e51776

Please sign in to comment.