1+ import BigNumber from 'bignumber.js' ;
12import type { PublicClient } from 'viem' ;
23import type { Mock } from 'vitest' ;
34
45import fakeAddress , { altAddress } from '__mocks__/models/address' ;
56import { poolData } from '__mocks__/models/pools' ;
67import tokens from '__mocks__/models/tokens' ;
7- import { apiTradePositions , tradePositions } from '__mocks__/models/trade' ;
8+ import { apiTradePositions } from '__mocks__/models/trade' ;
89import { logError } from 'libs/errors' ;
910import { ChainId } from 'types' ;
10- import { restService } from 'utilities' ;
11+ import { formatToTradePosition , restService } from 'utilities' ;
1112import { type GetRawTradePositionsInput , getRawTradePositions } from '..' ;
1213import { getPools } from '../../useGetPools/getPools' ;
1314
@@ -33,6 +34,25 @@ const fakeInput: GetRawTradePositionsInput = {
3334 tokens,
3435} ;
3536
37+ const formatApiTradePosition = ( apiTradePosition : ( typeof apiTradePositions ) [ number ] ) =>
38+ formatToTradePosition ( {
39+ pool : poolData [ 0 ] ,
40+ chainId : fakeInput . chainId ,
41+ positionAccountAddress : apiTradePosition . positionAccountAddress ,
42+ dsaVTokenAddress : apiTradePosition . dsaVTokenAddress ,
43+ dsaBalanceMantissa : new BigNumber (
44+ apiTradePosition . capitalUtilization . suppliedPrincipalMantissa || 0 ,
45+ ) ,
46+ dsaUtilizedBalanceMantissa : new BigNumber (
47+ apiTradePosition . capitalUtilization . capitalUtilizedMantissa || 0 ,
48+ ) ,
49+ longVTokenAddress : apiTradePosition . longVTokenAddress ,
50+ shortVTokenAddress : apiTradePosition . shortVTokenAddress ,
51+ leverageFactor : Number ( apiTradePosition . effectiveLeverageRatio ?? 0 ) ,
52+ unrealizedPnlCents : Number ( apiTradePosition . pnl ?. unrealizedPnlUsd ?? 0 ) * 100 ,
53+ unrealizedPnlPercentage : Number ( apiTradePosition . pnl ?. unrealizedPnlRatio ?? 0 ) * 100 ,
54+ } ) ;
55+
3656describe ( 'getRawTradePositions' , ( ) => {
3757 beforeEach ( ( ) => {
3858 ( restService as Mock ) . mockResolvedValue ( {
@@ -104,10 +124,10 @@ describe('getRawTradePositions', () => {
104124 it ( 'returns positions in the correct format on success' , async ( ) => {
105125 const response = await getRawTradePositions ( fakeInput ) ;
106126
107- const expectedPositions = tradePositions . slice ( 0 , 2 ) . map ( ( position , index ) => ( {
108- ... position ,
109- unrealizedPnlPercentage : Number ( apiTradePositions [ index ] . pnl ?. unrealizedPnlRatio ?? 0 ) * 100 ,
110- } ) ) ;
127+ const expectedPositions = apiTradePositions
128+ . slice ( 0 , 2 )
129+ . map ( formatApiTradePosition )
130+ . filter ( position => position !== undefined ) ;
111131
112132 expect ( response ) . toEqual ( {
113133 positions : expectedPositions ,
@@ -164,7 +184,7 @@ describe('getRawTradePositions', () => {
164184 const response = await getRawTradePositions ( fakeInput ) ;
165185
166186 expect ( response ) . toEqual ( {
167- positions : [ tradePositions [ 0 ] ] ,
187+ positions : [ formatApiTradePosition ( apiTradePositions [ 0 ] ) ] ,
168188 } ) ;
169189 expect ( logError ) . toHaveBeenCalledTimes ( 1 ) ;
170190 expect ( logError ) . toHaveBeenCalledWith (
0 commit comments