Skip to content

Commit

Permalink
stack2deep
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Apr 19, 2024
1 parent 96b29e9 commit 3ec0b53
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/ConstantSum/ConstantSumSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ contract ConstantSumSolver {
swapData = abi.encode(1, 0, amountIn, amountOut);
}

(bool valid,,,,,,) = IStrategy(strategy).validateSwap(
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);
return (valid, amountOut, swapData);
Expand Down
2 changes: 1 addition & 1 deletion src/CoveredCall/CoveredCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ contract CoveredCall is PairStrategy {
(reserves, totalLiquidity, params) =
abi.decode(data, (uint256[], uint256, CoveredCallParams));

params.timestamp = block.timestamp;
params.lastTimestamp = block.timestamp;

if (params.mean < MIN_WIDTH || params.mean > MAX_MEAN) {
revert InvalidMean();
Expand Down
4 changes: 2 additions & 2 deletions src/CoveredCall/CoveredCallMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function computeTradingFunction(
}

function computeTau(CoveredCallParams memory params) pure returns (uint256) {
if (params.timestamp >= params.maturity) {
if (params.lastTimestamp >= params.maturity) {
return 0;
} else {
return ONE * (params.maturity - params.timestamp) / YEAR;
return ONE * (params.maturity - params.lastTimestamp) / YEAR;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/CoveredCall/CoveredCallSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ contract CoveredCallSolver {
uint256 timestamp
) public view returns (CoveredCallParams memory) {
CoveredCallParams memory params = getPoolParams(poolId);
params.timestamp = timestamp;
params.lastTimestamp = timestamp;
return params;
}

Expand Down Expand Up @@ -305,7 +305,7 @@ contract CoveredCallSolver {
}

uint256 poolId = poolId;
(bool valid,,,,,,) = IStrategy(strategy).validateSwap(
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);

Expand Down
6 changes: 3 additions & 3 deletions src/DFMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ contract DFMM is IDFMM {
uint256 amountIn;
uint256 amountOut;
uint256 deltaLiquidity;
bytes postSwapHookData;
}

/// @inheritdoc IDFMM
Expand All @@ -225,7 +226,6 @@ contract DFMM is IDFMM {
bytes calldata callbackData
) external payable lock returns (address, address, uint256, uint256) {
SwapState memory state;
bytes memory postSwapHookData;

(
state.valid,
Expand All @@ -235,7 +235,7 @@ contract DFMM is IDFMM {
state.amountIn,
state.amountOut,
state.deltaLiquidity,
postSwapHookData
state.postSwapHookData
) = IStrategy(_pools[poolId].strategy).validateSwap(
msg.sender, poolId, _pools[poolId], data
);
Expand Down Expand Up @@ -267,7 +267,7 @@ contract DFMM is IDFMM {
_transfer(state.tokenOut, recipient, state.amountOut);

IStrategy(_pools[poolId].strategy).postSwapHook(
msg.sender, poolId, _pools[poolId], postSwapHookData
msg.sender, poolId, _pools[poolId], state.postSwapHookData
);

// If the callbackData is empty, do a regular `_transferFrom()` call, as in the other operations.
Expand Down
2 changes: 1 addition & 1 deletion src/GeometricMean/GeometricMeanSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ contract GeometricMeanSolver {
bytes memory swapData =
abi.encode(tokenInIndex, tokenOutIndex, amountIn, state.amountOut);

(bool valid,,,,,,) = IStrategy(strategy).validateSwap(
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);

Expand Down
2 changes: 1 addition & 1 deletion src/LogNormal/LogNormalSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ contract LogNormalSolver {
}

uint256 poolId = poolId;
(bool valid,,,,,,) = IStrategy(strategy).validateSwap(
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);
return (
Expand Down
2 changes: 1 addition & 1 deletion src/NTokenGeometricMean/NTokenGeometricMeanSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract NTokenGeometricMeanSolver {
bytes memory swapData =
abi.encode(tokenInIndex, tokenOutIndex, amountIn, state.amountOut);

(bool valid,,,,,,) = IStrategy(strategy).validateSwap(
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);

Expand Down
2 changes: 1 addition & 1 deletion src/PairStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ abstract contract PairStrategy is IStrategy {
uint256,
Pool memory,
bytes calldata
) external onlyDFMM { }
) external virtual onlyDFMM { }

/// @inheritdoc IStrategy
function getPoolParams(uint256 poolId)
Expand Down
2 changes: 1 addition & 1 deletion test/CoveredCall/unit/Init.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract CoveredCallInitTest is CoveredCallSetUp {
width: 0.1 ether,
maturity: YEAR * 2,
swapFee: FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down
12 changes: 6 additions & 6 deletions test/CoveredCall/unit/SetUp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract CoveredCallSetUp is SetUp {
width: 0.1 ether,
maturity: YEAR,
swapFee: TEST_SWAP_FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down Expand Up @@ -76,7 +76,7 @@ contract CoveredCallSetUp is SetUp {
width: 0.05 ether,
maturity: YEAR * 2,
swapFee: FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down Expand Up @@ -113,7 +113,7 @@ contract CoveredCallSetUp is SetUp {
width: 0.00001 ether,
maturity: YEAR,
swapFee: 0,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down Expand Up @@ -148,7 +148,7 @@ contract CoveredCallSetUp is SetUp {
width: 0.1 ether,
maturity: YEAR / 4,
swapFee: TEST_SWAP_FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down Expand Up @@ -183,7 +183,7 @@ contract CoveredCallSetUp is SetUp {
width: 0.25 ether,
maturity: YEAR,
swapFee: TEST_SWAP_FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down Expand Up @@ -214,7 +214,7 @@ contract CoveredCallSetUp is SetUp {
width: 0,
maturity: YEAR,
swapFee: TEST_SWAP_FEE,
timestamp: block.timestamp,
lastTimestamp: block.timestamp,
controller: address(this)
});

Expand Down
39 changes: 4 additions & 35 deletions test/SYCoveredCall/unit/Swap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract SYCoveredCallSwapTest is SYCoveredCallSetUp {
bool swapXForY = true;

(bool valid,,, bytes memory payload) =
solver.simulateSwap(POOL_ID, swapXForY, amountIn);
solver.simulateSwap(POOL_ID, swapXForY, amountIn, block.timestamp);
assertEq(valid, true);

(,, uint256 inputAmount, uint256 outputAmount) =
Expand All @@ -34,10 +34,7 @@ contract SYCoveredCallSwapTest is SYCoveredCallSetUp {
);
}

function test_SYCoveredCall_swap_SwapsXforY_WarpToMaturity()
public
init_no_fee
{
function test_SYCoveredCall_swap_SwapsXforY_WarpToMaturity() public init {
vm.warp(370 days);
uint256 preDfmmBalanceX = tokenX.balanceOf(address(dfmm));
uint256 preDfmmBalanceY = tokenY.balanceOf(address(dfmm));
Expand All @@ -49,7 +46,7 @@ contract SYCoveredCallSwapTest is SYCoveredCallSetUp {
bool swapXForY = true;

(bool valid, uint256 amountOut,, bytes memory payload) =
solver.simulateSwap(POOL_ID, swapXForY, amountIn);
solver.simulateSwap(POOL_ID, swapXForY, amountIn, block.timestamp);
assertEq(valid, true);

console2.log("out", amountOut);
Expand Down Expand Up @@ -77,7 +74,7 @@ contract SYCoveredCallSwapTest is SYCoveredCallSetUp {
bool swapXForY = false;

(bool valid,,, bytes memory payload) =
solver.simulateSwap(POOL_ID, swapXForY, amountIn);
solver.simulateSwap(POOL_ID, swapXForY, amountIn, block.timestamp);
assertEq(valid, true);
(,, uint256 inputAmount, uint256 outputAmount) =
dfmm.swap(POOL_ID, address(this), payload, "");
Expand Down Expand Up @@ -129,32 +126,4 @@ contract SYCoveredCallSwapTest is SYCoveredCallSetUp {
vm.expectRevert();
dfmm.swap(POOL_ID, address(this), payload, "");
}

function test_SYCoveredCall_swap_ChargesCorrectFeesYIn() public deep {
uint256 amountIn = 1 ether;
bool swapXForY = false;

(bool valid,,, bytes memory payload) =
solver.simulateSwap(POOL_ID, swapXForY, amountIn);

(,, uint256 inputAmount, uint256 outputAmount) =
dfmm.swap(POOL_ID, address(this), payload, "");

console2.log(inputAmount);
console2.log(outputAmount);
}

function test_SYCoveredCall_swap_ChargesCorrectFeesXIn() public deep {
uint256 amountIn = 1 ether;
bool swapXForY = true;

(bool valid,,, bytes memory payload) =
solver.simulateSwap(POOL_ID, swapXForY, amountIn);

(,, uint256 inputAmount, uint256 outputAmount) =
dfmm.swap(POOL_ID, address(this), payload, "");

console2.log(inputAmount);
console2.log(outputAmount);
}
}
16 changes: 13 additions & 3 deletions test/utils/MockStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ contract MockStrategy is IStrategy {
uint256 tokenOutIndex,
uint256 amountIn,
uint256 amountOut,
uint256 deltaLiquidity
uint256 deltaLiquidity,
bytes memory params
)
{
(
Expand All @@ -106,12 +107,21 @@ contract MockStrategy is IStrategy {
tokenOutIndex,
amountIn,
amountOut,
deltaLiquidity
deltaLiquidity,
params
) = abi.decode(
data, (bool, int256, uint256, uint256, uint256, uint256, uint256)
data,
(bool, int256, uint256, uint256, uint256, uint256, uint256, bytes)
);
}

function postSwapHook(
address,
uint256,
Pool calldata,
bytes calldata
) external pure override { }

function update(
address sender,
uint256 poolId,
Expand Down

0 comments on commit 3ec0b53

Please sign in to comment.