@@ -27,69 +27,75 @@ export const useGetPnl = () => {
27
27
queryFn : async ( ) => {
28
28
if ( ! CoreProxy || ! Multicall3 || ! blocks ) throw 'Missing data required for useGetPnl' ;
29
29
30
- const returnValues = await Promise . all (
31
- blocks . map ( ( block : number ) => {
32
- return Multicall3 . connect (
33
- new providers . JsonRpcBatchProvider ( network ?. rpcUrl ( ) )
34
- ) . callStatic . aggregate (
35
- [
36
- {
37
- target : CoreProxy . address ,
38
- callData : CoreProxy . interface . encodeFunctionData ( 'getVaultCollateral' , [
39
- 1 ,
40
- getsUSDCAddress ( network ?. id ) ,
41
- ] ) ,
42
- } ,
43
- {
44
- target : CoreProxy . address ,
45
- callData : CoreProxy . interface . encodeFunctionData ( 'getVaultDebt' , [
46
- 1 ,
47
- getsUSDCAddress ( network ?. id ) ,
48
- ] ) ,
49
- } ,
50
- ] ,
51
- { blockTag : block }
52
- ) ;
53
- } )
54
- ) ;
30
+ try {
31
+ const returnValues = await Promise . all (
32
+ blocks . map ( ( block : number ) => {
33
+ return Multicall3 . connect (
34
+ new providers . JsonRpcBatchProvider ( network ?. rpcUrl ( ) )
35
+ ) . callStatic . aggregate (
36
+ [
37
+ {
38
+ target : CoreProxy . address ,
39
+ callData : CoreProxy . interface . encodeFunctionData ( 'getVaultCollateral' , [
40
+ 1 ,
41
+ getsUSDCAddress ( network ?. id ) ,
42
+ ] ) ,
43
+ } ,
44
+ {
45
+ target : CoreProxy . address ,
46
+ callData : CoreProxy . interface . encodeFunctionData ( 'getVaultDebt' , [
47
+ 1 ,
48
+ getsUSDCAddress ( network ?. id ) ,
49
+ ] ) ,
50
+ } ,
51
+ ] ,
52
+ { blockTag : block }
53
+ ) ;
54
+ } )
55
+ ) ;
55
56
56
- const decoded = returnValues . map ( ( data ) => {
57
- const [ blockNumber , returnData ] = data ;
57
+ const decoded = returnValues . map ( ( data ) => {
58
+ const [ blockNumber , returnData ] = data ;
58
59
59
- const [ debt ] = CoreProxy . interface . decodeFunctionResult ( 'getVaultDebt' , returnData [ 1 ] ) ;
60
- const [ amount , value ] = CoreProxy . interface . decodeFunctionResult (
61
- 'getVaultCollateral' ,
62
- returnData [ 0 ]
63
- ) ;
60
+ const [ debt ] = CoreProxy . interface . decodeFunctionResult ( 'getVaultDebt' , returnData [ 1 ] ) ;
61
+ const [ amount , value ] = CoreProxy . interface . decodeFunctionResult (
62
+ 'getVaultCollateral' ,
63
+ returnData [ 0 ]
64
+ ) ;
64
65
65
- return {
66
- blockNumber,
67
- amount,
68
- value,
69
- debt,
70
- } ;
71
- } ) ;
66
+ return {
67
+ blockNumber,
68
+ amount,
69
+ value,
70
+ debt,
71
+ } ;
72
+ } ) ;
72
73
73
- const pnls : PnlData [ ] = [ ] ;
74
+ const pnls : PnlData [ ] = [ ] ;
74
75
75
- decoded . forEach ( ( data , i ) => {
76
- if ( i === 0 ) {
77
- return ;
78
- }
76
+ decoded . forEach ( ( data , i ) => {
77
+ if ( i === 0 ) {
78
+ return ;
79
+ }
79
80
80
- const previousDebt = wei ( decoded [ i - 1 ] . debt , 18 , true ) ;
81
- // Take the previous collateral amount
82
- const collateralAmount = wei ( decoded [ i - 1 ] . amount , 18 , true ) ;
83
- const currentDebt = wei ( data . debt , 18 , true ) ;
81
+ const previousDebt = wei ( decoded [ i - 1 ] . debt , 18 , true ) ;
82
+ // Take the previous collateral amount
83
+ const collateralAmount = wei ( decoded [ i - 1 ] . amount , 18 , true ) ;
84
+ const currentDebt = wei ( data . debt , 18 , true ) ;
84
85
85
- const pnlValue = previousDebt . sub ( currentDebt ) ;
86
+ const pnlValue = previousDebt . sub ( currentDebt ) ;
86
87
87
- pnls . push ( { pnlValue, collateralAmount } ) ;
88
- } ) ;
88
+ pnls . push ( { pnlValue, collateralAmount } ) ;
89
+ } ) ;
89
90
90
- return {
91
- pnls,
92
- } ;
91
+ return {
92
+ pnls,
93
+ } ;
94
+ } catch ( error ) {
95
+ console . error ( 'Error fetching pnl' , error ) ;
96
+ return { pnls : [ ] } ;
97
+ }
93
98
} ,
99
+ enabled : Boolean ( block && CoreProxy && Multicall3 && network ) ,
94
100
} ) ;
95
101
} ;
0 commit comments