Skip to content

Commit ddfab38

Browse files
author
Sachin
authored
fix(PerpV2BasisTradingModule): bytecode size limit bug (SetProtocol#200)
* 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
1 parent e48e673 commit ddfab38

32 files changed

+12872
-145
lines changed

contracts/interfaces/IPerpV2LeverageModule.sol

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { IExchange } from "./external/perp-v2/IExchange.sol";
2828
import { IVault } from "./external/perp-v2/IVault.sol";
2929
import { IQuoter } from "./external/perp-v2/IQuoter.sol";
3030
import { IMarketRegistry } from "./external/perp-v2/IMarketRegistry.sol";
31-
31+
import { PerpV2Positions } from "../protocol/integration/lib/PerpV2Positions.sol";
3232

3333
/**
3434
* @title IPerpV2LeverageModule
@@ -42,18 +42,6 @@ interface IPerpV2LeverageModule {
4242

4343
/* ============ Structs ============ */
4444

45-
struct PositionNotionalInfo {
46-
address baseToken; // Virtual token minted by the Perp protocol
47-
int256 baseBalance; // Base position notional quantity in 10**18 decimals. When negative, position is short
48-
int256 quoteBalance; // vUSDC "debt" notional quantity minted to open position. When positive, position is short
49-
}
50-
51-
struct PositionUnitInfo {
52-
address baseToken; // Virtual token minted by the Perp protocol
53-
int256 baseUnit; // Base position unit. When negative, position is short
54-
int256 quoteUnit; // vUSDC "debt" position unit. When positive, position is short
55-
}
56-
5745
// Note: when `pendingFundingPayments` is positive it will be credited to account on settlement,
5846
// when negative it's a debt owed that will be repaid on settlement. (PerpProtocol.Exchange returns the value
5947
// with the opposite meaning, e.g positively signed payments are owed by account to system).
@@ -252,7 +240,7 @@ interface IPerpV2LeverageModule {
252240
* + baseBalance: baseToken balance as notional quantity (10**18)
253241
* + quoteBalance: USDC quote asset balance as notional quantity (10**18)
254242
*/
255-
function getPositionNotionalInfo(ISetToken _setToken) external view returns (PositionNotionalInfo[] memory);
243+
function getPositionNotionalInfo(ISetToken _setToken) external view returns (PerpV2Positions.PositionNotionalInfo[] memory);
256244

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

270258
/**
271259
* @dev Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide

contracts/interfaces/external/perp-v2/IMarketRegistry.sol

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,15 @@ pragma solidity 0.6.10;
2020
pragma experimental ABIEncoderV2;
2121

2222
interface IMarketRegistry {
23-
//
24-
// STRUCT
25-
//
26-
struct MarketInfo {
27-
address pool;
28-
uint24 exchangeFeeRatio;
29-
uint24 uniswapFeeRatio;
30-
uint24 insuranceFundFeeRatio;
31-
}
32-
33-
//
34-
// EVENT
35-
//
36-
event PoolAdded(address indexed baseToken, uint24 indexed feeRatio, address indexed pool);
37-
event FeeRatioChanged(address baseToken, uint24 feeRatio);
38-
event InsuranceFundFeeRatioChanged(uint24 feeRatio);
39-
event MaxOrdersPerMarketChanged(uint8 maxOrdersPerMarket);
40-
41-
//
42-
// FUNCTION
43-
//
44-
45-
function addPool(address baseToken, uint24 feeRatio) external returns (address);
46-
47-
function setFeeRatio(address baseToken, uint24 feeRatio) external;
48-
49-
function setInsuranceFundFeeRatio(address baseToken, uint24 insuranceFundFeeRatioArg) external;
50-
51-
function setMaxOrdersPerMarket(uint8 maxOrdersPerMarketArg) external;
52-
5323
//
5424
// EXTERNAL VIEW
5525
//
5626

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

59-
function getFeeRatio(address baseToken) external view returns (uint24);
60-
61-
function getInsuranceFundFeeRatio(address baseToken) external view returns (uint24);
62-
63-
function getMarketInfo(address baseToken) external view returns (MarketInfo memory);
64-
6529
function getQuoteToken() external view returns (address);
6630

6731
function getUniswapV3Factory() external view returns (address);
6832

69-
function getMaxOrdersPerMarket() external view returns (uint8);
70-
7133
function hasPool(address baseToken) external view returns (bool);
7234
}

contracts/mocks/PositionV2Mock.sol

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Copyright 2020 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
pragma experimental "ABIEncoderV2";
21+
22+
import { ISetToken } from "../interfaces/ISetToken.sol";
23+
import { PositionV2 } from "../protocol/lib/PositionV2.sol";
24+
25+
26+
// Mock contract implementation of PositionV2 functions
27+
contract PositionV2Mock {
28+
constructor()
29+
public
30+
{}
31+
32+
function initialize(ISetToken _setToken) external {
33+
_setToken.initializeModule();
34+
}
35+
36+
function testHasDefaultPosition(ISetToken _setToken, address _component) external view returns(bool) {
37+
return PositionV2.hasDefaultPosition(_setToken, _component);
38+
}
39+
40+
function testHasExternalPosition(ISetToken _setToken, address _component) external view returns(bool) {
41+
return PositionV2.hasExternalPosition(_setToken, _component);
42+
}
43+
function testHasSufficientDefaultUnits(ISetToken _setToken, address _component, uint256 _unit) external view returns(bool) {
44+
return PositionV2.hasSufficientDefaultUnits(_setToken, _component, _unit);
45+
}
46+
function testHasSufficientExternalUnits(
47+
ISetToken _setToken,
48+
address _component,
49+
address _module,
50+
uint256 _unit
51+
)
52+
external
53+
view
54+
returns(bool)
55+
{
56+
return PositionV2.hasSufficientExternalUnits(_setToken, _component, _module, _unit);
57+
}
58+
59+
function testEditDefaultPosition(ISetToken _setToken, address _component, uint256 _newUnit) external {
60+
return PositionV2.editDefaultPosition(_setToken, _component, _newUnit);
61+
}
62+
63+
function testEditExternalPosition(
64+
ISetToken _setToken,
65+
address _component,
66+
address _module,
67+
int256 _newUnit,
68+
bytes memory _data
69+
)
70+
external
71+
{
72+
PositionV2.editExternalPosition(_setToken, _component, _module, _newUnit, _data);
73+
}
74+
75+
function testGetDefaultTotalNotional(
76+
uint256 _setTokenSupply,
77+
uint256 _positionUnit
78+
)
79+
external
80+
pure
81+
returns (uint256)
82+
{
83+
return PositionV2.getDefaultTotalNotional(_setTokenSupply, _positionUnit);
84+
}
85+
86+
function testGetDefaultPositionUnit(
87+
uint256 _setTokenSupply,
88+
uint256 _totalNotional
89+
)
90+
external
91+
pure
92+
returns (uint256)
93+
{
94+
return PositionV2.getDefaultPositionUnit(_setTokenSupply, _totalNotional);
95+
}
96+
97+
function testGetDefaultTrackedBalance(ISetToken _setToken, address _component)
98+
external
99+
view
100+
returns (uint256)
101+
{
102+
return PositionV2.getDefaultTrackedBalance(_setToken, _component);
103+
}
104+
105+
function testCalculateAndEditDefaultPosition(
106+
ISetToken _setToken,
107+
address _component,
108+
uint256 _setTotalSupply,
109+
uint256 _componentPreviousBalance
110+
)
111+
external
112+
returns (uint256, uint256, uint256)
113+
{
114+
return PositionV2.calculateAndEditDefaultPosition(
115+
_setToken,
116+
_component,
117+
_setTotalSupply,
118+
_componentPreviousBalance
119+
);
120+
}
121+
122+
function testCalculateDefaultEditPositionUnit(
123+
uint256 _setTokenSupply,
124+
uint256 _preTotalNotional,
125+
uint256 _postTotalNotional,
126+
uint256 _prePositionUnit
127+
)
128+
external
129+
pure
130+
returns (uint256)
131+
{
132+
return PositionV2.calculateDefaultEditPositionUnit(
133+
_setTokenSupply,
134+
_preTotalNotional,
135+
_postTotalNotional,
136+
_prePositionUnit
137+
);
138+
}
139+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
Copyright 2021 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
pragma experimental ABIEncoderV2;
21+
22+
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
23+
import { IClearingHouse } from "../../../../interfaces/external/perp-v2/IClearingHouse.sol";
24+
import { IVault } from "../../../../interfaces/external/perp-v2/IVault.sol";
25+
import { IQuoter } from "../../../../interfaces/external/perp-v2/IQuoter.sol";
26+
27+
import { PerpV2LibraryV2 } from "../../../../protocol/integration/lib/PerpV2LibraryV2.sol";
28+
import { ISetToken } from "../../../../interfaces/ISetToken.sol";
29+
30+
/**
31+
* @title PerpV2LibraryV2Mock
32+
* @author Set Protocol
33+
*
34+
* Mock for PerpV2LibraryV2 Library contract. Used for testing PerpV2LibraryV2 Library contract, as the library
35+
* contract can't be tested directly using ethers.js.
36+
*/
37+
contract PerpV2LibraryV2Mock {
38+
39+
/* ============ External ============ */
40+
41+
function testGetDepositCalldata(
42+
IVault _vault,
43+
IERC20 _asset,
44+
uint256 _amountNotional
45+
)
46+
public
47+
pure
48+
returns (address, uint256, bytes memory)
49+
{
50+
return PerpV2LibraryV2.getDepositCalldata(_vault, _asset, _amountNotional);
51+
}
52+
53+
function testInvokeDeposit(
54+
ISetToken _setToken,
55+
IVault _vault,
56+
IERC20 _asset,
57+
uint256 _amountNotional
58+
)
59+
external
60+
{
61+
return PerpV2LibraryV2.invokeDeposit(_setToken, _vault, _asset, _amountNotional);
62+
}
63+
64+
function testGetWithdrawCalldata(
65+
IVault _vault,
66+
IERC20 _asset,
67+
uint256 _amountNotional
68+
)
69+
public
70+
pure
71+
returns (address, uint256, bytes memory)
72+
{
73+
return PerpV2LibraryV2.getWithdrawCalldata(_vault, _asset, _amountNotional);
74+
}
75+
76+
function testInvokeWithdraw(
77+
ISetToken _setToken,
78+
IVault _vault,
79+
IERC20 _asset,
80+
uint256 _amountNotional
81+
)
82+
external
83+
{
84+
return PerpV2LibraryV2.invokeWithdraw(_setToken, _vault, _asset, _amountNotional);
85+
}
86+
87+
function testGetOpenPositionCalldata(
88+
IClearingHouse _clearingHouse,
89+
IClearingHouse.OpenPositionParams memory _params
90+
)
91+
public
92+
pure
93+
returns (address, uint256, bytes memory)
94+
{
95+
return PerpV2LibraryV2.getOpenPositionCalldata(_clearingHouse, _params);
96+
}
97+
98+
function testInvokeOpenPosition(
99+
ISetToken _setToken,
100+
IClearingHouse _clearingHouse,
101+
IClearingHouse.OpenPositionParams memory _params
102+
)
103+
external
104+
returns (uint256 deltaBase, uint256 deltaQuote)
105+
{
106+
return PerpV2LibraryV2.invokeOpenPosition(_setToken, _clearingHouse, _params);
107+
}
108+
109+
function testGetSwapCalldata(
110+
IQuoter _quoter,
111+
IQuoter.SwapParams memory _params
112+
)
113+
public
114+
pure
115+
returns (address, uint256, bytes memory)
116+
{
117+
return PerpV2LibraryV2.getSwapCalldata(_quoter, _params);
118+
}
119+
120+
function testInvokeSwap(
121+
ISetToken _setToken,
122+
IQuoter _quoter,
123+
IQuoter.SwapParams memory _params
124+
)
125+
external
126+
returns (IQuoter.SwapResponse memory)
127+
{
128+
return PerpV2LibraryV2.invokeSwap(_setToken, _quoter, _params);
129+
}
130+
131+
function testSimulateTrade(
132+
PerpV2LibraryV2.ActionInfo memory _actionInfo,
133+
IQuoter _perpQuoter
134+
)
135+
external
136+
returns (uint256, uint256)
137+
{
138+
return PerpV2LibraryV2.simulateTrade(_actionInfo, _perpQuoter);
139+
}
140+
141+
function testExecuteTrade(
142+
PerpV2LibraryV2.ActionInfo memory _actionInfo,
143+
IClearingHouse _perpClearingHouse
144+
)
145+
external
146+
returns (uint256, uint256)
147+
{
148+
return PerpV2LibraryV2.executeTrade(_actionInfo, _perpClearingHouse);
149+
}
150+
151+
/* ============ Helper Functions ============ */
152+
153+
function initializeModuleOnSet(ISetToken _setToken) external {
154+
_setToken.initializeModule();
155+
}
156+
}

0 commit comments

Comments
 (0)