11/*
2- Copyright 2018 Set Labs Inc.
2+ Copyright 2019 Set Labs Inc.
33
44 Licensed under the Apache License, Version 2.0 (the "License");
55 you may not use this file except in compliance with the License.
@@ -26,8 +26,10 @@ import {
2626 ProtocolViewerWrapper ,
2727 RebalancingSetTokenV2Wrapper ,
2828 RebalancingSetTokenV3Wrapper ,
29+ SocialAllocatorWrapper ,
2930 SocialTradingManagerWrapper ,
30- SocialTradingManagerV2Wrapper
31+ SocialTradingManagerV2Wrapper ,
32+ TWAPLiquidatorWrapper ,
3133} from '../wrappers' ;
3234import { Assertions } from '../assertions' ;
3335import { coreAPIErrors } from '../errors' ;
@@ -38,7 +40,8 @@ import {
3840 SetProtocolConfig ,
3941 Tx ,
4042 EntryFeePaid ,
41- RebalanceFeePaid
43+ RebalanceFeePaid ,
44+ Bounds
4245} from '../types/common' ;
4346
4447import {
@@ -61,12 +64,14 @@ const { SetProtocolUtils: SetUtils } = setProtocolUtils;
6164export class SocialTradingAPI {
6265 private web3 : Web3 ;
6366 private assert : Assertions ;
67+ private performanceFeeCalculator : PerformanceFeeCalculatorWrapper ;
6468 private protocolViewer : ProtocolViewerWrapper ;
69+ private socialAllocator : SocialAllocatorWrapper ;
6570 private socialTradingManager : SocialTradingManagerWrapper ;
6671 private socialTradingManagerV2 : SocialTradingManagerV2Wrapper ;
6772 private rebalancingSetV2 : RebalancingSetTokenV2Wrapper ;
6873 private rebalancingSetV3 : RebalancingSetTokenV3Wrapper ;
69- private performanceFeeCalculator : PerformanceFeeCalculatorWrapper ;
74+ private twapLiquidator : TWAPLiquidatorWrapper ;
7075
7176 /**
7277 * Instantiates a new RebalancingManagerAPI instance that contains methods for issuing and redeeming Sets
@@ -79,11 +84,13 @@ export class SocialTradingAPI {
7984 constructor ( web3 : Web3 , assertions : Assertions , config : SetProtocolConfig ) {
8085 this . web3 = web3 ;
8186 this . protocolViewer = new ProtocolViewerWrapper ( web3 , config . protocolViewerAddress ) ;
87+ this . socialAllocator = new SocialAllocatorWrapper ( web3 ) ;
8288 this . socialTradingManager = new SocialTradingManagerWrapper ( web3 ) ;
8389 this . socialTradingManagerV2 = new SocialTradingManagerV2Wrapper ( web3 ) ;
8490 this . rebalancingSetV2 = new RebalancingSetTokenV2Wrapper ( web3 ) ;
8591 this . rebalancingSetV3 = new RebalancingSetTokenV3Wrapper ( web3 ) ;
8692 this . performanceFeeCalculator = new PerformanceFeeCalculatorWrapper ( web3 ) ;
93+ this . twapLiquidator = new TWAPLiquidatorWrapper ( web3 ) ;
8794
8895 this . assert = assertions ;
8996 }
@@ -291,6 +298,47 @@ export class SocialTradingAPI {
291298 ) ;
292299 }
293300
301+ /**
302+ * Calls SocialTradingManager's updateAllocation function. This function creates a new collateral Set and
303+ * calls startRebalance on RebalancingSetTokenV2 using a TWAPLiquidator Updates allocation state on Manager
304+ * contract.
305+ *
306+ * @param manager Address of the social trading manager contract
307+ * @param tradingPool Address of tradingPool being updated
308+ * @param newAllocation New allocation amount in base asset percentage
309+ * @param chunkSize Value to be auctioned in each chunk
310+ * @param chunkAuctionPeriod Time between chunk auctions
311+ * @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
312+ * gasPrice data
313+ * @return The hash of the resulting transaction.
314+ */
315+ public async updateAllocationWithTWAPAsync (
316+ manager : Address ,
317+ tradingPool : Address ,
318+ newAllocation : BigNumber ,
319+ chunkSize : BigNumber ,
320+ chunkAuctionPeriod : BigNumber ,
321+ txOpts : Tx ,
322+ ) : Promise < string > {
323+ await this . assertUpdateAllocationWithTWAP (
324+ manager ,
325+ tradingPool ,
326+ newAllocation ,
327+ chunkSize ,
328+ chunkAuctionPeriod ,
329+ txOpts
330+ ) ;
331+
332+ const liquidatorData = SetUtils . generateTWAPLiquidatorCalldata ( chunkSize , chunkAuctionPeriod ) ;
333+ return this . socialTradingManager . updateAllocation (
334+ manager ,
335+ tradingPool ,
336+ newAllocation ,
337+ liquidatorData ,
338+ txOpts
339+ ) ;
340+ }
341+
294342 /**
295343 * Calls tradingPool to accrue fees to manager.
296344 *
@@ -711,6 +759,15 @@ export class SocialTradingAPI {
711759
712760 /* ============ Private Functions ============ */
713761
762+ private async getTradingPoolAssetPair (
763+ allocator : Address
764+ ) : Promise < [ Address , Address ] > {
765+ const baseAsset = await this . socialAllocator . baseAsset ( allocator ) ;
766+ const quoteAsset = await this . socialAllocator . quoteAsset ( allocator ) ;
767+
768+ return [ baseAsset , quoteAsset ] ;
769+ }
770+
714771 private createNewTradingPoolObject (
715772 newPoolInfo : any ,
716773 ) : NewTradingPoolInfo {
@@ -935,6 +992,29 @@ export class SocialTradingAPI {
935992 this . assertValidAllocation ( newAllocation ) ;
936993 }
937994
995+ private async assertUpdateAllocationWithTWAP (
996+ manager : Address ,
997+ tradingPool : Address ,
998+ newAllocation : BigNumber ,
999+ chunkSize : BigNumber ,
1000+ chunkAuctionPeriod : BigNumber ,
1001+ txOpts : Tx ,
1002+ ) : Promise < void > {
1003+ const poolInfo = await this . fetchNewTradingPoolV2DetailsAsync ( tradingPool ) ;
1004+
1005+ const assetPair = await this . getTradingPoolAssetPair ( poolInfo . allocator ) ;
1006+ const chunkSizeBounds : Bounds = await this . twapLiquidator . chunkSizeWhiteList (
1007+ poolInfo . liquidator ,
1008+ assetPair [ 0 ] ,
1009+ assetPair [ 1 ]
1010+ ) ;
1011+ this . assert . socialTrading . chunkSizeIsBetweenBounds ( chunkSizeBounds , chunkSize ) ;
1012+
1013+ this . assert . socialTrading . chunkAuctionPeriodGreaterOrEqualToZero ( chunkAuctionPeriod ) ;
1014+
1015+ await this . assertUpdateAllocation ( manager , tradingPool , newAllocation , txOpts ) ;
1016+ }
1017+
9381018 private async assertInitiateEntryFeeChange (
9391019 manager : Address ,
9401020 tradingPool : Address ,
0 commit comments