Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"moment": "^2.22.2",
"set-protocol-contracts": "1.4.9-beta",
"set-protocol-oracles": "^1.0.16",
"set-protocol-strategies": "^1.1.37",
"set-protocol-strategies": "^1.1.39",
"set-protocol-viewers": "^1.0.12",
"set-protocol-utils": "^1.1.2",
"timekeeper": "^2.1.2",
Expand Down
232 changes: 231 additions & 1 deletion src/api/RebalancingManagerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@
'use strict';

import * as _ from 'lodash';
import * as setProtocolUtils from 'set-protocol-utils';
import Web3 from 'web3';

import { BigNumber } from '../util';
import {
AssetPairManagerWrapper,
AssetPairManagerV2Wrapper,
BTCDAIRebalancingManagerWrapper,
BTCETHRebalancingManagerWrapper,
ETHDAIRebalancingManagerWrapper,
MACOStrategyManagerWrapper,
MACOStrategyManagerV2Wrapper,
MovingAverageOracleWrapper,
MedianizerWrapper,
PerformanceFeeCalculatorWrapper,
ProtocolViewerWrapper,
RebalancingSetTokenV3Wrapper,
SetTokenWrapper,
RebalancingSetTokenWrapper,
} from '../wrappers';
import { Assertions } from '../assertions';
import { Address, ManagerType, SetProtocolConfig, Tx } from '../types/common';
import {
Address,
Bytes,
FeeType,
ManagerType,
SetProtocolConfig,
Tx
} from '../types/common';
import {
DAI_FULL_TOKEN_UNITS,
DAI_PRICE,
Expand All @@ -45,12 +56,15 @@ import {
} from '../constants';
import {
AssetPairManagerDetails,
AssetPairManagerV2Details,
BTCDAIRebalancingManagerDetails,
BTCETHRebalancingManagerDetails,
ETHDAIRebalancingManagerDetails,
MovingAverageManagerDetails,
} from '../types/strategies';

const { SetProtocolUtils: SetUtils } = setProtocolUtils;

/**
* @title RebalancingManagerAPI
* @author Set Protocol
Expand All @@ -71,6 +85,9 @@ export class RebalancingManagerAPI {
private macoStrategyManager: MACOStrategyManagerWrapper;
private macoStrategyManagerV2: MACOStrategyManagerV2Wrapper;
private assetPairManager: AssetPairManagerWrapper;
private assetPairManagerV2: AssetPairManagerV2Wrapper;
private performanceFeeCalculator: PerformanceFeeCalculatorWrapper;
private rebalancingSetV3: RebalancingSetTokenV3Wrapper;

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

this.assert = assertions;
this.setToken = new SetTokenWrapper(web3);
Expand Down Expand Up @@ -172,6 +192,121 @@ export class RebalancingManagerAPI {
return await this.macoStrategyManager.confirmPropose(macoManager, txOpts);
}

/**
* Calls manager to adjustFees. Only for asset pair manager v2
*
* @param manager Address of manager
* @param newFeeType Type of fee being changed
* @param newFeePercentage New fee percentage
* @return The hash of the resulting transaction.
*/
public async adjustPerformanceFeesAsync(
manager: Address,
newFeeType: FeeType,
newFeePercentage: BigNumber,
txOpts: Tx,
): Promise<string> {
await this.assertAdjustFees(
manager,
newFeeType,
newFeePercentage,
txOpts
);

const newFeeCallData = SetUtils.generateAdjustFeeCallData(
new BigNumber(newFeeType),
newFeePercentage
);

return this.assetPairManagerV2.adjustFee(
manager,
newFeeCallData,
txOpts
);
}

/**
* Cancels previous fee adjustment (before enacted)
*
* @param manager Address of manager
* @param upgradeHash Hash of the inital fee adjustment call data
* @return The hash of the resulting transaction.
*/
public async removeFeeUpdateAsync(
manager: Address,
upgradeHash: string,
txOpts: Tx,
): Promise<string> {
return this.assetPairManagerV2.removeRegisteredUpgrade(manager, upgradeHash);
}

/**
* Calls AssetPairManagerV2's setLiquidator function. Changes liquidator used in rebalances.
*
* @param manager Address of the asset pair manager contract
* @param newLiquidator New liquidator address
* @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
* gasPrice data
* @return The hash of the resulting transaction.
*/
public async setLiquidatorAsync(
manager: Address,
newLiquidator: Address,
txOpts: Tx,
): Promise<string> {
await this.assertAddressSetters(manager, newLiquidator);

return this.assetPairManagerV2.setLiquidator(
manager,
newLiquidator,
txOpts
);
}

/**
* Calls AssetPairManagerV2's setLiquidatorData function. Changes liquidatorData used in rebalances.
*
* @param manager Address of the asset pair manager contract
* @param newLiquidatorData New liquidator data
* @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
* gasPrice data
* @return The hash of the resulting transaction.
*/
public async setLiquidatorDataAsync(
manager: Address,
newLiquidatorData: Bytes,
txOpts: Tx,
): Promise<string> {
return this.assetPairManagerV2.setLiquidatorData(
manager,
newLiquidatorData,
txOpts
);
}

/**
* Calls AssetPairManagerV2's setFeeRecipient function. Changes feeRecipient address.
*
* @param manager Address of the asset pair manager contract
* @param newFeeRecipient New feeRecipient address
* @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
* gasPrice data
* @return The hash of the resulting transaction.
*/
public async setFeeRecipientAsync(
manager: Address,
newFeeRecipient: Address,
txOpts: Tx,
): Promise<string> {
await this.assertAddressSetters(manager, newFeeRecipient);

return this.assetPairManagerV2.setFeeRecipient(
manager,
newFeeRecipient,
txOpts
);
}

/**
* Fetches if initialPropose can be called without revert on AssetPairManager
*
Expand Down Expand Up @@ -520,6 +655,66 @@ export class RebalancingManagerAPI {
} as AssetPairManagerDetails;
}

/**
* Fetches the state variables of the Asset Pair Manager V2 contract.
*
* @param manager Address of the AssetPairManagerV2 contract
* @return Object containing the state information related to the manager
*/
public async getAssetPairManagerV2DetailsAsync(
manager: Address
): Promise<AssetPairManagerV2Details> {
const [
allocationDenominator,
allocator,
baseAssetAllocation,
bullishBaseAssetAllocation,
bearishBaseAssetAllocation,
core,
recentInitialProposeTimestamp,
rebalancingSetToken,
] = await Promise.all([
this.assetPairManagerV2.allocationDenominator(manager),
this.assetPairManagerV2.allocator(manager),
this.assetPairManagerV2.baseAssetAllocation(manager),
this.assetPairManagerV2.bullishBaseAssetAllocation(manager),
this.assetPairManagerV2.bearishBaseAssetAllocation(manager),
this.assetPairManagerV2.core(manager),
this.assetPairManagerV2.recentInitialProposeTimestamp(manager),
this.assetPairManagerV2.rebalancingSetToken(manager),
]);

const [
signalConfirmationMinTime,
signalConfirmationMaxTime,
trigger,
liquidatorData,
rebalanceFeeCalculator,
] = await Promise.all([
this.assetPairManagerV2.signalConfirmationMinTime(manager),
this.assetPairManagerV2.signalConfirmationMaxTime(manager),
this.assetPairManagerV2.trigger(manager),
this.assetPairManagerV2.liquidatorData(manager),
this.rebalancingSetV3.rebalanceFeeCalculator(rebalancingSetToken),
]);

return {
allocationDenominator,
allocator,
baseAssetAllocation,
bullishBaseAssetAllocation,
bearishBaseAssetAllocation,
core,
recentInitialProposeTimestamp,
rebalancingSetToken,
signalConfirmationMinTime,
signalConfirmationMaxTime,
trigger,
liquidatorData,
rebalanceFeeCalculator,
} as AssetPairManagerV2Details;
}

/**
* Fetches the crossover confirmation time of AssetPairManager contracts.
*
Expand Down Expand Up @@ -789,6 +984,41 @@ export class RebalancingManagerAPI {
);
}

private async assertAdjustFees(
manager: Address,
newFeeType: FeeType,
newFeePercentage: BigNumber,
txOpts: Tx
): Promise<void> {
const managerDetails = await this.getAssetPairManagerV2DetailsAsync(manager);
const feeCalculatorAddress = managerDetails.rebalanceFeeCalculator;

let maxFee: BigNumber;
if (newFeeType == FeeType.StreamingFee) {
maxFee = await this.performanceFeeCalculator.maximumStreamingFeePercentage(
feeCalculatorAddress
);
} else {
maxFee = await this.performanceFeeCalculator.maximumProfitFeePercentage(
feeCalculatorAddress
);
}

this.assert.common.isGreaterOrEqualThan(
maxFee,
newFeePercentage,
'Passed fee exceeds allowed maximum.'
);
}

private async assertAddressSetters(
manager: Address,
newAddress: Address,
): Promise<void> {
this.assert.schema.isValidAddress('manager', manager);
this.assert.schema.isValidAddress('newAddress', newAddress);
}

// Helper functions

private assertCrossoverTriggerMet(
Expand Down
16 changes: 16 additions & 0 deletions src/types/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ export interface AssetPairManagerDetails {
trigger: Address;
}

export interface AssetPairManagerV2Details {
allocationDenominator: BigNumber;
allocator: Address;
baseAssetAllocation: BigNumber;
bullishBaseAssetAllocation: BigNumber;
bearishBaseAssetAllocation: BigNumber;
core: Address;
recentInitialProposeTimestamp: BigNumber;
rebalancingSetToken: Address;
signalConfirmationMinTime: BigNumber;
signalConfirmationMaxTime: BigNumber;
trigger: Address;
liquidatorData: string;
rebalanceFeeCalculator: Address;
}

export interface BTCETHRebalancingManagerDetails {
core: Address;
btcPriceFeed: Address;
Expand Down
1 change: 1 addition & 0 deletions src/wrappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export { WhitelistWrapper } from './set_protocol/WhitelistWrapper';
export { VaultWrapper } from './set_protocol/VaultWrapper';

export { AssetPairManagerWrapper } from './strategies/AssetPairManagerWrapper';
export { AssetPairManagerV2Wrapper } from './strategies/AssetPairManagerV2Wrapper';
export { BTCDAIRebalancingManagerWrapper } from './strategies/BTCDAIRebalancingManagerWrapper';
export { BTCETHRebalancingManagerWrapper } from './strategies/BTCETHRebalancingManagerWrapper';
export { ETHDAIRebalancingManagerWrapper } from './strategies/ETHDAIRebalancingManagerWrapper';
Expand Down
14 changes: 14 additions & 0 deletions src/wrappers/set_protocol/RebalancingSetTokenV3Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ export class RebalancingSetTokenV3Wrapper {
txOpts
);
}

/**
* Get rebalanceFeeCalculator
*
* @param rebalancingSetAddress Address of the Set
* @return Transaction hash
*/
public async rebalanceFeeCalculator(
rebalancingSetAddress: Address,
): Promise<string> {
const rebalancingSetTokenInstance = await this.contracts.loadRebalancingSetTokenV3Async(rebalancingSetAddress);

return await rebalancingSetTokenInstance.rebalanceFeeCalculator.callAsync();
}
}
Loading