Skip to content

Commit

Permalink
Merge pull request #39 from primitivefinance/feat/improvements
Browse files Browse the repository at this point in the history
Feat/improvements
  • Loading branch information
clemlak authored Sep 28, 2021
2 parents e7173a3 + 028c7d6 commit fd7388a
Show file tree
Hide file tree
Showing 30 changed files with 292 additions and 195 deletions.
42 changes: 15 additions & 27 deletions contracts/PrimitiveHouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ pragma solidity 0.8.6;
import "@primitivefinance/v2-core/contracts/interfaces/engine/IPrimitiveEngineView.sol";

import "./interfaces/IPrimitiveHouse.sol";
import "./libraries/TransferHelper.sol";

import "./base/Multicall.sol";
import "./base/CashManager.sol";
import "./base/SelfPermit.sol";
import "./base/PositionWrapper.sol";
import "./base/PositionManager.sol";
import "./base/SwapManager.sol";

import "hardhat/console.sol";
import "./libraries/TransferHelper.sol";

/// @title Primitive House
/// @author Primitive
Expand All @@ -22,7 +19,7 @@ contract PrimitiveHouse is
Multicall,
CashManager,
SelfPermit,
PositionWrapper,
PositionManager,
SwapManager
{
using TransferHelper for IERC20;
Expand All @@ -37,7 +34,7 @@ contract PrimitiveHouse is
address _factory,
address _WETH10,
string memory _URI
) HouseBase(_factory, _WETH10) PositionWrapper(_URI) {}
) HouseBase(_factory, _WETH10) PositionManager(_URI) {}

