Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
71e3502
Set allowUnlimitedContractSize to false
Mar 9, 2022
46f71d5
Fix PerpV2BasisTradingModule bytecode size limit
Mar 11, 2022
da16e40
Remove contractSizer config settings
Mar 11, 2022
d4d5143
Delete commented stuff
Mar 11, 2022
28c13a8
Add PerpV2Positions mock
Mar 14, 2022
a025096
Fix tests by linking libraries during deployment
Mar 14, 2022
faf08bf
Fix failing tests
Mar 14, 2022
ad7b7b8
Fix warnings
Mar 14, 2022
f898ea0
Use public getPositionNotionalInfo
Mar 14, 2022
93bc54a
Fix failing integration & viewer tests
Mar 14, 2022
6cc578a
Update to V2 for every contract; Make PositionsV2 library functions e…
Mar 15, 2022
2f41f90
Link PositionV2 library and fix PerpV2PositionV2 linking
Mar 15, 2022
cdc382c
Fix failing perpV2BasisTradingModule tests
Mar 15, 2022
10c091e
Remove unnecessary todo
Mar 15, 2022
f61d144
Add tests for PositionV2 and ModuleBaseV2; Increase coverage
Mar 16, 2022
1e3effb
Fix javadocs; Add changelogs; Add missing test cases; Add
Mar 16, 2022
9e4efde
Add changelog in PerpV2LeverageModuleV2
Mar 16, 2022
c570678
Fix javadocs and hardhat config
Mar 16, 2022
8cb142a
Add tests for PerpV2LibraryV2
Mar 16, 2022
5e277c3
Updated version to 0.1.13-basis.0
Mar 16, 2022
0448da9
Add PerpV2LibraryV2 tests
Mar 17, 2022
4462762
Add tests for PerpV2Positions library
Mar 17, 2022
1ab4e0c
Move formatAdjustments to PerpV2Positioons library
Mar 17, 2022
1651761
Remove .only and bump package
Mar 17, 2022
d193f69
Attempt to fix existing flaky test
Mar 17, 2022
3062b18
Fix variable name; Removing trailing whitespaces
Mar 17, 2022
0d05add
Override initalize function
Mar 17, 2022
6e621f2
Fix coverage; Add tests for intialize(old) function
Mar 17, 2022
8160cfc
Merge branch 'master' into sachin/update-allowUnlimitedContractSize
Mar 18, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions contracts/interfaces/IPerpV2LeverageModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { IExchange } from "./external/perp-v2/IExchange.sol";
import { IVault } from "./external/perp-v2/IVault.sol";
import { IQuoter } from "./external/perp-v2/IQuoter.sol";
import { IMarketRegistry } from "./external/perp-v2/IMarketRegistry.sol";

import { PerpV2Positions } from "../protocol/integration/lib/PerpV2Positions.sol";

/**
* @title IPerpV2LeverageModule
Expand All @@ -42,18 +42,6 @@ interface IPerpV2LeverageModule {

/* ============ Structs ============ */

struct PositionNotionalInfo {
address baseToken; // Virtual token minted by the Perp protocol
int256 baseBalance; // Base position notional quantity in 10**18 decimals. When negative, position is short
int256 quoteBalance; // vUSDC "debt" notional quantity minted to open position. When positive, position is short
}

struct PositionUnitInfo {
address baseToken; // Virtual token minted by the Perp protocol
int256 baseUnit; // Base position unit. When negative, position is short
int256 quoteUnit; // vUSDC "debt" position unit. When positive, position is short
}

