Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Solvers clean #116

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ConstantSum/ConstantSumMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ function computeSwapDeltaLiquidity(
return (params.swapFee).mulDivUp(delta, params.price);
}
}

/**
* @dev Computes the price using the reserve of token X.
*/
function computePrice(ConstantSumParams memory params) pure returns (uint256) {
return params.price;
}
Comment on lines +57 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this one is needed? I understand that it might be for consistency regarding the other strategies but I think this one could just be an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really but makes the api the same between them all. I think general is better than bespoke so i advocate for keeping it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this, but if it lives in the ConstantSumMath.sol and it's not invoked anywhere in e.g. solver, or strategy it doesn't really do anything, so I'm not sure what to do with this at the moment.

49 changes: 48 additions & 1 deletion src/ConstantSum/ConstantSumSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import {
encodeFeeUpdate,
encodeControllerUpdate
} from "./ConstantSumUtils.sol";
import {
computeAllocationGivenX,
computeAllocationGivenY
} from "src/lib/StrategyLib.sol";
import {
ONE,
computeInitialPoolData,
FixedPointMathLib,
computeSwapDeltaLiquidity
} from "./ConstantSumMath.sol";
import { PairSolver } from "src/PairSolver.sol";

contract ConstantSumSolver {
contract ConstantSumSolver is PairSolver {
error NotEnoughLiquidity();

using FixedPointMathLib for uint256;
Expand Down Expand Up @@ -46,6 +51,7 @@ contract ConstantSumSolver {
uint256 deltaLiquidity;
}

/// @notice used by kit
function simulateSwap(
uint256 poolId,
bool swapXIn,
Expand Down Expand Up @@ -91,6 +97,7 @@ contract ConstantSumSolver {
return (valid, state.amountOut, swapData);
}

/// @notice used by kit
function preparePriceUpdate(uint256 newPrice)
public
pure
Expand All @@ -99,6 +106,7 @@ contract ConstantSumSolver {
return encodePriceUpdate(newPrice);
}

/// @notice used by kit
function prepareSwapFeeUpdate(uint256 newSwapFee)
public
pure
Expand All @@ -107,11 +115,50 @@ contract ConstantSumSolver {
return encodeFeeUpdate(newSwapFee);
}

/// @notice used by kit
function prepareControllerUpdate(address newController)
public
pure
returns (bytes memory)
{
return encodeControllerUpdate(newController);
}

function getReservesAndLiquidity(uint256 poolId)
public
view
override
returns (uint256, uint256, uint256)
{
Pool memory pool = IDFMM(IStrategy(strategy).dfmm()).pools(poolId);
return (pool.reserves[0], pool.reserves[1], pool.totalLiquidity);
}

function getPoolParams(uint256 poolId)
public
view
returns (ConstantSumParams memory)
{
return abi.decode(
IStrategy(strategy).getPoolParams(poolId), (ConstantSumParams)
);
}

// These are same for allocation and deallocation
// delta y is 0
function PrepareAllocationDeltaGivenDeltaX(
uint256 poolId,
uint256 deltaX
) public view returns (bytes memory) {
ConstantSumParams memory params = getPoolParams(poolId);
uint256 deltaL = deltaX.mulWadDown(params.price);
return abi.encode(deltaX, 0, deltaL);
}

function PrepareAllocationDeltaGivenDeltaY(
// uint256 poolId,
uint256 deltaY
) public pure returns (bytes memory) {
return abi.encode(0, deltaY, deltaY);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NatSpec could be improved and the name of the functions should use camel case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed some of the natspec but i am unsure if i am most familiar with the conventions you would like to adhere to.

}
2 changes: 1 addition & 1 deletion src/ConstantSum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $$
where $L$ is the **liquidity** of the pool.

## Price
The reported price of the pool given the reseres is $P$.
The reported price of the pool given the reserves is $P$.

## Pool initialization
The `ConstantSum` pool can be initialized with any given price and any given value of reserves.
Expand Down
48 changes: 3 additions & 45 deletions src/GeometricMean/GeometricMeanSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
computePrice,
computeSwapDeltaLiquidity
} from "./G3MMath.sol";
import { PairSolver } from "src/PairSolver.sol";

contract GeometricMeanSolver {
contract GeometricMeanSolver is PairSolver {
using FixedPointMathLib for uint256;
using FixedPointMathLib for int256;

Expand All @@ -46,6 +47,7 @@ contract GeometricMeanSolver {
function getReservesAndLiquidity(uint256 poolId)
public
view
override
returns (uint256, uint256, uint256)
{
Pool memory pool = IDFMM(IStrategy(strategy).dfmm()).pools(poolId);
Expand Down Expand Up @@ -94,50 +96,6 @@ contract GeometricMeanSolver {
return computeInitialPoolData(rx, S, params);
}

function allocateGivenDeltaX(
uint256 poolId,
uint256 deltaX
) public view returns (uint256, uint256) {
(uint256 rX, uint256 rY, uint256 totalLiquidity) =
getReservesAndLiquidity(poolId);
(uint256 deltaY, uint256 deltaLiquidity) =
computeAllocationGivenDeltaX(deltaX, rX, rY, totalLiquidity);
return (deltaY, deltaLiquidity);
}

function allocateGivenDeltaY(
uint256 poolId,
uint256 deltaY
) public view returns (uint256, uint256) {
(uint256 rX, uint256 rY, uint256 totalLiquidity) =
getReservesAndLiquidity(poolId);
(uint256 deltaX, uint256 deltaLiquidity) =
computeAllocationGivenDeltaY(deltaY, rX, rY, totalLiquidity);
return (deltaX, deltaLiquidity);
}

function deallocateGivenDeltaX(
uint256 poolId,
uint256 deltaX
) public view returns (uint256, uint256) {
(uint256 rX, uint256 rY, uint256 totalLiquidity) =
getReservesAndLiquidity(poolId);
(uint256 deltaY, uint256 deltaLiquidity) =
computeDeallocationGivenDeltaX(deltaX, rX, rY, totalLiquidity);
return (deltaY, deltaLiquidity);
}

function deallocateGivenDeltaY(
uint256 poolId,
uint256 deltaY
) public view returns (uint256, uint256) {
(uint256 rX, uint256 rY, uint256 totalLiquidity) =
getReservesAndLiquidity(poolId);
(uint256 deltaX, uint256 deltaLiquidity) =
computeDeallocationGivenDeltaY(deltaY, rX, rY, totalLiquidity);
return (deltaX, deltaLiquidity);
}

function getNextReserveX(
uint256 poolId,
uint256 ry,
Expand Down
Loading
Loading