Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit d54bc2e

Browse files
authored
Merge pull request #373 from SetProtocol/richard/asset-pair-manager-v2
Functions for Asset Pair Manager V2
2 parents 2936c00 + b7e168b commit d54bc2e

File tree

13 files changed

+1826
-7
lines changed

13 files changed

+1826
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"moment": "^2.22.2",
7070
"set-protocol-contracts": "1.4.9-beta",
7171
"set-protocol-oracles": "^1.0.16",
72-
"set-protocol-strategies": "^1.1.37",
72+
"set-protocol-strategies": "^1.1.39",
7373
"set-protocol-viewers": "^1.0.12",
7474
"set-protocol-utils": "^1.1.2",
7575
"timekeeper": "^2.1.2",

src/api/RebalancingManagerAPI.ts

Lines changed: 231 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,35 @@
1717
'use strict';
1818

1919
import * as _ from 'lodash';
20+
import * as setProtocolUtils from 'set-protocol-utils';
2021
import Web3 from 'web3';
2122

2223
import { BigNumber } from '../util';
2324
import {
2425
AssetPairManagerWrapper,
26+
AssetPairManagerV2Wrapper,
2527
BTCDAIRebalancingManagerWrapper,
2628
BTCETHRebalancingManagerWrapper,
2729
ETHDAIRebalancingManagerWrapper,
2830
MACOStrategyManagerWrapper,
2931
MACOStrategyManagerV2Wrapper,
3032
MovingAverageOracleWrapper,
3133
MedianizerWrapper,
34+
PerformanceFeeCalculatorWrapper,
3235
ProtocolViewerWrapper,
36+
RebalancingSetTokenV3Wrapper,
3337
SetTokenWrapper,
3438
RebalancingSetTokenWrapper,
3539
} from '../wrappers';
3640
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';
3849
import {
3950
DAI_FULL_TOKEN_UNITS,
4051
DAI_PRICE,
@@ -45,12 +56,15 @@ import {
4556
} from '../constants';
4657
import {
4758
AssetPairManagerDetails,
59+
AssetPairManagerV2Details,
4860
BTCDAIRebalancingManagerDetails,
4961
BTCETHRebalancingManagerDetails,
5062
ETHDAIRebalancingManagerDetails,
5163
MovingAverageManagerDetails,
5264
} from '../types/strategies';
5365

66+
const { SetProtocolUtils: SetUtils } = setProtocolUtils;
67+
5468
/**
5569
* @title RebalancingManagerAPI
5670
* @author Set Protocol
@@ -71,6 +85,9 @@ export class RebalancingManagerAPI {
7185
private macoStrategyManager: MACOStrategyManagerWrapper;
7286
private macoStrategyManagerV2: MACOStrategyManagerV2Wrapper;
7387
private assetPairManager: AssetPairManagerWrapper;
88+
private assetPairManagerV2: AssetPairManagerV2Wrapper;
89+
private performanceFeeCalculator: PerformanceFeeCalculatorWrapper;
90+
private rebalancingSetV3: RebalancingSetTokenV3Wrapper;
7491

7592
/**
7693
* Instantiates a new RebalancingManagerAPI instance that contains methods for issuing and redeeming Sets
@@ -86,6 +103,9 @@ export class RebalancingManagerAPI {
86103
this.macoStrategyManager = new MACOStrategyManagerWrapper(web3);
87104
this.macoStrategyManagerV2 = new MACOStrategyManagerV2Wrapper(web3);
88105
this.assetPairManager = new AssetPairManagerWrapper(web3);
106+
this.assetPairManagerV2 = new AssetPairManagerV2Wrapper(web3);
107+
this.performanceFeeCalculator = new PerformanceFeeCalculatorWrapper(web3);
108+
this.rebalancingSetV3 = new RebalancingSetTokenV3Wrapper(web3);
89109

90110
this.assert = assertions;
91111
this.setToken = new SetTokenWrapper(web3);
@@ -172,6 +192,121 @@ export class RebalancingManagerAPI {
172192
return await this.macoStrategyManager.confirmPropose(macoManager, txOpts);
173193
}
174194

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+
175310
/**
176311
* Fetches if initialPropose can be called without revert on AssetPairManager
177312
*
@@ -520,6 +655,66 @@ export class RebalancingManagerAPI {
520655
} as AssetPairManagerDetails;
521656
}
522657

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+
523718
/**
524719
* Fetches the crossover confirmation time of AssetPairManager contracts.
525720
*
@@ -789,6 +984,41 @@ export class RebalancingManagerAPI {
789984
);
790985
}
791986

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+
7921022
// Helper functions
7931023

7941024
private assertCrossoverTriggerMet(

src/types/strategies.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ export interface AssetPairManagerDetails {
5151
trigger: Address;
5252
}
5353

54+
export interface AssetPairManagerV2Details {
55+
allocationDenominator: BigNumber;
56+
allocator: Address;
57+
baseAssetAllocation: BigNumber;
58+
bullishBaseAssetAllocation: BigNumber;
59+
bearishBaseAssetAllocation: BigNumber;
60+
core: Address;
61+
recentInitialProposeTimestamp: BigNumber;
62+
rebalancingSetToken: Address;
63+
signalConfirmationMinTime: BigNumber;
64+
signalConfirmationMaxTime: BigNumber;
65+
trigger: Address;
66+
liquidatorData: string;
67+
rebalanceFeeCalculator: Address;
68+
}
69+
5470
export interface BTCETHRebalancingManagerDetails {
5571
core: Address;
5672
btcPriceFeed: Address;

src/wrappers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export { WhitelistWrapper } from './set_protocol/WhitelistWrapper';
4343
export { VaultWrapper } from './set_protocol/VaultWrapper';
4444

4545
export { AssetPairManagerWrapper } from './strategies/AssetPairManagerWrapper';
46+
export { AssetPairManagerV2Wrapper } from './strategies/AssetPairManagerV2Wrapper';
4647
export { BTCDAIRebalancingManagerWrapper } from './strategies/BTCDAIRebalancingManagerWrapper';
4748
export { BTCETHRebalancingManagerWrapper } from './strategies/BTCETHRebalancingManagerWrapper';
4849
export { ETHDAIRebalancingManagerWrapper } from './strategies/ETHDAIRebalancingManagerWrapper';

src/wrappers/set_protocol/RebalancingSetTokenV3Wrapper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,18 @@ export class RebalancingSetTokenV3Wrapper {
5353
txOpts
5454
);
5555
}
56+
57+
/**
58+
* Get rebalanceFeeCalculator
59+
*
60+
* @param rebalancingSetAddress Address of the Set
61+
* @return Transaction hash
62+
*/
63+
public async rebalanceFeeCalculator(
64+
rebalancingSetAddress: Address,
65+
): Promise<string> {
66+
const rebalancingSetTokenInstance = await this.contracts.loadRebalancingSetTokenV3Async(rebalancingSetAddress);
67+
68+
return await rebalancingSetTokenInstance.rebalanceFeeCalculator.callAsync();
69+
}
5670
}

0 commit comments

Comments
 (0)