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

audit(16): Round decays in favor of swapper #305

Merged
merged 11 commits into from
Oct 18, 2024
59 changes: 59 additions & 0 deletions src/lib/DutchDecayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ library DutchDecayLib {
return uint256(linearDecay(startPoint, endPoint, currentPoint, int256(startAmount), int256(endAmount)));
}

/// @notice returns the linear interpolation between the two points
/// @param startPoint The start of the decay
/// @param endPoint The end of the decay
/// @param currentPoint The current position in the decay
/// @param startAmount The amount of the start of the decay
/// @param endAmount The amount of the end of the decay
function v3LinearDecay(
uint256 startPoint,
uint256 endPoint,
uint256 currentPoint,
uint256 startAmount,
uint256 endAmount,
bool isInput
) internal pure returns (uint256) {
return
uint256(v3LinearDecay(startPoint, endPoint, currentPoint, int256(startAmount), int256(endAmount), isInput));
}

/// @notice returns the linear interpolation between the two points
/// @param startPoint The start of the decay
/// @param endPoint The end of the decay
Expand Down Expand Up @@ -84,6 +102,47 @@ library DutchDecayLib {
return startAmount + delta;
}

/// @notice returns the linear interpolation between the two points
/// @param startPoint The start of the decay
/// @param endPoint The end of the decay
/// @param currentPoint The current position in the decay
/// @param startAmount The amount of the start of the decay
/// @param endAmount The amount of the end of the decay
/// @dev rounds in favor of the swapper based on input or output
function v3LinearDecay(
uint256 startPoint,
uint256 endPoint,
uint256 currentPoint,
int256 startAmount,
int256 endAmount,
bool isInput
) internal pure returns (int256) {
if (currentPoint >= endPoint) {
return endAmount;
}
uint256 elapsed = currentPoint - startPoint;
uint256 duration = endPoint - startPoint;
int256 delta;
if (isInput) {
// Because startAmount + delta is subtracted from the original amount,
// we want to maximize startAmount + delta to favor the swapper
if (endAmount < startAmount) {
delta = -int256(uint256(startAmount - endAmount).mulDivDown(elapsed, duration));
} else {
delta = int256(uint256(endAmount - startAmount).mulDivUp(elapsed, duration));
}
} else {
// For outputs, we want to minimize startAmount + delta to favor the swapper
if (endAmount < startAmount) {
delta = -int256(uint256(startAmount - endAmount).mulDivUp(elapsed, duration));
} else {
delta = int256(uint256(endAmount - startAmount).mulDivDown(elapsed, duration));
}
}

return startAmount + delta;
}

/// @notice returns a decayed output using the given dutch spec and times
/// @param output The output to decay
/// @param decayStartTime The time to start decaying
Expand Down
10 changes: 6 additions & 4 deletions src/lib/NonlinearDutchDecayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ library NonlinearDutchDecayLib {
uint256 startAmount,
uint256 decayStartBlock,
uint256 minAmount,
uint256 maxAmount
uint256 maxAmount,
bool isInput
) internal view returns (uint256 decayedAmount) {
// mismatch of relativeAmounts and relativeBlocks
if (curve.relativeAmounts.length > 16) {
Expand All @@ -43,7 +44,8 @@ library NonlinearDutchDecayLib {
(uint16 startPoint, uint16 endPoint, int256 relStartAmount, int256 relEndAmount) =
locateCurvePosition(curve, blockDelta);
// get decay of only the relative amounts
int256 curveDelta = DutchDecayLib.linearDecay(startPoint, endPoint, blockDelta, relStartAmount, relEndAmount);
int256 curveDelta =
DutchDecayLib.v3LinearDecay(startPoint, endPoint, blockDelta, relStartAmount, relEndAmount, isInput);

return startAmount.boundedSub(curveDelta, minAmount, maxAmount);
}
Expand Down Expand Up @@ -95,7 +97,7 @@ library NonlinearDutchDecayLib {
returns (OutputToken memory result)
{
uint256 decayedOutput =
decay(output.curve, output.startAmount, decayStartBlock, output.minAmount, type(uint256).max);
decay(output.curve, output.startAmount, decayStartBlock, output.minAmount, type(uint256).max, false);
result = OutputToken(output.token, decayedOutput, output.recipient);
}

Expand Down Expand Up @@ -124,7 +126,7 @@ library NonlinearDutchDecayLib {
view
returns (InputToken memory result)
{
uint256 decayedInput = decay(input.curve, input.startAmount, decayStartBlock, 0, input.maxAmount);
uint256 decayedInput = decay(input.curve, input.startAmount, decayStartBlock, 0, input.maxAmount, true);
result = InputToken(input.token, decayedInput, input.maxAmount);
}
}
Loading
Loading