Skip to content

Commit

Permalink
fix early exit/pro rata condition checks, fix valid constructor test,…
Browse files Browse the repository at this point in the history
… fix relative dutch auction assertions
  • Loading branch information
kinrezC committed Oct 22, 2024
1 parent bd24900 commit 39d413b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
15 changes: 11 additions & 4 deletions src/Doppler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {FixedPoint96} from "v4-periphery/lib/v4-core/src/libraries/FixedPoint96.
import {TransientStateLibrary} from "v4-periphery/lib/v4-core/src/libraries/TransientStateLibrary.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {ProtocolFeeLibrary} from "v4-periphery/lib/v4-core/src/libraries/ProtocolFeeLibrary.sol";
import "forge-std/console.sol";

struct SlugData {
int24 tickLower;
Expand Down Expand Up @@ -174,8 +175,11 @@ contract Doppler is BaseHook {
}

// only check proceeds if we're after maturity and we haven't already triggered insufficient proceeds
console.log("minimumProceeds", minimumProceeds);
console.log("state.totalProceeds", state.totalProceeds);
if (block.timestamp > endingTime && !insufficientProceeds) {
if (state.totalProceeds < minimumProceeds) {
console.log("triggering insufficient proceeds");
insufficientProceeds = true;

PoolId poolId = key.toId();
Expand Down Expand Up @@ -218,11 +222,13 @@ contract Doppler is BaseHook {
for (uint256 i; i < numPDSlugs + 1; ++i) {
delete positions[bytes32(uint256(2 + i))];
}
} else {
revert InvalidSwapAfterMaturitySufficientProceeds();
}
}

if (block.timestamp > endingTime && !insufficientProceeds) {
revert InvalidSwapAfterMaturitySufficientProceeds();
}

if (!insufficientProceeds) {
_rebalance(key);
} else if (isToken0) {
Expand All @@ -232,7 +238,7 @@ contract Doppler is BaseHook {
}
} else {
if (swapParams.zeroForOne == true) {
revert InvalidSwapAfterMaturitySufficientProceeds();
revert InvalidSwapAfterMaturityInsufficientProceeds();
}
}

Expand Down Expand Up @@ -429,7 +435,8 @@ contract Doppler is BaseHook {

SlugData memory lowerSlug =
_computeLowerSlugData(key, requiredProceeds, numeraireAvailable, totalTokensSold_, tickLower, currentTick);
(SlugData memory upperSlug, uint256 assetRemaining) = _computeUpperSlugData(key, totalTokensSold_, currentTick, assetAvailable);
(SlugData memory upperSlug, uint256 assetRemaining) =
_computeUpperSlugData(key, totalTokensSold_, currentTick, assetAvailable);
SlugData[] memory priceDiscoverySlugs =
_computePriceDiscoverySlugsData(key, upperSlug, tickUpper, assetRemaining);

Expand Down
7 changes: 3 additions & 4 deletions test/integration/Rebalance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {InvalidTime, SwapBelowRange} from "src/Doppler.sol";
import {BaseTest} from "test/shared/BaseTest.sol";
import {Position} from "../../src/Doppler.sol";


contract RebalanceTest is BaseTest {
using PoolIdLibrary for PoolKey;
using StateLibrary for IPoolManager;
Expand Down Expand Up @@ -484,8 +483,7 @@ contract RebalanceTest is BaseTest {

buy(1 ether);

(uint40 lastEpoch,, uint256 totalTokensSold,, uint256 totalTokensSoldLastEpoch,) =
hook.state();
(uint40 lastEpoch,, uint256 totalTokensSold,, uint256 totalTokensSoldLastEpoch,) = hook.state();

assertEq(lastEpoch, 1);
// We sold 1e18 tokens just now
Expand Down Expand Up @@ -652,7 +650,8 @@ contract RebalanceTest is BaseTest {
}

// Lower slug upper tick should be at the currentTick
assertEq(lowerSlug.tickUpper, currentTick, "lowerSlug.tickUpper not at currentTick");
// use abs because if !istoken0 the tick will be currentTick - 1 because swappingn 1 wei causes us to round down
assertApproxEqAbs(lowerSlug.tickUpper, currentTick, 1, "lowerSlug.tickUpper not at currentTick");

// All slugs must be set
assertNotEq(lowerSlug.liquidity, 0, "lowerSlug.liquidity is 0");
Expand Down
7 changes: 5 additions & 2 deletions test/shared/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ contract BaseTest is Test, Deployers {

// isToken0 ? startTick > endTick : endTick > startTick
// In both cases, price(startTick) > price(endTick)
startTick = isToken0 ? int24(vm.envOr("START_TICK", DEFAULT_START_TICK)) : int24(vm.envOr("START_TICK", -DEFAULT_START_TICK));
endTick = isToken0 ? int24(vm.envOr("END_TICK", -DEFAULT_END_TICK)) : int24(vm.envOr("END_TICK", DEFAULT_END_TICK));
startTick = isToken0
? int24(vm.envOr("START_TICK", DEFAULT_START_TICK))
: int24(vm.envOr("START_TICK", -DEFAULT_START_TICK));
endTick =
isToken0 ? int24(vm.envOr("END_TICK", -DEFAULT_END_TICK)) : int24(vm.envOr("END_TICK", DEFAULT_END_TICK));

// Default to feeless case because it's easier to reason about
config.fee = uint24(vm.envOr("FEE", uint24(0)));
Expand Down
5 changes: 2 additions & 3 deletions test/unit/Constructor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,9 @@ contract ConstructorTest is BaseTest {
}

function testConstructor_Succeeds_WithValidParameters() public {
bool _isToken0 = true;

DopplerConfig memory config = DEFAULT_DOPPLER_CONFIG;
bool _isToken0 = true;

deployDoppler(0, config, 0, 0, _isToken0);
deployDoppler(0, config, 0, 0, asset < numeraire);
}
}
10 changes: 2 additions & 8 deletions test/unit/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ contract SwapTest is BaseTest {

int256 minimumProceeds = int256(hook.getMinimumProceeds());

swapRouter.swap(
// Swap numeraire to asset
// If zeroForOne, we use max price limit (else vice versa)
key,
IPoolManager.SwapParams(!isToken0, -minimumProceeds / 2, !isToken0 ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT),
PoolSwapTest.TestSettings(true, false),
""
);
buy(-minimumProceeds / 2);

vm.warp(hook.getEndingTime() + 1); // 1 second after the end time

vm.expectRevert(
Expand Down

0 comments on commit 39d413b

Please sign in to comment.