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

1 stream consumption against opposite swaps #37

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract Pool is IPool, Ownable {
mapping(bytes32 => mapping(uint256 => Swap[])) public triggerAndMarketOrderBook;
mapping(bytes32 => mapping(uint256 => Swap[])) public limitOrderBook;

mapping(bytes32 => uint256) public override highestPriceMarker;
mapping(bytes32 => uint256) public override highestPriceKey;

address[] public poolAddress;

Expand Down Expand Up @@ -674,8 +674,8 @@ contract Pool is IPool, Ownable {
return SWAP_IDS++;
}

function setHighestPriceMarker(bytes32 pairId, uint256 value) external override onlyPoolLogic {
highestPriceMarker[pairId] = value;
function setHighestPriceKey(bytes32 pairId, uint256 value) external override onlyPoolLogic {
highestPriceKey[pairId] = value;
}

function orderBook(
Expand Down
984 changes: 385 additions & 599 deletions src/PoolLogic.sol

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions src/Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,6 @@ contract Router is Ownable, ReentrancyGuard, IRouter {
IPoolLogic(poolStates.POOL_LOGIC()).processRemoveLiquidity(token);
}

function swap(address tokenIn, address tokenOut, uint256 amountIn, uint256 executionPrice) external nonReentrant {
// if (amountIn == 0) revert InvalidAmount();
// if (executionPrice == 0) revert InvalidExecutionPrice();
// if (!poolExist(tokenIn) || !poolExist(tokenOut)) revert InvalidPool();

// IERC20(tokenIn).safeTransferFrom(msg.sender, POOL_ADDRESS, amountIn);
// IPoolLogic(poolStates.POOL_LOGIC()).swap(msg.sender, tokenIn, tokenOut, amountIn, executionPrice);
}

function swapMarketOrder(address tokenIn, address tokenOut, uint256 amountIn) external nonReentrant {
if (amountIn == 0) revert InvalidAmount();
if (!poolExist(tokenIn) || !poolExist(tokenOut)) revert InvalidPool();
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/pool-logic/IPoolLogicActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ interface IPoolLogicActions {
)
external;
function withdrawFromGlobalPool(address user, address token, uint256 amount) external;
function swap(address user, address tokenIn, address tokenOut, uint256 amountIn, uint256 executionPrice) external;
function swapLimitOrder(
address user,
address tokenIn,
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/pool/IPoolActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ interface IPoolActions {
function getReserveA(address pool) external view returns (uint256);
function getReserveD(address pool) external view returns (uint256);

function setHighestPriceMarker(bytes32 pairId, uint256 value) external;
function setHighestPriceKey(bytes32 pairId, uint256 value) external;

function getPoolAddresses() external view returns (address[] memory);

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/pool/IPoolStates.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ interface IPoolStates {
returns (RemoveLiquidityStream[] memory removeLiquidityStream);

function globalPoolDBalance(address) external view returns (uint256);
function highestPriceMarker(bytes32) external view returns (uint256);
function highestPriceKey(bytes32) external view returns (uint256);
function orderBook(bytes32 pairId, uint256 priceKey, bool isLimitOrder) external view returns (Swap[] memory);
}
62 changes: 28 additions & 34 deletions src/lib/PoolLogicLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library PoolLogicLib {
using DSMath for uint256;
using ScaleDecimals for uint256;

// @audit ensure that decimals are being passed in efffectively in lib functions
function getExecutionPrice(
uint256 reserveA_In,
uint256 reserveA_Out,
Expand All @@ -27,6 +28,7 @@ library PoolLogicLib {
return (amountIn * reserveOut) / reserveIn;
}

// @audit unit test required && scale to A to 18
function getSwapAmountOut(
uint256 amountIn,
uint256 reserveA_In,
Expand All @@ -46,17 +48,24 @@ library PoolLogicLib {
// 100000000000000000000
// 1000000000000000000
uint256 d1 = (amountIn * reserveD_In) / (amountIn + reserveA_In);
// use the wdiv divide 2 18 decimals
return (d1, ((d1 * reserveA_Out) / (d1 + reserveD_Out)));
}

// @audit unit test required && scale reserveA to 18 decimals
// not used yet or duplicated
function getSwapAmountOutFromD(uint256 dIn, uint256 reserveA, uint256 reserveD) public pure returns (uint256) {
return ((dIn * reserveA) / (dIn + reserveD));
}

// @audit unit test required && scale reserveA to 18 decimals
// not used yet
function getTokenOut(uint256 dAmount, uint256 reserveA, uint256 reserveD) external pure returns (uint256) {
return (dAmount * reserveA) / (dAmount + reserveD);
}

// @audit unit test required && scale reserveA to 18 decimals
// not used yet
function getDOut(uint256 tokenAmount, uint256 reserveA, uint256 reserveD) external pure returns (uint256) {
return (tokenAmount * reserveD) / (tokenAmount + reserveA);
}
Expand All @@ -80,8 +89,10 @@ library PoolLogicLib {

uint256 scaledAmountIn = amountIn.scaleAmountToDecimals(decimalsIn, 18);

uint256 result = ((scaledAmountIn * streamCountPrecision) / (((streamCountPrecision - poolSlippage) * reserveD)));
uint256 result =
((scaledAmountIn * streamCountPrecision) / (((streamCountPrecision - poolSlippage) * reserveD)));
return result < 1 ? 1 : result;
// @audit require a limit on the maximum number of streams
}

function calculateAssetTransfer(
Expand All @@ -93,9 +104,10 @@ library PoolLogicLib {
pure
returns (uint256)
{
return (reserveA.wmul(lpUnits)).wdiv(totalLpUnits);
return (reserveA * lpUnits) / totalLpUnits;
}

// @audit ensure that this is being handled effectively without wmul
function calculateDToDeduct(
uint256 lpUnits,
uint256 reserveD,
Expand All @@ -105,14 +117,16 @@ library PoolLogicLib {
pure
returns (uint256)
{
return reserveD.wmul(lpUnits).wdiv(totalLpUnits);
return (reserveD * lpUnits) / totalLpUnits;
}

function getPoolId(address tokenA, address tokenB) public pure returns (bytes32) {
(address A, address B) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
// @audit replace keccak for abi.encode && check dependancies
return keccak256(abi.encodePacked(A, B));
}

// @audit ensure that this is being handled well with decimals
function calculateLpUnitsToMint(
uint256 lpUnitsDepth, // P => depth of lpUnits
uint256 amount, // a => assets incoming
Expand All @@ -137,6 +151,7 @@ library PoolLogicLib {
return lpUnitsDepth * (num / den);
}

// @note not used anymore , comment out and check for breaking changes
function calculateDUnitsToMint(
uint256 amount,
uint256 reserveA,
Expand All @@ -151,44 +166,19 @@ library PoolLogicLib {
return initialDToMint;
}

return reserveD.wmul(amount).wdiv(reserveA);
return (reserveD * amount) / reserveA;
}

function getExecutionPriceLower(uint256 executionPrice, uint256 pricePrecision) public pure returns (uint256) {
function getExecutionPriceKey(uint256 executionPrice, uint256 pricePrecision) public pure returns (uint256) {
uint256 mod = executionPrice % pricePrecision; // @audit decide decimals for precission + use global variable
// for precission
return executionPrice - mod;
}

function getReciprocalOppositePrice(
uint256 executionPrice,
uint256 reserveA_In,
uint8 decimals_In,
uint8 decimals_Out
)
public
pure
returns (uint256)
{
uint256 reserveA_Out = getOtherReserveFromPrice(executionPrice, reserveA_In, decimals_In, decimals_Out);
return getExecutionPrice(reserveA_Out, reserveA_In, decimals_Out, decimals_In);
}

function getOtherReserveFromPrice(
uint256 executionPrice,
uint256 reserveA_In,
uint8 decimals_In,
uint8 decimals_Out
)
public
pure
returns (uint256)
{
return (reserveA_In.scaleAmountToDecimals(decimals_In, 18).wdiv(executionPrice)).scaleAmountToDecimals(
18, decimals_Out
); // @audit confirm scaling
}

// @audit is it gas efficient to pass decimals as inputs
// @audit ensure decimals are passed in securely on internal calls
// @audit is a high level check approach for all non-18 tokens a better way to avoid utilising scaling
// considerations on every stream execution
function calculateAmountOutFromPrice(
uint256 amountIn,
uint256 price,
Expand Down Expand Up @@ -238,4 +228,8 @@ library PoolLogicLib {
// Scale back to TokenA decimals
return scaledAmountIn.scaleAmountToDecimals(18, decimalsIn);
}

function getOppositePrice(uint256 price) public pure returns (uint256) {
return DSMath.WAD.wdiv(price);
}
}
Loading
Loading