// Note: when `pendingFundingPayments` is positive it will be credited to account on settlement,
// when negative it's a debt owed that will be repaid on settlement. (PerpProtocol.Exchange returns the value
// with the opposite meaning, e.g positively signed payments are owed by account to system).
Expand Down Expand Up @@ -252,7 +240,7 @@ interface IPerpV2LeverageModule {
* + baseBalance: baseToken balance as notional quantity (10**18)
* + quoteBalance: USDC quote asset balance as notional quantity (10**18)
*/
function getPositionNotionalInfo(ISetToken _setToken) external view returns (PositionNotionalInfo[] memory);
function getPositionNotionalInfo(ISetToken _setToken) external view returns (PerpV2Positions.PositionNotionalInfo[] memory);

/**
* @dev Returns a PositionUnitInfo array representing all positions open for the SetToken.
Expand All @@ -265,7 +253,7 @@ interface IPerpV2LeverageModule {
* + baseUnit: baseToken balance as position unit (10**18)
* + quoteUnit: USDC quote asset balance as position unit (10**18)
*/
function getPositionUnitInfo(ISetToken _setToken) external view returns (PositionUnitInfo[] memory);
function getPositionUnitInfo(ISetToken _setToken) external view returns (PerpV2Positions.PositionUnitInfo[] memory);

/**
* @dev Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide
Expand Down
38 changes: 0 additions & 38 deletions contracts/interfaces/external/perp-v2/IMarketRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,15 @@ pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;

interface IMarketRegistry {
//
// STRUCT
//
struct MarketInfo {
address pool;
uint24 exchangeFeeRatio;
uint24 uniswapFeeRatio;
uint24 insuranceFundFeeRatio;
}

//
// EVENT
//
event PoolAdded(address indexed baseToken, uint24 indexed feeRatio, address indexed pool);
event FeeRatioChanged(address baseToken, uint24 feeRatio);
event InsuranceFundFeeRatioChanged(uint24 feeRatio);
event MaxOrdersPerMarketChanged(uint8 maxOrdersPerMarket);

//
// FUNCTION
//

function addPool(address baseToken, uint24 feeRatio) external returns (address);

function setFeeRatio(address baseToken, uint24 feeRatio) external;

function setInsuranceFundFeeRatio(address baseToken, uint24 insuranceFundFeeRatioArg) external;

function setMaxOrdersPerMarket(uint8 maxOrdersPerMarketArg) external;

//
// EXTERNAL VIEW
//

function getPool(address baseToken) external view returns (address);

function getFeeRatio(address baseToken) external view returns (uint24);

function getInsuranceFundFeeRatio(address baseToken) external view returns (uint24);

function getMarketInfo(address baseToken) external view returns (MarketInfo memory);

function getQuoteToken() external view returns (address);

function getUniswapV3Factory() external view returns (address);

function getMaxOrdersPerMarket() external view returns (uint8);

function hasPool(address baseToken) external view returns (bool);
}
139 changes: 139 additions & 0 deletions contracts/mocks/PositionV2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2020 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";

import { ISetToken } from "../interfaces/ISetToken.sol";
import { PositionV2 } from "../protocol/lib/PositionV2.sol";


// Mock contract implementation of PositionV2 functions
contract PositionV2Mock {
constructor()
public
{}

function initialize(ISetToken _setToken) external {
_setToken.initializeModule();
}

function testHasDefaultPosition(ISetToken _setToken, address _component) external view returns(bool) {
return PositionV2.hasDefaultPosition(_setToken, _component);
}

function testHasExternalPosition(ISetToken _setToken, address _component) external view returns(bool) {
return PositionV2.hasExternalPosition(_setToken, _component);
}
function testHasSufficientDefaultUnits(ISetToken _setToken, address _component, uint256 _unit) external view returns(bool) {
return PositionV2.hasSufficientDefaultUnits(_setToken, _component, _unit);
}
function testHasSufficientExternalUnits(
ISetToken _setToken,
address _component,
address _module,
uint256 _unit
)
external
view
returns(bool)
{
return PositionV2.hasSufficientExternalUnits(_setToken, _component, _module, _unit);
}

function testEditDefaultPosition(ISetToken _setToken, address _component, uint256 _newUnit) external {
return PositionV2.editDefaultPosition(_setToken, _component, _newUnit);
}

function testEditExternalPosition(
ISetToken _setToken,
address _component,
address _module,
int256 _newUnit,
bytes memory _data
)
external
{
PositionV2.editExternalPosition(_setToken, _component, _module, _newUnit, _data);
}

function testGetDefaultTotalNotional(
uint256 _setTokenSupply,
uint256 _positionUnit
)
external
pure
returns (uint256)
{
return PositionV2.getDefaultTotalNotional(_setTokenSupply, _positionUnit);
}

function testGetDefaultPositionUnit(
uint256 _setTokenSupply,
uint256 _totalNotional
)
external
pure
returns (uint256)
{
return PositionV2.getDefaultPositionUnit(_setTokenSupply, _totalNotional);
}

function testGetDefaultTrackedBalance(ISetToken _setToken, address _component)
external
view
returns (uint256)
{
return PositionV2.getDefaultTrackedBalance(_setToken, _component);
}

function testCalculateAndEditDefaultPosition(
ISetToken _setToken,
address _component,
uint256 _setTotalSupply,
uint256 _componentPreviousBalance
)
external
returns (uint256, uint256, uint256)
{
return PositionV2.calculateAndEditDefaultPosition(
_setToken,
_component,
_setTotalSupply,
_componentPreviousBalance
);
}

function testCalculateDefaultEditPositionUnit(
uint256 _setTokenSupply,
uint256 _preTotalNotional,
uint256 _postTotalNotional,
uint256 _prePositionUnit
)
external
pure
returns (uint256)
{
return PositionV2.calculateDefaultEditPositionUnit(
_setTokenSupply,
_preTotalNotional,
_postTotalNotional,
_prePositionUnit
);
}
}
156 changes: 156 additions & 0 deletions contracts/mocks/protocol/integration/lib/PerpV2LibraryV2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright 2021 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IClearingHouse } from "../../../../interfaces/external/perp-v2/IClearingHouse.sol";
import { IVault } from "../../../../interfaces/external/perp-v2/IVault.sol";
import { IQuoter } from "../../../../interfaces/external/perp-v2/IQuoter.sol";

import { PerpV2LibraryV2 } from "../../../../protocol/integration/lib/PerpV2LibraryV2.sol";
import { ISetToken } from "../../../../interfaces/ISetToken.sol";

/**
* @title PerpV2LibraryV2Mock
* @author Set Protocol
*
* Mock for PerpV2LibraryV2 Library contract. Used for testing PerpV2LibraryV2 Library contract, as the library
* contract can't be tested directly using ethers.js.
*/
contract PerpV2LibraryV2Mock {

/* ============ External ============ */

function testGetDepositCalldata(
IVault _vault,
IERC20 _asset,
uint256 _amountNotional
)
public
pure
returns (address, uint256, bytes memory)
{
return PerpV2LibraryV2.getDepositCalldata(_vault, _asset, _amountNotional);
}

function testInvokeDeposit(
ISetToken _setToken,
IVault _vault,
IERC20 _asset,
uint256 _amountNotional
)
external
{
return PerpV2LibraryV2.invokeDeposit(_setToken, _vault, _asset, _amountNotional);
}

function testGetWithdrawCalldata(
IVault _vault,
IERC20 _asset,
uint256 _amountNotional
)
public
pure
returns (address, uint256, bytes memory)
{
return PerpV2LibraryV2.getWithdrawCalldata(_vault, _asset, _amountNotional);
}

function testInvokeWithdraw(
ISetToken _setToken,
IVault _vault,
IERC20 _asset,
uint256 _amountNotional
)
external
{
return PerpV2LibraryV2.invokeWithdraw(_setToken, _vault, _asset, _amountNotional);
}

function testGetOpenPositionCalldata(
IClearingHouse _clearingHouse,
IClearingHouse.OpenPositionParams memory _params
)
public
pure
returns (address, uint256, bytes memory)
{
return PerpV2LibraryV2.getOpenPositionCalldata(_clearingHouse, _params);
}

function testInvokeOpenPosition(
ISetToken _setToken,
IClearingHouse _clearingHouse,
IClearingHouse.OpenPositionParams memory _params
)
external
returns (uint256 deltaBase, uint256 deltaQuote)
{
return PerpV2LibraryV2.invokeOpenPosition(_setToken, _clearingHouse, _params);
}

function testGetSwapCalldata(
IQuoter _quoter,
IQuoter.SwapParams memory _params
)
public
pure
returns (address, uint256, bytes memory)
{
return PerpV2LibraryV2.getSwapCalldata(_quoter, _params);
}

function testInvokeSwap(
ISetToken _setToken,
IQuoter _quoter,
IQuoter.SwapParams memory _params
)
external
returns (IQuoter.SwapResponse memory)
{
return PerpV2LibraryV2.invokeSwap(_setToken, _quoter, _params);
}

function testSimulateTrade(
PerpV2LibraryV2.ActionInfo memory _actionInfo,
IQuoter _perpQuoter
)
external
returns (uint256, uint256)
{
return PerpV2LibraryV2.simulateTrade(_actionInfo, _perpQuoter);
}

function testExecuteTrade(
PerpV2LibraryV2.ActionInfo memory _actionInfo,
IClearingHouse _perpClearingHouse
)
external
returns (uint256, uint256)
{
return PerpV2LibraryV2.executeTrade(_actionInfo, _perpClearingHouse);
}

/* ============ Helper Functions ============ */

function initializeModuleOnSet(ISetToken _setToken) external {
_setToken.initializeModule();
}
}
Loading