@@ -18,17 +18,22 @@ import request from 'supertest';
1818import { getFixedRepresentation , sendRequest } from '../../../helpers/helpers' ;
1919import config from '../../../../src/config' ;
2020import { DateTime } from 'luxon' ;
21+ import Big from 'big.js' ;
2122
2223describe ( 'vault-controller#V4' , ( ) => {
2324 const experimentVaultsPrevVal : string = config . EXPERIMENT_VAULTS ;
2425 const experimentVaultMarketsPrevVal : string = config . EXPERIMENT_VAULT_MARKETS ;
26+ const latestBlockHeight : string = '25' ;
2527 const currentBlockHeight : string = '7' ;
2628 const twoHourBlockHeight : string = '5' ;
2729 const twoDayBlockHeight : string = '3' ;
2830 const currentTime : DateTime = DateTime . utc ( ) . startOf ( 'day' ) . minus ( { hour : 5 } ) ;
31+ const latestTime : DateTime = currentTime . plus ( { second : 5 } ) ;
2932 const twoHoursAgo : DateTime = currentTime . minus ( { hour : 2 } ) ;
3033 const twoDaysAgo : DateTime = currentTime . minus ( { day : 2 } ) ;
3134 const initialFundingIndex : string = '10000' ;
35+ const vault1Equity : number = 159500 ;
36+ const vault2Equity : number = 10000 ;
3237
3338 beforeAll ( async ( ) => {
3439 await dbHelpers . migrate ( ) ;
@@ -61,8 +66,33 @@ describe('vault-controller#V4', () => {
6166 time : currentTime . toISO ( ) ,
6267 blockHeight : currentBlockHeight ,
6368 } ) ,
69+ BlockTable . create ( {
70+ ...testConstants . defaultBlock ,
71+ time : latestTime . toISO ( ) ,
72+ blockHeight : latestBlockHeight ,
73+ } ) ,
6474 ] ) ;
6575 await SubaccountTable . create ( testConstants . vaultSubaccount ) ;
76+ await Promise . all ( [
77+ PerpetualPositionTable . create (
78+ testConstants . defaultPerpetualPosition ,
79+ ) ,
80+ AssetPositionTable . upsert ( testConstants . defaultAssetPosition ) ,
81+ AssetPositionTable . upsert ( {
82+ ...testConstants . defaultAssetPosition ,
83+ subaccountId : testConstants . vaultSubaccountId ,
84+ } ) ,
85+ FundingIndexUpdatesTable . create ( {
86+ ...testConstants . defaultFundingIndexUpdate ,
87+ fundingIndex : initialFundingIndex ,
88+ effectiveAtHeight : testConstants . createdHeight ,
89+ } ) ,
90+ FundingIndexUpdatesTable . create ( {
91+ ...testConstants . defaultFundingIndexUpdate ,
92+ eventId : testConstants . defaultTendermintEventId2 ,
93+ effectiveAtHeight : twoDayBlockHeight ,
94+ } ) ,
95+ ] ) ;
6696 } ) ;
6797
6898 afterEach ( async ( ) => {
@@ -93,17 +123,25 @@ describe('vault-controller#V4', () => {
93123 expectedTicksIndex : number [ ] ,
94124 ) => {
95125 const createdPnlTicks : PnlTicksFromDatabase [ ] = await createPnlTicks ( ) ;
126+ const finalTick : PnlTicksFromDatabase = {
127+ ...createdPnlTicks [ expectedTicksIndex [ expectedTicksIndex . length - 1 ] ] ,
128+ equity : Big ( vault1Equity ) . toFixed ( ) ,
129+ blockHeight : latestBlockHeight ,
130+ blockTime : latestTime . toISO ( ) ,
131+ createdAt : latestTime . toISO ( ) ,
132+ } ;
96133
97134 const response : request . Response = await sendRequest ( {
98135 type : RequestMethod . GET ,
99136 path : `/v4/vault/v1/megavault/historicalPnl${ queryParam } ` ,
100137 } ) ;
101138
139+ expect ( response . body . megavaultPnl ) . toHaveLength ( expectedTicksIndex . length + 1 ) ;
102140 expect ( response . body . megavaultPnl ) . toEqual (
103141 expect . arrayContaining (
104142 expectedTicksIndex . map ( ( index : number ) => {
105143 return expect . objectContaining ( createdPnlTicks [ index ] ) ;
106- } ) ,
144+ } ) . concat ( [ finalTick ] ) ,
107145 ) ,
108146 ) ;
109147 } ) ;
@@ -127,7 +165,6 @@ describe('vault-controller#V4', () => {
127165 ] . join ( ',' ) ;
128166
129167 const createdPnlTicks : PnlTicksFromDatabase [ ] = await createPnlTicks ( ) ;
130-
131168 const response : request . Response = await sendRequest ( {
132169 type : RequestMethod . GET ,
133170 path : `/v4/vault/v1/megavault/historicalPnl${ queryParam } ` ,
@@ -138,7 +175,15 @@ describe('vault-controller#V4', () => {
138175 totalPnl : ( parseFloat ( testConstants . defaultPnlTick . totalPnl ) * 2 ) . toString ( ) ,
139176 netTransfers : ( parseFloat ( testConstants . defaultPnlTick . netTransfers ) * 2 ) . toString ( ) ,
140177 } ;
178+ const finalTick : PnlTicksFromDatabase = {
179+ ...expectedPnlTickBase ,
180+ equity : Big ( vault1Equity ) . add ( vault2Equity ) . toFixed ( ) ,
181+ blockHeight : latestBlockHeight ,
182+ blockTime : latestTime . toISO ( ) ,
183+ createdAt : latestTime . toISO ( ) ,
184+ } ;
141185
186+ expect ( response . body . megavaultPnl ) . toHaveLength ( expectedTicksIndex . length + 1 ) ;
142187 expect ( response . body . megavaultPnl ) . toEqual (
143188 expect . arrayContaining (
144189 expectedTicksIndex . map ( ( index : number ) => {
@@ -148,7 +193,7 @@ describe('vault-controller#V4', () => {
148193 blockHeight : createdPnlTicks [ index ] . blockHeight ,
149194 blockTime : createdPnlTicks [ index ] . blockTime ,
150195 } ) ;
151- } ) ,
196+ } ) . concat ( [ expect . objectContaining ( finalTick ) ] ) ,
152197 ) ,
153198 ) ;
154199 } ) ;
@@ -175,20 +220,27 @@ describe('vault-controller#V4', () => {
175220 expectedTicksIndex : number [ ] ,
176221 ) => {
177222 const createdPnlTicks : PnlTicksFromDatabase [ ] = await createPnlTicks ( ) ;
223+ const finalTick : PnlTicksFromDatabase = {
224+ ...createdPnlTicks [ expectedTicksIndex [ expectedTicksIndex . length - 1 ] ] ,
225+ equity : Big ( vault1Equity ) . toFixed ( ) ,
226+ blockHeight : latestBlockHeight ,
227+ blockTime : latestTime . toISO ( ) ,
228+ createdAt : latestTime . toISO ( ) ,
229+ } ;
178230
179231 const response : request . Response = await sendRequest ( {
180232 type : RequestMethod . GET ,
181233 path : `/v4/vault/v1/vaults/historicalPnl${ queryParam } ` ,
182234 } ) ;
183235
184236 expect ( response . body . vaultsPnl ) . toHaveLength ( 1 ) ;
185-
237+ expect ( response . body . vaultsPnl [ 0 ] . historicalPnl ) . toHaveLength ( expectedTicksIndex . length + 1 ) ;
186238 expect ( response . body . vaultsPnl [ 0 ] ) . toEqual ( {
187239 ticker : testConstants . defaultPerpetualMarket . ticker ,
188240 historicalPnl : expect . arrayContaining (
189241 expectedTicksIndex . map ( ( index : number ) => {
190242 return expect . objectContaining ( createdPnlTicks [ index ] ) ;
191- } ) ,
243+ } ) . concat ( finalTick ) ,
192244 ) ,
193245 } ) ;
194246 } ) ;
@@ -213,6 +265,20 @@ describe('vault-controller#V4', () => {
213265 ] . join ( ',' ) ;
214266
215267 const createdPnlTicks : PnlTicksFromDatabase [ ] = await createPnlTicks ( ) ;
268+ const finalTick1 : PnlTicksFromDatabase = {
269+ ...createdPnlTicks [ expectedTicksIndex1 [ expectedTicksIndex1 . length - 1 ] ] ,
270+ equity : Big ( vault1Equity ) . toFixed ( ) ,
271+ blockHeight : latestBlockHeight ,
272+ blockTime : latestTime . toISO ( ) ,
273+ createdAt : latestTime . toISO ( ) ,
274+ } ;
275+ const finalTick2 : PnlTicksFromDatabase = {
276+ ...createdPnlTicks [ expectedTicksIndex2 [ expectedTicksIndex2 . length - 1 ] ] ,
277+ equity : Big ( vault2Equity ) . toFixed ( ) ,
278+ blockHeight : latestBlockHeight ,
279+ blockTime : latestTime . toISO ( ) ,
280+ createdAt : latestTime . toISO ( ) ,
281+ } ;
216282
217283 const response : request . Response = await sendRequest ( {
218284 type : RequestMethod . GET ,
@@ -223,14 +289,14 @@ describe('vault-controller#V4', () => {
223289 ticker : testConstants . defaultPerpetualMarket . ticker ,
224290 historicalPnl : expectedTicksIndex1 . map ( ( index : number ) => {
225291 return createdPnlTicks [ index ] ;
226- } ) ,
292+ } ) . concat ( finalTick1 ) ,
227293 } ;
228294
229295 const expectedVaultPnl2 : VaultHistoricalPnl = {
230296 ticker : testConstants . defaultPerpetualMarket2 . ticker ,
231297 historicalPnl : expectedTicksIndex2 . map ( ( index : number ) => {
232298 return createdPnlTicks [ index ] ;
233- } ) ,
299+ } ) . concat ( finalTick2 ) ,
234300 } ;
235301
236302 expect ( response . body . vaultsPnl ) . toEqual (
@@ -260,23 +326,6 @@ describe('vault-controller#V4', () => {
260326 } ) ;
261327
262328 it ( 'Get /megavault/positions with 1 vault subaccount' , async ( ) => {
263- await Promise . all ( [
264- PerpetualPositionTable . create (
265- testConstants . defaultPerpetualPosition ,
266- ) ,
267- AssetPositionTable . upsert ( testConstants . defaultAssetPosition ) ,
268- FundingIndexUpdatesTable . create ( {
269- ...testConstants . defaultFundingIndexUpdate ,
270- fundingIndex : initialFundingIndex ,
271- effectiveAtHeight : testConstants . createdHeight ,
272- } ) ,
273- FundingIndexUpdatesTable . create ( {
274- ...testConstants . defaultFundingIndexUpdate ,
275- eventId : testConstants . defaultTendermintEventId2 ,
276- effectiveAtHeight : twoDayBlockHeight ,
277- } ) ,
278- ] ) ;
279-
280329 const response : request . Response = await sendRequest ( {
281330 type : RequestMethod . GET ,
282331 path : '/v4/vault/v1/megavault/positions' ,
@@ -334,27 +383,6 @@ describe('vault-controller#V4', () => {
334383 testConstants . defaultPerpetualMarket2 . clobPairId ,
335384 ] . join ( ',' ) ;
336385
337- await Promise . all ( [
338- PerpetualPositionTable . create (
339- testConstants . defaultPerpetualPosition ,
340- ) ,
341- AssetPositionTable . upsert ( testConstants . defaultAssetPosition ) ,
342- AssetPositionTable . upsert ( {
343- ...testConstants . defaultAssetPosition ,
344- subaccountId : testConstants . vaultSubaccountId ,
345- } ) ,
346- FundingIndexUpdatesTable . create ( {
347- ...testConstants . defaultFundingIndexUpdate ,
348- fundingIndex : initialFundingIndex ,
349- effectiveAtHeight : testConstants . createdHeight ,
350- } ) ,
351- FundingIndexUpdatesTable . create ( {
352- ...testConstants . defaultFundingIndexUpdate ,
353- eventId : testConstants . defaultTendermintEventId2 ,
354- effectiveAtHeight : twoDayBlockHeight ,
355- } ) ,
356- ] ) ;
357-
358386 const response : request . Response = await sendRequest ( {
359387 type : RequestMethod . GET ,
360388 path : '/v4/vault/v1/megavault/positions' ,
0 commit comments