Skip to content

Commit

Permalink
fix(PerpV2BasisTradingModule): bytecode size limit bug (SetProtocol#200)
Browse files Browse the repository at this point in the history
* Set allowUnlimitedContractSize to false

* Fix PerpV2BasisTradingModule bytecode size limit

Add hardhat-contract-sizer as dev dependency
Add PerpV2Positions library
Add functions to PerpV2 library
Modify PerpV2LeverageModuel to use the new libraries
Fix existing interfaces

* Remove contractSizer config settings

* Delete commented stuff

* Add PerpV2Positions mock

* Fix tests by linking libraries during deployment

* Fix failing tests

* Fix warnings

* Use public getPositionNotionalInfo

* Fix failing integration & viewer tests

* Update to V2 for every contract; Make PositionsV2 library functions external

* Link PositionV2 library and fix PerpV2PositionV2 linking

* Fix failing perpV2BasisTradingModule tests

* Remove unnecessary todo

* Add tests for PositionV2 and ModuleBaseV2; Increase coverage

* Fix javadocs; Add changelogs; Add missing test cases; Add
PerpV2LeveageModuleV2<>SIM integration tests

* Add changelog in PerpV2LeverageModuleV2

* Fix javadocs and hardhat config

* Add tests for PerpV2LibraryV2

* Updated version to 0.1.13-basis.0

* Add PerpV2LibraryV2 tests

* Add tests for PerpV2Positions library

* Move formatAdjustments to PerpV2Positioons library

* Remove .only and bump package

* Attempt to fix existing flaky test

* Fix variable name; Removing trailing whitespaces

* Override initalize function

* Fix coverage; Add tests for intialize(old) function
  • Loading branch information
Sachin committed Mar 18, 2022
1 parent e48e673 commit ddfab38
Show file tree
Hide file tree
Showing 32 changed files with 12,872 additions and 145 deletions.
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

0 comments on commit ddfab38

Please sign in to comment.