Skip to content

Commit

Permalink
Merge pull request #247 from primitivefinance/audit/sherlock-audit-v2
Browse files Browse the repository at this point in the history
Audit/sherlock audit v2
  • Loading branch information
SherlockProtocol authored Nov 5, 2021
2 parents b0a275f + 480cff2 commit f54c8b9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
25 changes: 12 additions & 13 deletions contracts/PrimitiveEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ contract PrimitiveEngine is IPrimitiveEngine {
/// @inheritdoc IPrimitiveEngineView
uint256 public immutable override MIN_LIQUIDITY;
/// @inheritdoc IPrimitiveEngineView
uint256 public immutable override scaleFactorRisky;
/// @inheritdoc IPrimitiveEngineView
uint256 public immutable override scaleFactorStable;
/// @inheritdoc IPrimitiveEngineView
address public immutable override factory;
/// @inheritdoc IPrimitiveEngineView
address public immutable override risky;
/// @inheritdoc IPrimitiveEngineView
address public immutable override stable;
/// @inheritdoc IPrimitiveEngineView
uint256 public immutable override scaleFactorRisky;
/// @inheritdoc IPrimitiveEngineView
uint256 public immutable override scaleFactorStable;
/// @dev Reentrancy guard initialized to state
uint8 private unlocked = 1;
/// @inheritdoc IPrimitiveEngineView
mapping(bytes32 => Calibration) public override calibrations;
/// @inheritdoc IPrimitiveEngineView
Expand All @@ -69,8 +71,6 @@ contract PrimitiveEngine is IPrimitiveEngine {
/// @inheritdoc IPrimitiveEngineView
mapping(address => mapping(bytes32 => uint256)) public override liquidity;

uint8 private unlocked = 1;

modifier lock() {
if (unlocked != 1) revert LockedError();

Expand Down Expand Up @@ -142,7 +142,7 @@ contract PrimitiveEngine is IPrimitiveEngine {

/// @inheritdoc IPrimitiveEngineActions
function create(
uint256 strike,
uint128 strike,
uint32 sigma,
uint32 maturity,
uint32 gamma,
Expand All @@ -160,8 +160,7 @@ contract PrimitiveEngine is IPrimitiveEngine {
)
{
(uint256 factor0, uint256 factor1) = (scaleFactorRisky, scaleFactorStable);
uint128 scaledStrike = strike.toUint128();
poolId = keccak256(abi.encodePacked(address(this), scaledStrike, sigma, maturity, gamma));
poolId = keccak256(abi.encodePacked(address(this), strike, sigma, maturity, gamma));
if (calibrations[poolId].lastTimestamp != 0) revert PoolDuplicateError();
if (sigma > 1e7 || sigma < 100) revert SigmaError(sigma);
if (strike == 0) revert StrikeError(strike);
Expand All @@ -170,7 +169,7 @@ contract PrimitiveEngine is IPrimitiveEngine {
if (gamma >= Units.PERCENTAGE || gamma < 9000) revert GammaError(gamma);

Calibration memory cal = Calibration({
strike: scaledStrike,
strike: strike,
sigma: sigma,
maturity: maturity,
lastTimestamp: _blockTimestamp(),
Expand Down Expand Up @@ -289,13 +288,13 @@ contract PrimitiveEngine is IPrimitiveEngine {

struct SwapDetails {
address recipient;
bytes32 poolId;
uint256 deltaIn;
uint256 deltaOut;
bool riskyForStable;
bool fromMargin;
bool toMargin;
uint32 timestamp;
bytes32 poolId;
uint256 deltaIn;
uint256 deltaOut;
}

/// @inheritdoc IPrimitiveEngineActions
Expand Down
2 changes: 1 addition & 1 deletion contracts/PrimitiveFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract PrimitiveFactory is IPrimitiveFactory {
) internal returns (address engine) {
(uint256 riskyDecimals, uint256 stableDecimals) = (IERC20(risky).decimals(), IERC20(stable).decimals());
if (riskyDecimals > 18 || riskyDecimals < 6) revert DecimalsError(riskyDecimals);
if (stableDecimals > 18 || stableDecimals < 6) revert DecimalsError(riskyDecimals);
if (stableDecimals > 18 || stableDecimals < 6) revert DecimalsError(stableDecimals);

uint256 scaleFactorRisky = 10**(18 - riskyDecimals);
uint256 scaleFactorStable = 10**(18 - stableDecimals);
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/engine/IPrimitiveEngineActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface IPrimitiveEngineActions {
/// delRisky Total amount of risky tokens provided to reserves
/// delStable Total amount of stable tokens provided to reserves
function create(
uint256 strike,
uint128 strike,
uint32 sigma,
uint32 maturity,
uint32 gamma,
Expand Down
4 changes: 2 additions & 2 deletions contracts/interfaces/engine/IPrimitiveEngineErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ interface IPrimitiveEngineErrors {
/// @notice Thrown when calling an expired pool, where block.timestamp > maturity, + BUFFER if swap
error PoolExpiredError();

/// @notice Thrown when liquidity is lower than the minimum amount of liquidity
/// @notice Thrown when liquidity is lower than or equal to the minimum amount of liquidity
error MinLiquidityError(uint256 value);

/// @notice Thrown when riskyPerLp is outside the range of acceptable values, 0 < riskyPerLp < 1eRiskyDecimals
error RiskyPerLpError(uint256 value);

/// @notice Thrown when sigma is outside the range of acceptable values, 100 < sigma < 1e7 with 4 precision
/// @notice Thrown when sigma is outside the range of acceptable values, 100 <= sigma <= 1e7 with 4 precision
error SigmaError(uint256 value);

/// @notice Thrown when strike is not valid, i.e. equal to 0 or greater than 2^128
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/engine/IPrimitiveEngineEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IPrimitiveEngineEvents {
/// @param sigma Implied Volatility of the pool
/// @param maturity Maturity timestamp of the pool
/// @param gamma 1 - Fee % of the pool, as an integer with precision of 1e4
event Create(address indexed from, uint256 indexed strike, uint256 sigma, uint256 indexed maturity, uint256 gamma);
event Create(address indexed from, uint128 indexed strike, uint32 sigma, uint32 indexed maturity, uint32 gamma);

/// @notice Updates the time until expiry of the pool with `poolId`
/// @param poolId Pool Identifier
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/TestRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract TestRouter is TestBase {
) public {
caller = msg.sender;
IPrimitiveEngine(engine).create(
strike,
uint128(strike),
uint32(sigma),
uint32(maturity),
uint32(gamma),
Expand Down

0 comments on commit f54c8b9

Please sign in to comment.