Skip to content

Commit

Permalink
feat: introduce safecast safeguards
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Mar 4, 2024
1 parent ccb3390 commit 63fda06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/curve/constant-product/ConstantProductPair.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import { SafeCastLib } from "solady/utils/SafeCastLib.sol";

import { Math } from "@openzeppelin/utils/math/Math.sol";
import { FixedPointMathLib } from "solady/utils/FixedPointMathLib.sol";

Expand Down Expand Up @@ -245,9 +247,10 @@ contract ConstantProductPair is ReservoirPair {
previous.logAccClampedPrice + int56(logInstantClampedPrice) * int56(int256(uint256(aTimeElapsed)));
_slot0.index += 1;
_observations[_slot0.index] = Observation(
logInstantRawPrice,
logInstantClampedPrice,
logAccRawPrice,
// TODO: prove that these values are guaranteed <=int56 to remove these safe casts
SafeCastLib.toInt56(logInstantRawPrice),
SafeCastLib.toInt56(logInstantClampedPrice),
SafeCastLib.toInt56(logAccRawPrice),
logAccClampedPrice,
aCurrentTimestamp
);
Expand Down
9 changes: 6 additions & 3 deletions src/curve/stable/StablePair.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import { SafeCastLib } from "solady/utils/SafeCastLib.sol";

import { IReservoirCallee } from "src/interfaces/IReservoirCallee.sol";
import { IGenericFactory } from "src/interfaces/IGenericFactory.sol";

Expand Down Expand Up @@ -300,9 +302,10 @@ contract StablePair is ReservoirPair {
previous.logAccClampedPrice + int56(logInstantClampedPrice) * int56(int256(uint256(aTimeElapsed)));
_slot0.index += 1;
_observations[_slot0.index] = Observation(
logInstantRawPrice,
logInstantClampedPrice,
logAccRawPrice,
// TODO: prove that these values are guaranteed <=int56 to remove these safe casts
SafeCastLib.toInt56(logInstantRawPrice),
SafeCastLib.toInt56(logInstantClampedPrice),
SafeCastLib.toInt56(logAccRawPrice),
logAccClampedPrice,
aCurrentTimestamp
);
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/StableOracleMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ library StableOracleMath {

/// @notice Calculates the spot price of token1 in token0
/// @param amplificationParameter in precise form (see StableMath.A_PRECISION)
/// @param reserve0 token0 reserves normalized to 18 decimals
/// @param reserve1 token1 reserves normalized to 18 decimals
/// @return spotPrice price where 1e18 == 1
/// @param reserve0 normalized to 18 decimals
/// @param reserve1 normalized to 18 decimals
/// @return spotPrice 18 decimal fixed point number. Minimum price is 1e-18 (1 wei)
function calcSpotPrice(uint256 amplificationParameter, uint256 reserve0, uint256 reserve1)
internal
pure
Expand Down

0 comments on commit 63fda06

Please sign in to comment.