1
+ /*
2
+ Copyright 2022 Set Labs Inc.
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ */
13
+
14
+ 'use strict' ;
15
+
16
+ import { Address } from '@setprotocol/set-protocol-v2/utils/types' ;
17
+ import { BigNumber , ContractTransaction } from 'ethers' ;
18
+ import { TransactionOverrides } from '@setprotocol/set-protocol-v2/dist/typechain' ;
19
+ import { Provider } from '@ethersproject/providers' ;
20
+ import { generateTxOpts } from '../../utils/transactions' ;
21
+
22
+ import ContractWrapper from './ContractWrapper' ;
23
+
24
+ /**
25
+ * @title PerpV2LeverageModuleWrapper
26
+ * @author Set Protocol
27
+ *
28
+ * The PerpV2LeverageModuleWrapper forwards functionality from the PerpV2LeverageModule contract.
29
+ *
30
+ */
31
+ export default class PerpV2LeverageModuleWrapper {
32
+ private provider : Provider ;
33
+ private contracts : ContractWrapper ;
34
+
35
+ private perpV2LeverageModuleAddress : Address ;
36
+
37
+ public constructor ( provider : Provider , perpV2LeverageModuleAddress : Address ) {
38
+ this . provider = provider ;
39
+ this . contracts = new ContractWrapper ( this . provider ) ;
40
+ this . perpV2LeverageModuleAddress = perpV2LeverageModuleAddress ;
41
+ }
42
+
43
+ /**
44
+ * Initializes this module to the SetToken. Only callable by the SetToken's manager.
45
+ *
46
+ * @param setTokenAddress Address of the SetToken to initialize
47
+ * @param callerAddress Address of caller (optional)
48
+ * @param txOpts Overrides for transaction (optional)
49
+ */
50
+ public async initialize (
51
+ setTokenAddress : Address ,
52
+ callerAddress : Address = undefined ,
53
+ txOpts : TransactionOverrides = { }
54
+ ) : Promise < ContractTransaction > {
55
+ const txOptions = await generateTxOpts ( txOpts ) ;
56
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
57
+ this . perpV2LeverageModuleAddress ,
58
+ callerAddress
59
+ ) ;
60
+
61
+ return await perpV2LeverageModuleInstance . initialize (
62
+ setTokenAddress ,
63
+ txOptions ,
64
+ ) ;
65
+ }
66
+
67
+ /**
68
+ * Gets the address of the collateral token
69
+ *
70
+ * @param callerAddress Address of the method caller
71
+ * @return The address of the ERC20 collateral token
72
+ */
73
+ public async collateralToken (
74
+ callerAddress : Address = undefined ,
75
+ ) : Promise < Address > {
76
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
77
+ this . perpV2LeverageModuleAddress ,
78
+ callerAddress
79
+ ) ;
80
+
81
+ return await perpV2LeverageModuleInstance . collateralToken ( ) ;
82
+ }
83
+
84
+ /**
85
+ * Gets decimals of the collateral token
86
+ *
87
+ * @param callerAddress Address of the method caller
88
+ * @return The decimals of the ERC20 collateral token
89
+ */
90
+ public async collateralDecimals (
91
+ callerAddress : Address = undefined ,
92
+ ) : Promise < Number > {
93
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
94
+ this . perpV2LeverageModuleAddress ,
95
+ callerAddress
96
+ ) ;
97
+
98
+ return await perpV2LeverageModuleInstance . collateralDecimals ( ) ;
99
+ }
100
+
101
+ /**
102
+ * Returns a PositionUnitNotionalInfo array representing all positions open for the SetToken.
103
+ *
104
+ * @param _setToken Instance of SetToken
105
+ *
106
+ * @return address[] baseToken: addresses
107
+ * @return BigNumber[] baseBalance: baseToken balances as notional quantity (10**18)
108
+ * @return BigNumber[] quoteBalance: USDC quote asset balances as notional quantity (10**18)
109
+ */
110
+ public async getPositionNotionalInfo (
111
+ setTokenAddress : Address ,
112
+ callerAddress : Address = undefined ,
113
+ ) : Promise < ( Address | BigNumber ) [ ] [ ] > {
114
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
115
+ this . perpV2LeverageModuleAddress ,
116
+ callerAddress
117
+ ) ;
118
+
119
+ return await perpV2LeverageModuleInstance . getPositionNotionalInfo (
120
+ setTokenAddress ,
121
+ ) ;
122
+ }
123
+
124
+ /**
125
+ * Returns a PositionUnitInfo array representing all positions open for the SetToken.
126
+ *
127
+ * @param _setToken Instance of SetToken
128
+ *
129
+ * @return address[] baseToken: addresses
130
+ * @return BigNumber[] baseUnit: baseToken balances as position unit (10**18)
131
+ * @return BigNumber[] quoteUnit: USDC quote asset balances as position unit (10**18)
132
+ */
133
+ public async getPositionUnitInfo (
134
+ setTokenAddress : Address ,
135
+ callerAddress : Address = undefined ,
136
+ ) : Promise < ( Address | BigNumber ) [ ] [ ] > {
137
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
138
+ this . perpV2LeverageModuleAddress ,
139
+ callerAddress
140
+ ) ;
141
+
142
+ return await perpV2LeverageModuleInstance . getPositionUnitInfo (
143
+ setTokenAddress ,
144
+ ) ;
145
+ }
146
+
147
+ /**
148
+ * Gets Perp account info for SetToken. Returns an AccountInfo struct containing account wide
149
+ * (rather than position specific) balance info
150
+ *
151
+ * @param _setToken Instance of the SetToken
152
+ *
153
+ * @return BigNumber collateral balance (10**18, regardless of underlying collateral decimals)
154
+ * @return BigNumber owed realized Pnl` (10**18)
155
+ * @return BigNumber pending funding payments (10**18)
156
+ * @return BigNumber net quote balance (10**18)
157
+ */
158
+ public async getAccountInfo (
159
+ setTokenAddress : Address ,
160
+ callerAddress : Address = undefined ,
161
+ ) : Promise < BigNumber [ ] > {
162
+ const perpV2LeverageModuleInstance = await this . contracts . loadPerpV2LeverageModuleAsync (
163
+ this . perpV2LeverageModuleAddress ,
164
+ callerAddress
165
+ ) ;
166
+
167
+ return await perpV2LeverageModuleInstance . getAccountInfo (
168
+ setTokenAddress ,
169
+ ) ;
170
+ }
171
+ }
0 commit comments