Skip to content

Commit

Permalink
Merge pull request #107 from primitivefinance/fix/total-liquidity-init
Browse files Browse the repository at this point in the history
Fix/total liquidity init
  • Loading branch information
clemlak authored Apr 1, 2024
2 parents a6ed4ed + cebcdca commit 8bff684
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
decodeControllerUpdate
} from "./ConstantSumUtils.sol";
import { PairStrategy, IStrategy, Pool } from "src/PairStrategy.sol";
import { EPSILON } from "src/lib/StrategyLib.sol";

struct InternalParams {
uint256 price;
Expand Down Expand Up @@ -64,8 +65,10 @@ contract ConstantSum is PairStrategy {
)
{
ConstantSumParams memory params;
(reserves, totalLiquidity, params) =
abi.decode(data, (uint256[], uint256, ConstantSumParams));

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

if (pool.reserves.length != 2 || reserves.length != 2) {
revert InvalidReservesLength();
Expand All @@ -79,7 +82,7 @@ contract ConstantSum is PairStrategy {
invariant =
tradingFunction(reserves, totalLiquidity, abi.encode(params));

valid = invariant >= 0;
valid = invariant >= 0 && invariant <= EPSILON;

return (valid, invariant, reserves, totalLiquidity);
}
Expand Down
3 changes: 1 addition & 2 deletions src/ConstantSum/ConstantSumMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ function computeInitialPoolData(
) pure returns (bytes memory) {
// The pool can be initialized with any non-negative amount of rx, and ry.
// so we have to allow a user to pass an amount of both even if one is zero.
uint256 L = rx.mulWadDown(params.price) + ry;
uint256[] memory reserves = new uint256[](2);
reserves[0] = rx;
reserves[1] = ry;
return abi.encode(reserves, L, params);
return abi.encode(reserves, params);
}

function computeDeltaLiquidity(
Expand Down
4 changes: 2 additions & 2 deletions src/GeometricMean/GeometricMean.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
computeDeltaGivenDeltaLRoundDown,
computeSwapDeltaLiquidity
} from "./G3MMath.sol";
import { ONE } from "src/lib/StrategyLib.sol";
import { ONE, EPSILON } from "src/lib/StrategyLib.sol";

/**
* @dev Parameterization of the GeometricMean curve.
Expand Down Expand Up @@ -105,7 +105,7 @@ contract GeometricMean is PairStrategy {
abi.decode(getPoolParams(poolId), (GeometricMeanParams))
);

state.valid = state.invariant >= 0;
state.valid = state.invariant >= 0 && state.invariant <= EPSILON;

return
(state.valid, state.invariant, state.reserves, state.totalLiquidity);
Expand Down
3 changes: 2 additions & 1 deletion src/LogNormal/LogNormal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
decodeWidthUpdate,
decodeControllerUpdate
} from "src/LogNormal/LogNormalUtils.sol";
import { EPSILON } from "src/lib/StrategyLib.sol";

enum UpdateCode {
Invalid,
Expand Down Expand Up @@ -106,7 +107,7 @@ contract LogNormal is PairStrategy {

invariant =
tradingFunction(reserves, totalLiquidity, getPoolParams(poolId));
valid = invariant >= 0;
valid = invariant >= 0 && invariant <= EPSILON;
}

/// @inheritdoc IStrategy
Expand Down
4 changes: 2 additions & 2 deletions src/NTokenGeometricMean/NTokenGeometricMean.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
computeDeltaGivenDeltaLRoundDown,
computeSwapDeltaLiquidity
} from "src/NTokenGeometricMean/NTokenGeometricMeanMath.sol";
import { ONE } from "src/lib/StrategyLib.sol";
import { ONE, EPSILON } from "src/lib/StrategyLib.sol";

/**
* @dev Parameterization of the GeometricMean curve.
Expand Down Expand Up @@ -122,7 +122,7 @@ contract NTokenGeometricMean is NTokenStrategy {
state.reserves, state.totalLiquidity, getPoolParams(poolId)
);

bool valid = invariant >= 0;
bool valid = invariant >= 0 && invariant <= EPSILON;

return (valid, invariant, state.reserves, state.totalLiquidity);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/StrategyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.13;

import { FixedPointMathLib } from "solmate/utils/FixedPointMathLib.sol";

int256 constant EPSILON = 30;
uint256 constant HALF = 0.5e18;
uint256 constant ONE = 1e18;
uint256 constant TWO = 2e18;
Expand Down

0 comments on commit 8bff684

Please sign in to comment.