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

fix(strategies): new base strategy abstract contract with single side… #131

Open
wants to merge 3 commits into
base: feat/v0.3.0
Choose a base branch
from
Open
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
26 changes: 4 additions & 22 deletions src/ConstantSum/ConstantSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
decodeFeeUpdate,
decodeControllerUpdate
} from "./ConstantSumUtils.sol";
import { PairStrategy, IStrategy, Pool } from "src/PairStrategy.sol";
import { Strategy, IStrategy, Pool } from "src/Strategy.sol";
import { EPSILON } from "src/lib/StrategyLib.sol";

struct InternalParams {
Expand All @@ -35,7 +35,7 @@ enum UpdateCode {
Controller
}

contract ConstantSum is PairStrategy {
contract ConstantSum is Strategy {
using FixedPointMathLib for uint256;

/// @notice Thrown when the expected liquidity is not met.
Expand All @@ -47,7 +47,7 @@ contract ConstantSum is PairStrategy {
mapping(uint256 => InternalParams) public internalParams;

/// @param dfmm_ Address of the DFMM contract.
constructor(address dfmm_) PairStrategy(dfmm_) { }
constructor(address dfmm_) Strategy(dfmm_) { }

/// @inheritdoc IStrategy
function init(
Expand Down Expand Up @@ -219,25 +219,7 @@ contract ConstantSum is PairStrategy {
);
}

/// @inheritdoc PairStrategy
function _computeAllocateDeltasGivenDeltaL(
uint256,
Pool memory,
bytes memory
) internal pure override returns (uint256[] memory) {
return new uint256[](2);
}

/// @inheritdoc PairStrategy
function _computeDeallocateDeltasGivenDeltaL(
uint256,
Pool memory,
bytes memory
) internal pure override returns (uint256[] memory) {
return new uint256[](2);
}

/// @inheritdoc PairStrategy
/// @inheritdoc Strategy
function _computeSwapDeltaLiquidity(
Pool memory,
bytes memory params,
Expand Down
40 changes: 4 additions & 36 deletions src/GeometricMean/GeometricMean.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.22;

import { FixedPointMathLib } from "solmate/utils/FixedPointMathLib.sol";
import { PairStrategy, IStrategy } from "src/PairStrategy.sol";
import { Strategy, IStrategy } from "src/Strategy.sol";
import { DynamicParam, DynamicParamLib } from "src/lib/DynamicParamLib.sol";
import { Pool } from "src/interfaces/IDFMM.sol";
import {
Expand Down Expand Up @@ -37,7 +37,7 @@ enum UpdateCode {
/**
* @notice Geometric Mean Market Maker.
*/
contract GeometricMean is PairStrategy {
contract GeometricMean is Strategy {
using FixedPointMathLib for uint256;
using FixedPointMathLib for int256;
using DynamicParamLib for DynamicParam;
Expand All @@ -54,7 +54,7 @@ contract GeometricMean is PairStrategy {
mapping(uint256 => InternalParams) public internalParams;

/// @param dfmm_ Address of the DFMM contract.
constructor(address dfmm_) PairStrategy(dfmm_) { }
constructor(address dfmm_) Strategy(dfmm_) { }

/// @dev Thrown if the weight of X is greater than 1 (in WAD).
error InvalidWeightX();
Expand Down Expand Up @@ -170,39 +170,7 @@ contract GeometricMean is PairStrategy {
);
}

/// @inheritdoc PairStrategy
function _computeAllocateDeltasGivenDeltaL(
uint256 deltaLiquidity,
Pool memory pool,
bytes memory
) internal pure override returns (uint256[] memory deltas) {
deltas = new uint256[](2);
deltas[0] = computeDeltaGivenDeltaLRoundUp(
pool.reserves[0], deltaLiquidity, pool.totalLiquidity
);

deltas[1] = computeDeltaGivenDeltaLRoundUp(
pool.reserves[1], deltaLiquidity, pool.totalLiquidity
);
}

/// @inheritdoc PairStrategy
function _computeDeallocateDeltasGivenDeltaL(
uint256 deltaLiquidity,
Pool memory pool,
bytes memory
) internal pure override returns (uint256[] memory deltas) {
deltas = new uint256[](2);
deltas[0] = computeDeltaGivenDeltaLRoundDown(
pool.reserves[0], deltaLiquidity, pool.totalLiquidity
);

deltas[1] = computeDeltaGivenDeltaLRoundDown(
pool.reserves[1], deltaLiquidity, pool.totalLiquidity
);
}

/// @inheritdoc PairStrategy
/// @inheritdoc Strategy
function _computeSwapDeltaLiquidity(
Pool memory pool,
bytes memory params,
Expand Down
44 changes: 4 additions & 40 deletions src/LogNormal/LogNormal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.22;

import { Pool } from "src/interfaces/IDFMM.sol";
import { PairStrategy, IStrategy } from "src/PairStrategy.sol";
import { Strategy, IStrategy } from "src/Strategy.sol";
import { DynamicParamLib, DynamicParam } from "src/lib/DynamicParamLib.sol";
import {
computeTradingFunction,
Expand Down Expand Up @@ -57,7 +57,7 @@ uint256 constant MAX_MEAN = uint256(type(int256).max);
* @title LogNormal Strategy for DFMM.
* @author Primitive
*/
contract LogNormal is PairStrategy {
contract LogNormal is Strategy {
using DynamicParamLib for DynamicParam;

/// @inheritdoc IStrategy
Expand All @@ -66,7 +66,7 @@ contract LogNormal is PairStrategy {
mapping(uint256 => InternalParams) public internalParams;

/// @param dfmm_ Address of the DFMM contract.
constructor(address dfmm_) PairStrategy(dfmm_) { }
constructor(address dfmm_) Strategy(dfmm_) { }

/// @inheritdoc IStrategy
function init(
Expand Down Expand Up @@ -173,43 +173,7 @@ contract LogNormal is PairStrategy {
);
}

/// @inheritdoc PairStrategy
function _computeAllocateDeltasGivenDeltaL(
uint256 deltaLiquidity,
Pool memory pool,
bytes memory
) internal pure override returns (uint256[] memory) {
uint256[] memory deltas = new uint256[](2);

deltas[0] = computeDeltaGivenDeltaLRoundUp(
pool.reserves[0], deltaLiquidity, pool.totalLiquidity
);

deltas[1] = computeDeltaGivenDeltaLRoundUp(
pool.reserves[1], deltaLiquidity, pool.totalLiquidity
);

return deltas;
}

/// @inheritdoc PairStrategy
function _computeDeallocateDeltasGivenDeltaL(
uint256 deltaLiquidity,
Pool memory pool,
bytes memory
) internal pure override returns (uint256[] memory) {
uint256[] memory deltas = new uint256[](2);

deltas[0] = computeDeltaGivenDeltaLRoundDown(
pool.reserves[0], deltaLiquidity, pool.totalLiquidity
);

deltas[1] = computeDeltaGivenDeltaLRoundDown(
pool.reserves[1], deltaLiquidity, pool.totalLiquidity
);
return deltas;
}

/// @inheritdoc Strategy
function _computeSwapDeltaLiquidity(
Pool memory pool,
bytes memory params,
Expand Down
71 changes: 17 additions & 54 deletions src/NTokenGeometricMean/NTokenGeometricMean.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.22;

import { DynamicParam, DynamicParamLib } from "src/lib/DynamicParamLib.sol";
import { NTokenStrategy, IStrategy } from "src/NTokenStrategy.sol";
import { Strategy, IStrategy } from "src/Strategy.sol";
import { Pool } from "src/interfaces/IDFMM.sol";
import {
decodeFeeUpdate,
Expand Down Expand Up @@ -39,7 +39,7 @@ enum UpdateCode {
/**
* @notice N-Token Geometric Mean Market Maker.
*/
contract NTokenGeometricMean is NTokenStrategy {
contract NTokenGeometricMean is Strategy {
using DynamicParamLib for DynamicParam;

struct InternalParams {
Expand All @@ -54,7 +54,7 @@ contract NTokenGeometricMean is NTokenStrategy {
mapping(uint256 => InternalParams) public internalParams;

/// @param dfmm_ Address of the DFMM contract.
constructor(address dfmm_) NTokenStrategy(dfmm_) { }
constructor(address dfmm_) Strategy(dfmm_) { }

/// @dev Thrown when the sum of the weights is not equal to 1 (in WAD).
error InvalidWeights(uint256 totalWeight);
Expand Down Expand Up @@ -201,57 +201,7 @@ contract NTokenGeometricMean is NTokenStrategy {
);
}

/// @inheritdoc NTokenStrategy
function _computeAllocateDeltasAndReservesGivenDeltaL(
uint256 deltaLiquidity,
uint256[] memory maxDeltas,
Pool memory pool
)
internal
pure
override
returns (uint256[] memory deltas, uint256[] memory nextReserves)
{
deltas = new uint256[](pool.reserves.length);
nextReserves = new uint256[](pool.reserves.length);
for (uint256 i = 0; i < pool.reserves.length; i++) {
uint256 reserveT = pool.reserves[i];
deltas[i] = computeDeltaGivenDeltaLRoundUp(
pool.reserves[i], deltaLiquidity, pool.totalLiquidity
);
if (deltas[i] > maxDeltas[i]) {
revert DeltaError(maxDeltas[i], deltas[i]);
}
nextReserves[i] = reserveT + deltas[i];
}
}

/// @inheritdoc NTokenStrategy
function _computeDeallocateDeltasAndReservesGivenDeltaL(
uint256 deltaLiquidity,
uint256[] memory minDeltas,
Pool memory pool
)
internal
pure
override
returns (uint256[] memory deltas, uint256[] memory nextReserves)
{
deltas = new uint256[](pool.reserves.length);
nextReserves = new uint256[](pool.reserves.length);
for (uint256 i = 0; i < pool.reserves.length; i++) {
uint256 reserveT = pool.reserves[i];
deltas[i] = computeDeltaGivenDeltaLRoundDown(
reserveT, deltaLiquidity, pool.totalLiquidity
);
if (minDeltas[i] > deltas[i]) {
revert DeltaError(minDeltas[i], deltas[i]);
}
nextReserves[i] = reserveT - deltas[i];
}
}

/// @inheritdoc NTokenStrategy
/// @inheritdoc Strategy
function _computeSwapDeltaLiquidity(
Pool memory pool,
bytes memory params,
Expand All @@ -271,4 +221,17 @@ contract NTokenGeometricMean is NTokenStrategy {
poolParams.swapFee
);
}

/// todo: work on this
/// @inheritdoc Strategy
/* function _computeDeltaLGivenDeltas(
uint256[] memory tokenDeltas,
Pool memory pool,
bytes memory params
) internal view override returns (uint256) {
NTokenGeometricMeanParams memory poolParams =
abi.decode(params, (NTokenGeometricMeanParams));

return computeL(pool.reserves, poolParams);
} */
}
Loading
Loading