1
1
/*
2
- Copyright 2018 Set Labs Inc.
2
+ Copyright 2019 Set Labs Inc.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -26,8 +26,10 @@ import {
26
26
ProtocolViewerWrapper ,
27
27
RebalancingSetTokenV2Wrapper ,
28
28
RebalancingSetTokenV3Wrapper ,
29
+ SocialAllocatorWrapper ,
29
30
SocialTradingManagerWrapper ,
30
- SocialTradingManagerV2Wrapper
31
+ SocialTradingManagerV2Wrapper ,
32
+ TWAPLiquidatorWrapper ,
31
33
} from '../wrappers' ;
32
34
import { Assertions } from '../assertions' ;
33
35
import { coreAPIErrors } from '../errors' ;
@@ -38,7 +40,8 @@ import {
38
40
SetProtocolConfig ,
39
41
Tx ,
40
42
EntryFeePaid ,
41
- RebalanceFeePaid
43
+ RebalanceFeePaid ,
44
+ Bounds
42
45
} from '../types/common' ;
43
46
44
47
import {
@@ -61,12 +64,14 @@ const { SetProtocolUtils: SetUtils } = setProtocolUtils;
61
64
export class SocialTradingAPI {
62
65
private web3 : Web3 ;
63
66
private assert : Assertions ;
67
+ private performanceFeeCalculator : PerformanceFeeCalculatorWrapper ;
64
68
private protocolViewer : ProtocolViewerWrapper ;
69
+ private socialAllocator : SocialAllocatorWrapper ;
65
70
private socialTradingManager : SocialTradingManagerWrapper ;
66
71
private socialTradingManagerV2 : SocialTradingManagerV2Wrapper ;
67
72
private rebalancingSetV2 : RebalancingSetTokenV2Wrapper ;
68
73
private rebalancingSetV3 : RebalancingSetTokenV3Wrapper ;
69
- private performanceFeeCalculator : PerformanceFeeCalculatorWrapper ;
74
+ private twapLiquidator : TWAPLiquidatorWrapper ;
70
75
71
76
/**
72
77
* Instantiates a new RebalancingManagerAPI instance that contains methods for issuing and redeeming Sets
@@ -79,11 +84,13 @@ export class SocialTradingAPI {
79
84
constructor ( web3 : Web3 , assertions : Assertions , config : SetProtocolConfig ) {
80
85
this . web3 = web3 ;
81
86
this . protocolViewer = new ProtocolViewerWrapper ( web3 , config . protocolViewerAddress ) ;
87
+ this . socialAllocator = new SocialAllocatorWrapper ( web3 ) ;
82
88
this . socialTradingManager = new SocialTradingManagerWrapper ( web3 ) ;
83
89
this . socialTradingManagerV2 = new SocialTradingManagerV2Wrapper ( web3 ) ;
84
90
this . rebalancingSetV2 = new RebalancingSetTokenV2Wrapper ( web3 ) ;
85
91
this . rebalancingSetV3 = new RebalancingSetTokenV3Wrapper ( web3 ) ;
86
92
this . performanceFeeCalculator = new PerformanceFeeCalculatorWrapper ( web3 ) ;
93
+ this . twapLiquidator = new TWAPLiquidatorWrapper ( web3 ) ;
87
94
88
95
this . assert = assertions ;
89
96
}
@@ -291,6 +298,47 @@ export class SocialTradingAPI {
291
298
) ;
292
299
}
293
300
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
+
294
342
/**
295
343
* Calls tradingPool to accrue fees to manager.
296
344
*
@@ -711,6 +759,15 @@ export class SocialTradingAPI {
711
759
712
760
/* ============ Private Functions ============ */
713
761
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
+
714
771
private createNewTradingPoolObject (
715
772
newPoolInfo : any ,
716
773
) : NewTradingPoolInfo {
@@ -935,6 +992,29 @@ export class SocialTradingAPI {
935
992
this . assertValidAllocation ( newAllocation ) ;
936
993
}
937
994
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
+
938
1018
private async assertInitiateEntryFeeChange (
939
1019
manager : Address ,
940
1020
tradingPool : Address ,
0 commit comments