17
17
'use strict' ;
18
18
19
19
import * as _ from 'lodash' ;
20
+ import * as setProtocolUtils from 'set-protocol-utils' ;
20
21
import Web3 from 'web3' ;
21
22
22
23
import { BigNumber } from '../util' ;
23
24
import {
24
25
AssetPairManagerWrapper ,
26
+ AssetPairManagerV2Wrapper ,
25
27
BTCDAIRebalancingManagerWrapper ,
26
28
BTCETHRebalancingManagerWrapper ,
27
29
ETHDAIRebalancingManagerWrapper ,
28
30
MACOStrategyManagerWrapper ,
29
31
MACOStrategyManagerV2Wrapper ,
30
32
MovingAverageOracleWrapper ,
31
33
MedianizerWrapper ,
34
+ PerformanceFeeCalculatorWrapper ,
32
35
ProtocolViewerWrapper ,
36
+ RebalancingSetTokenV3Wrapper ,
33
37
SetTokenWrapper ,
34
38
RebalancingSetTokenWrapper ,
35
39
} from '../wrappers' ;
36
40
import { Assertions } from '../assertions' ;
37
- import { Address , ManagerType , SetProtocolConfig , Tx } from '../types/common' ;
41
+ import {
42
+ Address ,
43
+ Bytes ,
44
+ FeeType ,
45
+ ManagerType ,
46
+ SetProtocolConfig ,
47
+ Tx
48
+ } from '../types/common' ;
38
49
import {
39
50
DAI_FULL_TOKEN_UNITS ,
40
51
DAI_PRICE ,
@@ -45,12 +56,15 @@ import {
45
56
} from '../constants' ;
46
57
import {
47
58
AssetPairManagerDetails ,
59
+ AssetPairManagerV2Details ,
48
60
BTCDAIRebalancingManagerDetails ,
49
61
BTCETHRebalancingManagerDetails ,
50
62
ETHDAIRebalancingManagerDetails ,
51
63
MovingAverageManagerDetails ,
52
64
} from '../types/strategies' ;
53
65
66
+ const { SetProtocolUtils : SetUtils } = setProtocolUtils ;
67
+
54
68
/**
55
69
* @title RebalancingManagerAPI
56
70
* @author Set Protocol
@@ -71,6 +85,9 @@ export class RebalancingManagerAPI {
71
85
private macoStrategyManager : MACOStrategyManagerWrapper ;
72
86
private macoStrategyManagerV2 : MACOStrategyManagerV2Wrapper ;
73
87
private assetPairManager : AssetPairManagerWrapper ;
88
+ private assetPairManagerV2 : AssetPairManagerV2Wrapper ;
89
+ private performanceFeeCalculator : PerformanceFeeCalculatorWrapper ;
90
+ private rebalancingSetV3 : RebalancingSetTokenV3Wrapper ;
74
91
75
92
/**
76
93
* Instantiates a new RebalancingManagerAPI instance that contains methods for issuing and redeeming Sets
@@ -86,6 +103,9 @@ export class RebalancingManagerAPI {
86
103
this . macoStrategyManager = new MACOStrategyManagerWrapper ( web3 ) ;
87
104
this . macoStrategyManagerV2 = new MACOStrategyManagerV2Wrapper ( web3 ) ;
88
105
this . assetPairManager = new AssetPairManagerWrapper ( web3 ) ;
106
+ this . assetPairManagerV2 = new AssetPairManagerV2Wrapper ( web3 ) ;
107
+ this . performanceFeeCalculator = new PerformanceFeeCalculatorWrapper ( web3 ) ;
108
+ this . rebalancingSetV3 = new RebalancingSetTokenV3Wrapper ( web3 ) ;
89
109
90
110
this . assert = assertions ;
91
111
this . setToken = new SetTokenWrapper ( web3 ) ;
@@ -172,6 +192,121 @@ export class RebalancingManagerAPI {
172
192
return await this . macoStrategyManager . confirmPropose ( macoManager , txOpts ) ;
173
193
}
174
194
195
+ /**
196
+ * Calls manager to adjustFees. Only for asset pair manager v2
197
+ *
198
+ * @param manager Address of manager
199
+ * @param newFeeType Type of fee being changed
200
+ * @param newFeePercentage New fee percentage
201
+ * @return The hash of the resulting transaction.
202
+ */
203
+ public async adjustPerformanceFeesAsync (
204
+ manager : Address ,
205
+ newFeeType : FeeType ,
206
+ newFeePercentage : BigNumber ,
207
+ txOpts : Tx ,
208
+ ) : Promise < string > {
209
+ await this . assertAdjustFees (
210
+ manager ,
211
+ newFeeType ,
212
+ newFeePercentage ,
213
+ txOpts
214
+ ) ;
215
+
216
+ const newFeeCallData = SetUtils . generateAdjustFeeCallData (
217
+ new BigNumber ( newFeeType ) ,
218
+ newFeePercentage
219
+ ) ;
220
+
221
+ return this . assetPairManagerV2 . adjustFee (
222
+ manager ,
223
+ newFeeCallData ,
224
+ txOpts
225
+ ) ;
226
+ }
227
+
228
+ /**
229
+ * Cancels previous fee adjustment (before enacted)
230
+ *
231
+ * @param manager Address of manager
232
+ * @param upgradeHash Hash of the inital fee adjustment call data
233
+ * @return The hash of the resulting transaction.
234
+ */
235
+ public async removeFeeUpdateAsync (
236
+ manager : Address ,
237
+ upgradeHash : string ,
238
+ txOpts : Tx ,
239
+ ) : Promise < string > {
240
+ return this . assetPairManagerV2 . removeRegisteredUpgrade ( manager , upgradeHash ) ;
241
+ }
242
+
243
+ /**
244
+ * Calls AssetPairManagerV2's setLiquidator function. Changes liquidator used in rebalances.
245
+ *
246
+ * @param manager Address of the asset pair manager contract
247
+ * @param newLiquidator New liquidator address
248
+ * @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
249
+ * gasPrice data
250
+ * @return The hash of the resulting transaction.
251
+ */
252
+ public async setLiquidatorAsync (
253
+ manager : Address ,
254
+ newLiquidator : Address ,
255
+ txOpts : Tx ,
256
+ ) : Promise < string > {
257
+ await this . assertAddressSetters ( manager , newLiquidator ) ;
258
+
259
+ return this . assetPairManagerV2 . setLiquidator (
260
+ manager ,
261
+ newLiquidator ,
262
+ txOpts
263
+ ) ;
264
+ }
265
+
266
+ /**
267
+ * Calls AssetPairManagerV2's setLiquidatorData function. Changes liquidatorData used in rebalances.
268
+ *
269
+ * @param manager Address of the asset pair manager contract
270
+ * @param newLiquidatorData New liquidator data
271
+ * @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
272
+ * gasPrice data
273
+ * @return The hash of the resulting transaction.
274
+ */
275
+ public async setLiquidatorDataAsync (
276
+ manager : Address ,
277
+ newLiquidatorData : Bytes ,
278
+ txOpts : Tx ,
279
+ ) : Promise < string > {
280
+ return this . assetPairManagerV2 . setLiquidatorData (
281
+ manager ,
282
+ newLiquidatorData ,
283
+ txOpts
284
+ ) ;
285
+ }
286
+
287
+ /**
288
+ * Calls AssetPairManagerV2's setFeeRecipient function. Changes feeRecipient address.
289
+ *
290
+ * @param manager Address of the asset pair manager contract
291
+ * @param newFeeRecipient New feeRecipient address
292
+ * @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
293
+ * gasPrice data
294
+ * @return The hash of the resulting transaction.
295
+ */
296
+ public async setFeeRecipientAsync (
297
+ manager : Address ,
298
+ newFeeRecipient : Address ,
299
+ txOpts : Tx ,
300
+ ) : Promise < string > {
301
+ await this . assertAddressSetters ( manager , newFeeRecipient ) ;
302
+
303
+ return this . assetPairManagerV2 . setFeeRecipient (
304
+ manager ,
305
+ newFeeRecipient ,
306
+ txOpts
307
+ ) ;
308
+ }
309
+
175
310
/**
176
311
* Fetches if initialPropose can be called without revert on AssetPairManager
177
312
*
@@ -520,6 +655,66 @@ export class RebalancingManagerAPI {
520
655
} as AssetPairManagerDetails ;
521
656
}
522
657
658
+ /**
659
+ * Fetches the state variables of the Asset Pair Manager V2 contract.
660
+ *
661
+ * @param manager Address of the AssetPairManagerV2 contract
662
+ * @return Object containing the state information related to the manager
663
+ */
664
+ public async getAssetPairManagerV2DetailsAsync (
665
+ manager : Address
666
+ ) : Promise < AssetPairManagerV2Details > {
667
+ const [
668
+ allocationDenominator ,
669
+ allocator ,
670
+ baseAssetAllocation ,
671
+ bullishBaseAssetAllocation ,
672
+ bearishBaseAssetAllocation ,
673
+ core ,
674
+ recentInitialProposeTimestamp ,
675
+ rebalancingSetToken ,
676
+ ] = await Promise . all ( [
677
+ this . assetPairManagerV2 . allocationDenominator ( manager ) ,
678
+ this . assetPairManagerV2 . allocator ( manager ) ,
679
+ this . assetPairManagerV2 . baseAssetAllocation ( manager ) ,
680
+ this . assetPairManagerV2 . bullishBaseAssetAllocation ( manager ) ,
681
+ this . assetPairManagerV2 . bearishBaseAssetAllocation ( manager ) ,
682
+ this . assetPairManagerV2 . core ( manager ) ,
683
+ this . assetPairManagerV2 . recentInitialProposeTimestamp ( manager ) ,
684
+ this . assetPairManagerV2 . rebalancingSetToken ( manager ) ,
685
+ ] ) ;
686
+
687
+ const [
688
+ signalConfirmationMinTime ,
689
+ signalConfirmationMaxTime ,
690
+ trigger ,
691
+ liquidatorData ,
692
+ rebalanceFeeCalculator ,
693
+ ] = await Promise . all ( [
694
+ this . assetPairManagerV2 . signalConfirmationMinTime ( manager ) ,
695
+ this . assetPairManagerV2 . signalConfirmationMaxTime ( manager ) ,
696
+ this . assetPairManagerV2 . trigger ( manager ) ,
697
+ this . assetPairManagerV2 . liquidatorData ( manager ) ,
698
+ this . rebalancingSetV3 . rebalanceFeeCalculator ( rebalancingSetToken ) ,
699
+ ] ) ;
700
+
701
+ return {
702
+ allocationDenominator,
703
+ allocator,
704
+ baseAssetAllocation,
705
+ bullishBaseAssetAllocation,
706
+ bearishBaseAssetAllocation,
707
+ core,
708
+ recentInitialProposeTimestamp,
709
+ rebalancingSetToken,
710
+ signalConfirmationMinTime,
711
+ signalConfirmationMaxTime,
712
+ trigger,
713
+ liquidatorData,
714
+ rebalanceFeeCalculator,
715
+ } as AssetPairManagerV2Details ;
716
+ }
717
+
523
718
/**
524
719
* Fetches the crossover confirmation time of AssetPairManager contracts.
525
720
*
@@ -789,6 +984,41 @@ export class RebalancingManagerAPI {
789
984
) ;
790
985
}
791
986
987
+ private async assertAdjustFees (
988
+ manager : Address ,
989
+ newFeeType : FeeType ,
990
+ newFeePercentage : BigNumber ,
991
+ txOpts : Tx
992
+ ) : Promise < void > {
993
+ const managerDetails = await this . getAssetPairManagerV2DetailsAsync ( manager ) ;
994
+ const feeCalculatorAddress = managerDetails . rebalanceFeeCalculator ;
995
+
996
+ let maxFee : BigNumber ;
997
+ if ( newFeeType == FeeType . StreamingFee ) {
998
+ maxFee = await this . performanceFeeCalculator . maximumStreamingFeePercentage (
999
+ feeCalculatorAddress
1000
+ ) ;
1001
+ } else {
1002
+ maxFee = await this . performanceFeeCalculator . maximumProfitFeePercentage (
1003
+ feeCalculatorAddress
1004
+ ) ;
1005
+ }
1006
+
1007
+ this . assert . common . isGreaterOrEqualThan (
1008
+ maxFee ,
1009
+ newFeePercentage ,
1010
+ 'Passed fee exceeds allowed maximum.'
1011
+ ) ;
1012
+ }
1013
+
1014
+ private async assertAddressSetters (
1015
+ manager : Address ,
1016
+ newAddress : Address ,
1017
+ ) : Promise < void > {
1018
+ this . assert . schema . isValidAddress ( 'manager' , manager ) ;
1019
+ this . assert . schema . isValidAddress ( 'newAddress' , newAddress ) ;
1020
+ }
1021
+
792
1022
// Helper functions
793
1023
794
1024
private assertCrossoverTriggerMet (
0 commit comments