Skip to content

Commit 3b23713

Browse files
cgeweckerootulpbweick0xSachinK
authored
PerpLeverageModule (#158)
+ Add PerpV2LeverageModule + Add PerpV2Lib + Add PerpV2 fixtures and tests Co-authored-by: Rootul Patel <[email protected]> Co-authored-by: bweick <[email protected]> Co-authored-by: alpha-guy <[email protected]>
1 parent f7fe16b commit 3b23713

38 files changed

+10183
-706
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
interface IAccountBalance {
23+
function getBaseTokens(address trader) external view returns (address[] memory);
24+
function hasOrder(address trader) external view returns (bool);
25+
function getMarginRequirementForLiquidation(address trader) external view returns (int256);
26+
function getTotalDebtValue(address trader) external view returns (uint256);
27+
function getPnlAndPendingFee(address trader) external view returns (int256,int256,uint256);
28+
function getBase(address trader, address baseToken) external view returns (int256);
29+
function getQuote(address trader, address baseToken) external view returns (int256);
30+
function getNetQuoteBalanceAndPendingFee(address trader) external view returns (int256, uint256);
31+
function getPositionSize(address trader, address baseToken) external view returns (int256);
32+
function getPositionValue(address trader, address baseToken) external view returns (int256);
33+
function getTotalAbsPositionValue(address trader) external view returns (uint256);
34+
function getClearingHouseConfig() external view returns (address);
35+
function getExchange() external view returns (address);
36+
function getOrderBook() external view returns (address);
37+
function getVault() external view returns (address);
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
interface IClearingHouse {
23+
struct OpenPositionParams {
24+
address baseToken;
25+
bool isBaseToQuote;
26+
bool isExactInput;
27+
uint256 amount;
28+
// B2Q + exact input, want more output quote as possible, so we set a lower bound of output quote
29+
// B2Q + exact output, want less input base as possible, so we set a upper bound of input base
30+
// Q2B + exact input, want more output base as possible, so we set a lower bound of output base
31+
// Q2B + exact output, want less input quote as possible, so we set a upper bound of input quote
32+
// when it's 0 in exactInput, means ignore slippage protection
33+
// when it's maxUint in exactOutput = ignore
34+
// when it's over or under the bound, it will be reverted
35+
uint256 oppositeAmountBound;
36+
uint256 deadline;
37+
// B2Q: the price cannot be less than this value after the swap
38+
// Q2B: The price cannot be greater than this value after the swap
39+
// it will fill the trade until it reach the price limit instead of reverted
40+
uint160 sqrtPriceLimitX96;
41+
bytes32 referralCode;
42+
}
43+
44+
struct ClosePositionParams {
45+
address baseToken;
46+
uint160 sqrtPriceLimitX96;
47+
uint256 oppositeAmountBound;
48+
uint256 deadline;
49+
bytes32 referralCode;
50+
}
51+
52+
function openPosition(OpenPositionParams memory params)
53+
external
54+
returns (uint256 deltaBase, uint256 deltaQuote);
55+
56+
function closePosition(ClosePositionParams calldata params)
57+
external
58+
returns (uint256 deltaBase, uint256 deltaQuote);
59+
60+
function getAccountValue(address trader) external view returns (int256);
61+
function getPositionSize(address trader, address baseToken) external view returns (int256);
62+
function getPositionValue(address trader, address baseToken) external view returns (int256);
63+
function getOpenNotional(address trader, address baseToken) external view returns (int256);
64+
function getOwedRealizedPnl(address trader) external view returns (int256);
65+
function getTotalInitialMarginRequirement(address trader) external view returns (uint256);
66+
function getNetQuoteBalance(address trader) external view returns (int256);
67+
function getTotalUnrealizedPnl(address trader) external view returns (int256);
68+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
interface IExchange {
23+
struct FundingGrowth {
24+
int256 twPremiumX96;
25+
int256 twPremiumDivBySqrtPriceX96;
26+
}
27+
28+
struct SwapParams {
29+
address trader;
30+
address baseToken;
31+
bool isBaseToQuote;
32+
bool isExactInput;
33+
uint256 amount;
34+
uint160 sqrtPriceLimitX96;
35+
FundingGrowth fundingGrowthGlobal;
36+
}
37+
38+
struct SwapResponse {
39+
uint256 deltaAvailableBase;
40+
uint256 deltaAvailableQuote;
41+
int256 exchangedPositionSize;
42+
int256 exchangedPositionNotional;
43+
uint256 fee;
44+
uint256 insuranceFundFee;
45+
int24 tick;
46+
int256 realizedPnl;
47+
int256 openNotional;
48+
}
49+
50+
function getPool(address baseToken) external view returns (address);
51+
function getTick(address baseToken) external view returns (int24);
52+
function getSqrtMarkTwapX96(address baseToken, uint32 twapInterval) external view returns (uint160);
53+
function getMaxTickCrossedWithinBlock(address baseToken) external view returns (uint24);
54+
function getAllPendingFundingPayment(address trader) external view returns (int256);
55+
function getPendingFundingPayment(address trader, address baseToken) external view returns (int256);
56+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
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+
53+
//
54+
// EXTERNAL VIEW
55+
//
56+
57+
function getPool(address baseToken) external view returns (address);
58+
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+
65+
function getQuoteToken() external view returns (address);
66+
67+
function getUniswapV3Factory() external view returns (address);
68+
69+
function getMaxOrdersPerMarket() external view returns (uint8);
70+
71+
function hasPool(address baseToken) external view returns (bool);
72+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
interface IQuoter {
23+
struct SwapParams {
24+
address baseToken;
25+
bool isBaseToQuote;
26+
bool isExactInput;
27+
uint256 amount;
28+
uint160 sqrtPriceLimitX96; // price slippage protection
29+
}
30+
31+
struct SwapResponse {
32+
uint256 deltaAvailableBase;
33+
uint256 deltaAvailableQuote;
34+
int256 exchangedPositionSize;
35+
int256 exchangedPositionNotional;
36+
uint160 sqrtPriceX96;
37+
}
38+
39+
function swap(SwapParams memory params) external returns (SwapResponse memory response);
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
21+
interface IVault {
22+
function getBalance(address account) external view returns (int256);
23+
function decimals() external view returns (uint8);
24+
function getFreeCollateral(address trader) external view returns (uint256);
25+
function getFreeCollateralByRatio(address trader, uint24 ratio) external view returns (int256);
26+
function getLiquidateMarginRequirement(address trader) external view returns (int256);
27+
function getSettlementToken() external view returns (address);
28+
function getAccountBalance() external view returns (address);
29+
function getClearingHouse() external view returns (address);
30+
function getExchange() external view returns (address);
31+
}

0 commit comments

Comments
 (0)