Skip to content

Commit

Permalink
Merge pull request #85 from primitivefinance/fix/reserves-length-check
Browse files Browse the repository at this point in the history
Fix/reserves length check
  • Loading branch information
clemlak authored Mar 25, 2024
2 parents c2fdd68 + 03bf4f4 commit 7033738
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contract ConstantSum is PairStrategy {
function init(
address,
uint256 poolId,
Pool calldata,
Pool calldata pool,
bytes calldata data
)
public
Expand All @@ -66,6 +66,10 @@ contract ConstantSum is PairStrategy {
(reserves, totalLiquidity, params) =
abi.decode(data, (uint256[], uint256, ConstantSumParams));

if (pool.reserves.length != 2 || reserves.length != 2) {
revert InvalidReservesLength();
}

internalParams[poolId].price = params.price;
internalParams[poolId].swapFee = params.swapFee;
internalParams[poolId].controller = params.controller;
Expand Down
7 changes: 5 additions & 2 deletions src/GeometricMean/G3MMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ function computeInitialPoolData(

L = computeNextLiquidity(amountX, rY, invariant, L, params);

return
abi.encode(amountX, rY, L, params.wX, params.swapFee, params.controller);
uint256[] memory reserves = new uint256[](2);
reserves[0] = amountX;
reserves[1] = rY;

return abi.encode(reserves, L, params.wX, params.swapFee, params.controller);
}
15 changes: 7 additions & 8 deletions src/GeometricMean/GeometricMean.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,22 @@ contract GeometricMean is PairStrategy {
function init(
address,
uint256 poolId,
Pool calldata,
Pool calldata pool,
bytes calldata data
) external onlyDFMM returns (bool, int256, uint256[] memory, uint256) {
InitState memory state;

state.reserves = new uint256[](2);

(
state.reserves[0],
state.reserves[1],
state.reserves,
state.totalLiquidity,
state.wX,
state.swapFee,
state.controller
) = abi.decode(
data, (uint256, uint256, uint256, uint256, uint256, address)
);
) = abi.decode(data, (uint256[], uint256, uint256, uint256, address));

if (pool.reserves.length != 2 || state.reserves.length != 2) {
revert InvalidReservesLength();
}

if (state.wX >= ONE) {
revert InvalidWeightX();
Expand Down
6 changes: 5 additions & 1 deletion src/LogNormal/LogNormal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract LogNormal is PairStrategy {
function init(
address,
uint256 poolId,
Pool calldata,
Pool calldata pool,
bytes calldata data
)
public
Expand All @@ -74,6 +74,10 @@ contract LogNormal is PairStrategy {
(reserves, totalLiquidity, params) =
abi.decode(data, (uint256[], uint256, LogNormalParams));

if (pool.reserves.length != 2 || reserves.length != 2) {
revert InvalidReservesLength();
}

internalParams[poolId].mean.lastComputedValue = params.mean;
internalParams[poolId].width.lastComputedValue = params.width;
internalParams[poolId].swapFee = params.swapFee;
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/IStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ interface IStrategy {
/// @dev Thrown when an expected delta does not match the actual delta.
error DeltaError(uint256 expected, uint256 actual);

/// @dev Thrown when the reserves length is not 2.
error InvalidReservesLength();

// Setters

/**
Expand Down

0 comments on commit 7033738

Please sign in to comment.