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

Commit 903d509

Browse files
committed
Pull latest
2 parents 3f9b508 + 2936c00 commit 903d509

17 files changed

+1653
-583
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setprotocol.js",
3-
"version": "1.2.8-rc5",
3+
"version": "1.2.8-rc6",
44
"description": "A javascript library for interacting with the Set protocol",
55
"keywords": [
66
"setProtocol.js",
@@ -70,7 +70,7 @@
7070
"set-protocol-contracts": "1.4.9-beta",
7171
"set-protocol-oracles": "^1.0.16",
7272
"set-protocol-strategies": "^1.1.39",
73-
"set-protocol-viewers": "^1.0.11",
73+
"set-protocol-viewers": "^1.0.12",
7474
"set-protocol-utils": "^1.1.2",
7575
"timekeeper": "^2.1.2",
7676
"tiny-promisify": "^1.0.0",

src/api/RebalancingAPI.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
RebalancingSetCTokenBidderWrapper,
3131
RebalancingSetEthBidderWrapper,
3232
RebalancingSetTokenWrapper,
33+
TWAPLiquidatorWrapper,
3334
} from '../wrappers';
3435
import { BigNumber, parseRebalanceState } from '../util';
3536
import {
@@ -44,6 +45,9 @@ import {
4445
Tx,
4546
TokenFlowsDetails,
4647
} from '../types/common';
48+
import {
49+
RBSetTWAPRebalanceInfo
50+
} from '../types/strategies';
4751

4852
/**
4953
* @title RebalancingAPI
@@ -62,6 +66,7 @@ export class RebalancingAPI {
6266
private rebalancingAuctionModule: RebalancingAuctionModuleWrapper;
6367
private rebalancingSetEthBidder: RebalancingSetEthBidderWrapper;
6468
private rebalancingSetCTokenBidder: RebalancingSetCTokenBidderWrapper;
69+
private twapLiquidator: TWAPLiquidatorWrapper;
6570
private setToken: SetTokenWrapper;
6671
private config: SetProtocolConfig;
6772

@@ -95,6 +100,7 @@ export class RebalancingAPI {
95100
new RebalancingSetCTokenBidderWrapper(this.web3, config.rebalancingSetCTokenBidderAddress);
96101
this.rebalancingSetEthBidder = new RebalancingSetEthBidderWrapper(this.web3, config.rebalancingSetEthBidderAddress);
97102
this.protocolViewer = new ProtocolViewerWrapper(this.web3, config.protocolViewerAddress);
103+
this.twapLiquidator = new TWAPLiquidatorWrapper(this.web3);
98104

99105
this.config = config;
100106
}
@@ -159,6 +165,25 @@ export class RebalancingAPI {
159165
return await this.rebalancingSetToken.startRebalance(rebalancingSetTokenAddress, txOpts);
160166
}
161167

168+
/**
169+
* Iterate TWAP Auction to the next chunkAuction
170+
*
171+
* @param liquidatorAddress Address of TWAP Liquidator
172+
* @param rebalancingSetTokenAddress Address of the Rebalancing Set
173+
* @param txOpts Transaction options object conforming to `Tx` with signer, gas, and
174+
* gasPrice data
175+
* @return Transaction hash
176+
*/
177+
public async iterateChunkAuction(
178+
liquidatorAddress: Address,
179+
rebalancingSetTokenAddress: Address,
180+
txOpts: Tx
181+
): Promise<string> {
182+
await this.assertIterateChunkAuction(rebalancingSetTokenAddress);
183+
184+
return await this.twapLiquidator.iterateChunkAuction(liquidatorAddress, rebalancingSetTokenAddress, txOpts);
185+
}
186+
162187
/**
163188
* Settles rebalance after auction has been completed
164189
*
@@ -630,6 +655,23 @@ export class RebalancingAPI {
630655
} as RebalancingProgressDetails;
631656
}
632657

658+
/**
659+
* Returns relevant details of RebalancingSetTokens being rebalanced by TWAP. Return object adheres to the
660+
* RBSetTWAPRebalanceInfo interface.
661+
*
662+
* @param tradingPool Address of tradingPool being updated
663+
* @return NewTradingPoolInfo
664+
*/
665+
public async fetchRBSetTWAPRebalanceDetailsAsync(
666+
rebalancingSetTokenAddress: Address
667+
): Promise<RBSetTWAPRebalanceInfo> {
668+
const newPoolInfo = await this.protocolViewer.fetchRBSetTWAPRebalanceDetails(
669+
rebalancingSetTokenAddress
670+
);
671+
672+
return this.createRBSetTWAPRebalanceInfoObject(newPoolInfo);
673+
}
674+
633675
/**
634676
* Fetches the current state of the RebalancingSetToken
635677
*
@@ -690,6 +732,32 @@ export class RebalancingAPI {
690732
return await this.rebalancingSetToken.remainingCurrentSets(rebalancingSetTokenAddress);
691733
}
692734

735+
private createRBSetTWAPRebalanceInfoObject(
736+
newPoolInfo: any,
737+
): RBSetTWAPRebalanceInfo {
738+
const [rbSetInfo, collateralInfo] = newPoolInfo;
739+
740+
return {
741+
liquidator: rbSetInfo.liquidator,
742+
nextSet: rbSetInfo.nextSet,
743+
rebalanceStartTime: new BigNumber(rbSetInfo.rebalanceStartTime),
744+
timeToPivot: new BigNumber(rbSetInfo.timeToPivot),
745+
startPrice: new BigNumber(rbSetInfo.startPrice),
746+
endPrice: new BigNumber(rbSetInfo.endPrice),
747+
startingCurrentSets: new BigNumber(rbSetInfo.startingCurrentSets),
748+
remainingCurrentSets: new BigNumber(rbSetInfo.remainingCurrentSets),
749+
minimumBid: new BigNumber(rbSetInfo.minimumBid),
750+
orderSize: new BigNumber(rbSetInfo.orderSize),
751+
orderRemaining: new BigNumber(rbSetInfo.orderRemaining),
752+
totalSetsRemaining: new BigNumber(rbSetInfo.totalSetsRemaining),
753+
chunkSize: new BigNumber(rbSetInfo.chunkSize),
754+
chunkAuctionPeriod: new BigNumber(rbSetInfo.chunkAuctionPeriod),
755+
lastChunkAuctionEnd: new BigNumber(rbSetInfo.lastChunkAuctionEnd),
756+
rebalanceState: rbSetInfo.rebalanceState,
757+
nextSetInfo: collateralInfo,
758+
} as RBSetTWAPRebalanceInfo;
759+
}
760+
693761
/* ============ Private Assertions ============ */
694762

695763
private async assertPropose(
@@ -732,6 +800,25 @@ export class RebalancingAPI {
732800
await this.assert.rebalancing.sufficientTimeInProposalState(rebalancingSetTokenAddress);
733801
}
734802

803+
private async assertIterateChunkAuction(rebalancingSetTokenAddress: Address) {
804+
const twapRebalanceState = await this.fetchRBSetTWAPRebalanceDetailsAsync(rebalancingSetTokenAddress);
805+
806+
const nextChunkAuctionStart = twapRebalanceState.lastChunkAuctionEnd
807+
.add(twapRebalanceState.chunkAuctionPeriod);
808+
this.assert.rebalancing.sufficientTimeBetweenChunkAuctions(nextChunkAuctionStart);
809+
810+
this.assert.rebalancing.chunkAuctionFinished(
811+
twapRebalanceState.minimumBid,
812+
twapRebalanceState.remainingCurrentSets
813+
);
814+
815+
this.assert.rebalancing.twapAuctionNotFinished(
816+
rebalancingSetTokenAddress,
817+
twapRebalanceState.minimumBid,
818+
twapRebalanceState.totalSetsRemaining
819+
);
820+
}
821+
735822
private async assertSettleRebalance(rebalancingSetTokenAddress: Address) {
736823
this.assert.schema.isValidAddress('rebalancingSetTokenAddress', rebalancingSetTokenAddress);
737824

src/api/SocialTradingAPI.ts

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import {
4646
NewTradingPoolV2Info,
4747
PerformanceFeeInfo,
4848
TradingPoolAccumulationInfo,
49-
TradingPoolRebalanceInfo
49+
TradingPoolRebalanceInfo,
50+
TradingPoolTWAPRebalanceInfo,
5051
} from '../types/strategies';
5152

5253
const { SetProtocolUtils: SetUtils } = setProtocolUtils;
@@ -538,6 +539,23 @@ export class SocialTradingAPI {
538539
return this.createTradingPoolRebalanceObject(newPoolInfo);
539540
}
540541

542+
/**
543+
* Returns relevant details of Trading Pools being rebalanced by TWAP. Return object adheres to the
544+
* TradingPoolTWAPRebalanceInfo interface.
545+
*
546+
* @param tradingPool Address of tradingPool being updated
547+
* @return NewTradingPoolInfo
548+
*/
549+
public async fetchTradingPoolTWAPRebalanceDetailsAsync(
550+
tradingPool: Address
551+
): Promise<TradingPoolTWAPRebalanceInfo> {
552+
const newPoolInfo = await this.protocolViewer.fetchTradingPoolTWAPRebalanceDetails(
553+
tradingPool
554+
);
555+
556+
return this.createTradingPoolTWAPRebalanceObject(newPoolInfo);
557+
}
558+
541559
/**
542560
* Fetches all trading pool operators for an array of trading pools
543561
*
@@ -701,18 +719,18 @@ export class SocialTradingAPI {
701719
return {
702720
trader: poolInfo.trader,
703721
allocator: poolInfo.allocator,
704-
currentAllocation: poolInfo.currentAllocation,
722+
currentAllocation: new BigNumber(poolInfo.currentAllocation),
705723
manager: rbSetInfo.manager,
706724
feeRecipient: rbSetInfo.feeRecipient,
707725
currentSet: rbSetInfo.currentSet,
708726
poolName: rbSetInfo.name,
709727
poolSymbol: rbSetInfo.symbol,
710-
unitShares: rbSetInfo.unitShares,
711-
naturalUnit: rbSetInfo.naturalUnit,
712-
rebalanceInterval: rbSetInfo.rebalanceInterval,
713-
entryFee: rbSetInfo.entryFee,
714-
rebalanceFee: rbSetInfo.rebalanceFee,
715-
lastRebalanceTimestamp: rbSetInfo.lastRebalanceTimestamp,
728+
unitShares: new BigNumber(rbSetInfo.unitShares),
729+
naturalUnit: new BigNumber(rbSetInfo.naturalUnit),
730+
rebalanceInterval: new BigNumber(rbSetInfo.rebalanceInterval),
731+
entryFee: new BigNumber(rbSetInfo.entryFee),
732+
rebalanceFee: new BigNumber(rbSetInfo.rebalanceFee),
733+
lastRebalanceTimestamp: new BigNumber(rbSetInfo.lastRebalanceTimestamp),
716734
rebalanceState: rbSetInfo.rebalanceState,
717735
currentSetInfo: collateralInfo,
718736
} as NewTradingPoolInfo;
@@ -732,18 +750,18 @@ export class SocialTradingAPI {
732750
return {
733751
trader: poolInfo.trader,
734752
allocator: poolInfo.allocator,
735-
currentAllocation: poolInfo.currentAllocation,
753+
currentAllocation: new BigNumber(poolInfo.currentAllocation),
736754
manager: rbSetInfo.manager,
737755
feeRecipient: rbSetInfo.feeRecipient,
738756
currentSet: rbSetInfo.currentSet,
739757
poolName: rbSetInfo.name,
740758
poolSymbol: rbSetInfo.symbol,
741-
unitShares: rbSetInfo.unitShares,
742-
naturalUnit: rbSetInfo.naturalUnit,
743-
rebalanceInterval: rbSetInfo.rebalanceInterval,
744-
entryFee: rbSetInfo.entryFee,
745-
rebalanceFee: rbSetInfo.rebalanceFee,
746-
lastRebalanceTimestamp: rbSetInfo.lastRebalanceTimestamp,
759+
unitShares: new BigNumber(rbSetInfo.unitShares),
760+
naturalUnit: new BigNumber(rbSetInfo.naturalUnit),
761+
rebalanceInterval: new BigNumber(rbSetInfo.rebalanceInterval),
762+
entryFee: new BigNumber(rbSetInfo.entryFee),
763+
rebalanceFee: new BigNumber(rbSetInfo.rebalanceFee),
764+
lastRebalanceTimestamp: new BigNumber(rbSetInfo.lastRebalanceTimestamp),
747765
rebalanceState: rbSetInfo.rebalanceState,
748766
currentSetInfo: collateralInfo,
749767
performanceFeeInfo: perfFeeInfo,
@@ -759,21 +777,50 @@ export class SocialTradingAPI {
759777
return {
760778
trader: poolInfo.trader,
761779
allocator: poolInfo.allocator,
762-
currentAllocation: poolInfo.currentAllocation,
780+
currentAllocation: new BigNumber(poolInfo.currentAllocation),
763781
liquidator: rbSetInfo.liquidator,
764782
nextSet: rbSetInfo.nextSet,
765-
rebalanceStartTime: rbSetInfo.rebalanceStartTime,
766-
timeToPivot: rbSetInfo.timeToPivot,
767-
startPrice: rbSetInfo.startPrice,
768-
endPrice: rbSetInfo.endPrice,
769-
startingCurrentSets: rbSetInfo.startingCurrentSets,
770-
remainingCurrentSets: rbSetInfo.remainingCurrentSets,
771-
minimumBid: rbSetInfo.minimumBid,
783+
rebalanceStartTime: new BigNumber(rbSetInfo.rebalanceStartTime),
784+
timeToPivot: new BigNumber(rbSetInfo.timeToPivot),
785+
startPrice: new BigNumber(rbSetInfo.startPrice),
786+
endPrice: new BigNumber(rbSetInfo.endPrice),
787+
startingCurrentSets: new BigNumber(rbSetInfo.startingCurrentSets),
788+
remainingCurrentSets: new BigNumber(rbSetInfo.remainingCurrentSets),
789+
minimumBid: new BigNumber(rbSetInfo.minimumBid),
772790
rebalanceState: rbSetInfo.rebalanceState,
773791
nextSetInfo: collateralInfo,
774792
} as TradingPoolRebalanceInfo;
775793
}
776794

795+
private createTradingPoolTWAPRebalanceObject(
796+
newPoolInfo: any,
797+
): TradingPoolTWAPRebalanceInfo {
798+
const [poolInfo, rbSetInfo, collateralInfo] = newPoolInfo;
799+
800+
return {
801+
trader: poolInfo.trader,
802+
allocator: poolInfo.allocator,
803+
currentAllocation: new BigNumber(poolInfo.currentAllocation),
804+
liquidator: rbSetInfo.liquidator,
805+
nextSet: rbSetInfo.nextSet,
806+
rebalanceStartTime: new BigNumber(rbSetInfo.rebalanceStartTime),
807+
timeToPivot: new BigNumber(rbSetInfo.timeToPivot),
808+
startPrice: new BigNumber(rbSetInfo.startPrice),
809+
endPrice: new BigNumber(rbSetInfo.endPrice),
810+
startingCurrentSets: new BigNumber(rbSetInfo.startingCurrentSets),
811+
remainingCurrentSets: new BigNumber(rbSetInfo.remainingCurrentSets),
812+
minimumBid: new BigNumber(rbSetInfo.minimumBid),
813+
orderSize: new BigNumber(rbSetInfo.orderSize),
814+
orderRemaining: new BigNumber(rbSetInfo.orderRemaining),
815+
totalSetsRemaining: new BigNumber(rbSetInfo.totalSetsRemaining),
816+
chunkSize: new BigNumber(rbSetInfo.chunkSize),
817+
chunkAuctionPeriod: new BigNumber(rbSetInfo.chunkAuctionPeriod),
818+
lastChunkAuctionEnd: new BigNumber(rbSetInfo.lastChunkAuctionEnd),
819+
rebalanceState: rbSetInfo.rebalanceState,
820+
nextSetInfo: collateralInfo,
821+
} as TradingPoolTWAPRebalanceInfo;
822+
}
823+
777824
private createTradingPoolAccumulationObject(
778825
tradingPoolFees: any,
779826
): TradingPoolAccumulationInfo[] {

src/assertions/RebalancingAssertions.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ export class RebalancingAssertions {
179179
}
180180
}
181181

182+
/**
183+
* Throws if insufficient time between chunk auctions
184+
*
185+
* @param nextChunkAuctionStart UNIX timestamp of next chunk auction start time
186+
*/
187+
public sufficientTimeBetweenChunkAuctions(nextChunkAuctionStart: BigNumber) {
188+
const currentTimeStamp = new BigNumber(Date.now());
189+
const nextChunkAuctionStartMilliSec = nextChunkAuctionStart.mul(1000);
190+
191+
if (nextChunkAuctionStartMilliSec.greaterThan(currentTimeStamp)) {
192+
const nextChunkAuctionFormattedDate = moment(nextChunkAuctionStartMilliSec.toNumber())
193+
.format('dddd, MMMM Do YYYY, h:mm:ss a');
194+
throw new Error(rebalancingErrors.INSUFFICIENT_TIME_BETWEEN_CHUNKS(nextChunkAuctionFormattedDate));
195+
}
196+
}
197+
182198
/**
183199
* Throws if not enough time passed between last rebalance on rebalancing set token
184200
*
@@ -252,13 +268,48 @@ export class RebalancingAssertions {
252268

253269
if (remainingCurrentSets.greaterThanOrEqualTo(minimumBid)) {
254270
throw new Error(rebalancingErrors.NOT_ENOUGH_SETS_REBALANCED(
255-
rebalancingSetTokenAddress,
256271
minimumBid.toString(),
257272
remainingCurrentSets.toString()
258273
));
259274
}
260275
}
261276

277+
/**
278+
* Throws if not enough current sets rebalanced in chunk auction
279+
*
280+
* @param rebalancingSetTokenAddress The address of the rebalancing set token
281+
* @param minimumBid Minimum Bid of the auction
282+
* @param remainingCurrentSets Amount of remainingSet in current chunk auction
283+
*/
284+
public chunkAuctionFinished(
285+
minimumBid: BigNumber,
286+
remainingCurrentSets: BigNumber,
287+
) {
288+
if (remainingCurrentSets.greaterThanOrEqualTo(minimumBid)) {
289+
throw new Error(rebalancingErrors.CHUNK_AUCTION_NOT_FINISHED(
290+
minimumBid.toString(),
291+
remainingCurrentSets.toString()
292+
));
293+
}
294+
}
295+
296+
/**
297+
* Throws if twap auction is finished
298+
*
299+
* @param rebalancingSetTokenAddress The address of the rebalancing set token
300+
* @param minimumBid Minimum Bid of the auction
301+
* @param remainingCurrentSets Amount of remainingSet in current chunk auction
302+
*/
303+
public twapAuctionNotFinished(
304+
rebalancingSetTokenAddress: Address,
305+
minimumBid: BigNumber,
306+
totalRemainingSets: BigNumber,
307+
) {
308+
if (totalRemainingSets.lessThan(minimumBid)) {
309+
throw new Error(rebalancingErrors.TWAP_AUCTION_FINISHED(rebalancingSetTokenAddress));
310+
}
311+
}
312+
262313
/**
263314
* Throws if not past pivot time
264315
*

src/errors/rebalancingErrors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const rebalancingErrors = {
2828
`${nextSetAddress} must be a multiple of ${currentSetAddress}, or vice versa to propose a valid rebalance.`,
2929
INCORRECT_STATE: (rebalancingSetAddress: string, requiredState: string) => `Rebalancing token at ` +
3030
`${rebalancingSetAddress} must be in ${requiredState} state to call that function.`,
31-
NOT_ENOUGH_SETS_REBALANCED: (nextAvailableRebalance: string, minimumBid: string, remainingCurrentSets: string) =>
31+
NOT_ENOUGH_SETS_REBALANCED: (minimumBid: string, remainingCurrentSets: string) =>
3232
`In order to settle rebalance there must be less than current ${minimumBid} sets remaining to be rebalanced. ` +
3333
`There are currently ${remainingCurrentSets} remaining for rebalance.`,
3434
BID_AMOUNT_EXCEEDS_REMAINING_CURRENT_SETS: (remainingCurrentSets: string, bidQuantity: string) => `The submitted ` +
@@ -39,4 +39,11 @@ export const rebalancingErrors = {
3939
`starts at ${pivotTimeStart}`,
4040
NOT_VALID_DRAWDOWN: (rebalancingSetAddress: string) =>
4141
`Auction has no remaining bids. Cannot drawdown Set at ${rebalancingSetAddress}.`,
42+
TWAP_AUCTION_FINISHED: (rebalancingSetAddress: string) =>
43+
`The TWAPAuction for ${rebalancingSetAddress} is finished. Cannot iterate to next chunk.`,
44+
CHUNK_AUCTION_NOT_FINISHED: (minimumBid: string, remainingCurrentSets: string) =>
45+
`In order to iterate the chunk auction there must be less than current ${minimumBid} sets remaining to be ` +
46+
`rebalanced. There are currently ${remainingCurrentSets} remaining for the chunk auction.`,
47+
INSUFFICIENT_TIME_BETWEEN_CHUNKS: (nextAvailableRebalance: string) => `Attempting to iterate auction too soon.` +
48+
` Next chunk available at ${nextAvailableRebalance}`,
4249
};

0 commit comments

Comments
 (0)