Skip to content

Commit

Permalink
Merge pull request #268 from primitivefinance/audit/tob
Browse files Browse the repository at this point in the history
Release Candidate 1: ToB Audit Fixes
  • Loading branch information
Alexangelj authored Jan 30, 2022
2 parents 5dcf430 + 1c8ba12 commit d18c386
Show file tree
Hide file tree
Showing 59 changed files with 5,863 additions and 961 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ typechain/
.env.test
.npmrc



crytic-export/
corpus
e2e_swap_corpus/
e2e_swap_2_corpus/
12 changes: 6 additions & 6 deletions contracts/PrimitiveEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ contract PrimitiveEngine is IPrimitiveEngine {
if (strike == 0) revert StrikeError(strike);
if (delLiquidity <= MIN_LIQUIDITY) revert MinLiquidityError(delLiquidity);
if (riskyPerLp > PRECISION / factor0 || riskyPerLp == 0) revert RiskyPerLpError(riskyPerLp);
if (gamma >= Units.PERCENTAGE || gamma < 9000) revert GammaError(gamma);
if (gamma > Units.PERCENTAGE || gamma < 9000) revert GammaError(gamma);

Calibration memory cal = Calibration({
strike: strike,
Expand All @@ -185,15 +185,16 @@ contract PrimitiveEngine is IPrimitiveEngine {
if (delRisky == 0 || delStable == 0) revert CalibrationError(delRisky, delStable);

calibrations[poolId] = cal; // state update
liquidity[msg.sender][poolId] += delLiquidity - MIN_LIQUIDITY; // burn min liquidity, at cost of msg.sender
uint256 amount = delLiquidity - MIN_LIQUIDITY;
liquidity[msg.sender][poolId] += amount; // burn min liquidity, at cost of msg.sender
reserves[poolId].allocate(delRisky, delStable, delLiquidity, cal.lastTimestamp); // state update

(uint256 balRisky, uint256 balStable) = (balanceRisky(), balanceStable());
IPrimitiveCreateCallback(msg.sender).createCallback(delRisky, delStable, data);
checkRiskyBalance(balRisky + delRisky);
checkStableBalance(balStable + delStable);

emit Create(msg.sender, cal.strike, cal.sigma, cal.maturity, cal.gamma);
emit Create(msg.sender, cal.strike, cal.sigma, cal.maturity, cal.gamma, delRisky, delStable, amount);
}

// ===== Margin =====
Expand Down Expand Up @@ -246,7 +247,6 @@ contract PrimitiveEngine is IPrimitiveEngine {
Reserve.Data storage reserve = reserves[poolId];
if (reserve.blockTimestamp == 0) revert UninitializedError();
uint32 timestamp = _blockTimestamp();
if (timestamp > calibrations[poolId].maturity) revert PoolExpiredError();

uint256 liquidity0 = (delRisky * reserve.liquidity) / uint256(reserve.reserveRisky);
uint256 liquidity1 = (delStable * reserve.liquidity) / uint256(reserve.reserveStable);
Expand All @@ -265,7 +265,7 @@ contract PrimitiveEngine is IPrimitiveEngine {
checkStableBalance(balStable + delStable);
}

emit Allocate(msg.sender, recipient, poolId, delRisky, delStable);
emit Allocate(msg.sender, recipient, poolId, delRisky, delStable, delLiquidity);
}

/// @inheritdoc IPrimitiveEngineActions
Expand All @@ -284,7 +284,7 @@ contract PrimitiveEngine is IPrimitiveEngine {
reserve.remove(delRisky, delStable, delLiquidity, _blockTimestamp());
margins[msg.sender].deposit(delRisky, delStable);

emit Remove(msg.sender, poolId, delRisky, delStable);
emit Remove(msg.sender, poolId, delRisky, delStable, delLiquidity);
}

struct SwapDetails {
Expand Down
2 changes: 1 addition & 1 deletion contracts/PrimitiveFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ contract PrimitiveFactory is IPrimitiveFactory {
engine = address(new PrimitiveEngine{salt: keccak256(abi.encode(risky, stable))}());
delete args;
}
}
}
Loading

0 comments on commit d18c386

Please sign in to comment.