/// @inheritdoc IPrimitiveHouse
function create(
Expand All @@ -47,7 +44,7 @@ contract PrimitiveHouse is
uint256 strike,
uint64 sigma,
uint32 maturity,
uint256 delta,
uint256 riskyPerLp,
uint256 delLiquidity,
bool shouldTokenizeLiquidity
) external override lock returns (
Expand All @@ -69,7 +66,7 @@ contract PrimitiveHouse is
strike,
sigma,
maturity,
delta,
riskyPerLp,
delLiquidity,
abi.encode(callbackData)
);
Expand All @@ -84,22 +81,21 @@ contract PrimitiveHouse is
/// @inheritdoc IPrimitiveHouse
function allocate(
address engine,
bytes32 poolId,
address risky,
address stable,
bytes32 poolId,
uint256 delLiquidity,
uint256 delRisky,
uint256 delStable,
bool fromMargin,
bool shouldTokenizeLiquidity
) external override lock returns (
uint256 delRisky,
uint256 delStable
) {
if (delLiquidity == 0) revert ZeroLiquidityError();
) external override lock returns (uint256 delLiquidity) {
if (delRisky == 0 && delStable == 0) revert ZeroLiquidityError();

(delRisky, delStable) = IPrimitiveEngineActions(engine).allocate(
(delLiquidity) = IPrimitiveEngineActions(engine).allocate(
poolId,
address(this),
delLiquidity,
delRisky,
delStable,
fromMargin,
abi.encode(
CallbackData({
Expand Down Expand Up @@ -138,7 +134,7 @@ contract PrimitiveHouse is
emit Remove(msg.sender, engine, poolId, delLiquidity, delRisky, delStable);
}

// ===== Callback Implementations =====
/// CALLBACK IMPLEMENTATIONS ///

/// @inheritdoc IPrimitiveCreateCallback
function createCallback(
Expand Down Expand Up @@ -170,12 +166,4 @@ contract PrimitiveHouse is
if (delRisky > 0) TransferHelper.safeTransferFrom(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable > 0) TransferHelper.safeTransferFrom(decoded.stable, decoded.payer, msg.sender, delStable);
}

// TODO: Delete this callback when the interface will be updated
function removeCallback(
uint256 delRisky,
uint256 delStable,
bytes calldata data
) external override {
}
}
1 change: 0 additions & 1 deletion contracts/base/CashManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity 0.8.6;

import "../interfaces/ICashManager.sol";
import "../base/HouseBase.sol";

import "../libraries/TransferHelper.sol";
import "../interfaces/IWETH10.sol";

Expand Down
2 changes: 0 additions & 2 deletions contracts/base/HouseBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ pragma solidity 0.8.6;

import "../interfaces/IHouseBase.sol";
import "../interfaces/IPrimitiveHouse.sol";

import "./Reentrancy.sol";

import "../libraries/EngineAddress.sol";

/// @title HouseBase
Expand Down
1 change: 0 additions & 1 deletion contracts/base/MarginManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "@primitivefinance/v2-core/contracts/libraries/Margin.sol";

import "../interfaces/IMarginManager.sol";
import "./HouseBase.sol";

import "../libraries/TransferHelper.sol";

/// @title MarginManager
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/Multicall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.6;
import "../interfaces/IMulticall.sol";

/// @title Multicall
/// @author Primitive
/// @author Uniswap (https://github.com/Uniswap/v3-periphery/blob/main/contracts/base/Multicall.sol)
/// @notice Enables calling multiple methods in a single call to the contract
abstract contract Multicall is IMulticall {
/// @inheritdoc IMulticall
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/NFTGatekeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "../interfaces/INFTGatekeeper.sol";

/// @title NFTGatekeeper
/// @author Primitive
/// @notice Restricts access to holders of a specific NFT
/// @notice Limits access to holders of a specific NFT
contract NFTGatekeeper is INFTGatekeeper {
/// @inheritdoc INFTGatekeeper
uint256 public immutable override id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ pragma solidity 0.8.6;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

import "../interfaces/IPositionWrapper.sol";
import "../interfaces/IPositionManager.sol";

/// @title PositionWrapper
/// @title PositionManager
/// @author Primitive
/// @notice Wraps the positions into ERC1155 tokens
abstract contract PositionWrapper is IPositionWrapper, ERC1155 {
/// @inheritdoc IPositionWrapper
abstract contract PositionManager is IPositionManager, ERC1155 {
/// @inheritdoc IPositionManager
mapping(address => mapping(bytes32 => uint256)) public override liquidityOf;

/// @param _URI The address of the base URI
constructor(string memory _URI) ERC1155(_URI) {}

bytes private empty;
Expand Down Expand Up @@ -47,7 +48,7 @@ abstract contract PositionWrapper is IPositionWrapper, ERC1155 {
}
}

/// @inheritdoc IPositionWrapper
/// @inheritdoc IPositionManager
function wrapLiquidity(
bytes32 poolId,
uint256 amount
Expand All @@ -61,7 +62,7 @@ abstract contract PositionWrapper is IPositionWrapper, ERC1155 {
_mint(msg.sender, uint256(poolId), amount, empty);
}

/// @inheritdoc IPositionWrapper
/// @inheritdoc IPositionManager
function unwrapLiquidity(
bytes32 poolId,
uint256 amount
Expand Down
1 change: 1 addition & 0 deletions contracts/base/Reentrancy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity 0.8.6;
/// @author Primitive
/// @notice Prevents reentrancy
contract Reentrancy {
/// @notice Thrown when a call to the contract is made during a locked state
error LockedError();

uint256 private unlocked = 1;
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/SelfPermit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import "../interfaces/IERC20PermitAllowed.sol";
import "../interfaces/ISelfPermit.sol";

/// @title Self Permit
/// @author Uniswap (https://github.com/Uniswap/v3-periphery)
/// @notice Functionality to call permit on any EIP-2612-compliant token for use in the route
/// @dev These functions are expected to be embedded in multicalls to allow EOAs to approve a contract and call a function
/// that requires an approval in a single transaction.
/// Credits goes to Uniswap (https://github.com/Uniswap/v3-periphery)
abstract contract SelfPermit is ISelfPermit {
/// @inheritdoc ISelfPermit
function selfPermit(
Expand Down
19 changes: 8 additions & 11 deletions contracts/base/SwapManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import "@primitivefinance/v2-core/contracts/interfaces/engine/IPrimitiveEngineAc
import "@primitivefinance/v2-core/contracts/interfaces/engine/IPrimitiveEngineView.sol";

import "../interfaces/ISwapManager.sol";

import "../interfaces/IERC20.sol";

import "./MarginManager.sol";
import "./HouseBase.sol";

import "hardhat/console.sol";

/// @title SwapManager
/// @author Primitive
/// @dev Manages the swaps
Expand All @@ -21,11 +17,13 @@ abstract contract SwapManager is ISwapManager, HouseBase, MarginManager {
using Margin for mapping(address => Margin.Data);
using Margin for Margin.Data;

/// @notice Reverts the tx above the deadline
modifier checkDeadline(uint256 deadline) {
if (_blockTimestamp() > deadline) revert DeadlineReachedError();
_;
}

/// @inheritdoc ISwapManager
function swap(
SwapParameters memory params
) external override lock checkDeadline(params.deadline) returns (
Expand All @@ -38,6 +36,7 @@ abstract contract SwapManager is ISwapManager, HouseBase, MarginManager {
});

deltaOut = IPrimitiveEngineActions(params.engine).swap(
params.toMargin ? address(this) : params.recipient,
params.poolId,
params.riskyForStable,
params.deltaIn,
Expand All @@ -57,28 +56,26 @@ abstract contract SwapManager is ISwapManager, HouseBase, MarginManager {
}

if (params.toMargin) {
margins[msg.sender][params.engine].deposit(
margins[params.recipient][params.engine].deposit(
params.riskyForStable ? params.deltaIn : 0,
params.riskyForStable ? 0 : params.deltaIn
);
} else {
TransferHelper.safeTransfer(
params.riskyForStable ? params.stable : params.risky,
msg.sender,
deltaOut);
}

emit Swap(
msg.sender,
params.recipient,
params.engine,
params.poolId,
params.riskyForStable,
params.deltaIn,
deltaOut,
params.fromMargin
params.fromMargin,
params.toMargin
);
}

/// @inheritdoc IPrimitiveSwapCallback
function swapCallback(
uint256 delRisky,
uint256 delStable,
Expand Down
4 changes: 2 additions & 2 deletions contracts/interfaces/ICashManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pragma solidity 0.8.6;
interface ICashManager {
/// EVENTS ///

/// @notice Emitted when the sender is not WETH
/// @notice Thrown when the sender is not WETH
/// @param expected The expected sender (WETH)
/// @param actual The actual sender
error WrongSender(address expected, address actual);

/// @notice Emmited when the amount required is above balance
/// @notice Thrown when the amount required is above balance
/// @param expected The expected amount
/// @param actual The actual amount
error AmountTooLow(uint256 expected, uint256 actual);
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.6;

/// @title ERC20 Interface
/// @author Primitive
interface IERC20 {
function totalSupply() external view returns (uint256);

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IERC20PermitAllowed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pragma solidity 0.8.6;

/// @title Interface for permit
/// @author Uniswap (https://github.com/Uniswap/v3-periphery)
/// @notice Interface used by DAI/CHAI for permit
/// @dev Credits goes to Uniswap (https://github.com/Uniswap/v3-periphery)
interface IERC20PermitAllowed {
/// @notice Approve the spender to spend some tokens via the holder signature
/// @dev This is the permit interface used by DAI and CHAI
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/IHouseBase.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.6;

/// @title HouseBase Interface
/// @author Primitive
interface IHouseBase {
/// @notice Thrown when the sender is not an engine
error NotEngineError();

/// @notice Returns the address of the factory
function factory() external returns (address);

/// @notice Returns the address of WETH10
function WETH10() external returns (address);
}
Loading

0 comments on commit fd7388a

Please sign in to comment.