@@ -4,6 +4,18 @@ import { SubstrateAddress } from '@tangle-network/ui-components/types/address';
44import { useCallback , useMemo } from 'react' ;
55import { catchError , combineLatest , map , of } from 'rxjs' ;
66import { z } from 'zod' ;
7+ import { StorageKey , u64 } from '@polkadot/types' ;
8+ import { toSubstrateAddress } from '@tangle-network/ui-components/utils/toSubstrateAddress' ;
9+ import useNetworkStore from '@tangle-network/tangle-shared-ui/context/useNetworkStore' ;
10+ import { Option } from '@polkadot/types' ;
11+ import {
12+ TanglePrimitivesServicesService ,
13+ TanglePrimitivesServicesServiceServiceBlueprint ,
14+ TanglePrimitivesServicesServiceServiceRequest ,
15+ TanglePrimitivesServicesTypesOperatorProfile ,
16+ } from '@polkadot/types/lookup' ;
17+ import { ITuple } from '@polkadot/types/types' ;
18+ import { AccountId32 } from '@polkadot/types/interfaces' ;
719
820const operatorStatsSchema = z . object ( {
921 registeredBlueprints : z . number ( ) . default ( 0 ) ,
@@ -18,6 +30,8 @@ const operatorStatsSchema = z.object({
1830export const useOperatorStatsData = (
1931 operatorAddress : SubstrateAddress | null | undefined ,
2032) => {
33+ const { network } = useNetworkStore ( ) ;
34+
2135 const { result : operatorStats , ...rest } = useApiRx (
2236 useCallback (
2337 ( apiRx ) => {
@@ -30,14 +44,17 @@ export const useOperatorStatsData = (
3044 ? of ( { } )
3145 : apiRx . query . services ?. operatorsProfile ( operatorAddress ) . pipe (
3246 map ( ( operatorProfile ) => {
33- if ( operatorProfile . isNone ) {
47+ const unwrapped = (
48+ operatorProfile as Option < TanglePrimitivesServicesTypesOperatorProfile >
49+ ) . unwrapOr ( null ) ;
50+
51+ if ( unwrapped === null ) {
3452 return { } ;
3553 }
3654
37- const detailed = operatorProfile . unwrap ( ) ;
3855 return {
39- registeredBlueprints : detailed . blueprints . strings . length ,
40- runningServices : detailed . services . strings . length ,
56+ registeredBlueprints : unwrapped . blueprints . size ,
57+ runningServices : unwrapped . services . size ,
4158 } ;
4259 } ) ,
4360 catchError ( ( error ) => {
@@ -56,18 +73,39 @@ export const useOperatorStatsData = (
5673 map ( ( serviceRequests ) => {
5774 const pendingServices = serviceRequests . filter (
5875 ( [ requestId , serviceRequest ] ) => {
59- if ( serviceRequest . isNone ) {
76+ const unwrapped = (
77+ serviceRequest as Option < TanglePrimitivesServicesServiceServiceRequest >
78+ ) . unwrapOr ( null ) ;
79+
80+ if ( unwrapped === null ) {
6081 return false ;
6182 }
6283
6384 const primitiveServiceRequest = toPrimitiveServiceRequest (
64- requestId ,
65- serviceRequest . unwrap ( ) ,
85+ requestId as StorageKey < [ u64 ] > ,
86+ unwrapped ,
6687 ) ;
6788 return primitiveServiceRequest . operatorsWithApprovalState . some (
68- ( operator ) =>
69- operator . operator === operatorAddress &&
70- operator . approvalStateStatus === 'Pending' ,
89+ ( operator ) => {
90+ const normalizedChainOperator = toSubstrateAddress (
91+ operator . operator ,
92+ network . ss58Prefix ,
93+ ) ;
94+ const normalizedCurrentOperator = operatorAddress
95+ ? toSubstrateAddress (
96+ operatorAddress ,
97+ network . ss58Prefix ,
98+ )
99+ : null ;
100+
101+ const addressMatch =
102+ normalizedChainOperator ===
103+ normalizedCurrentOperator ;
104+ const statusMatch =
105+ operator . approvalStateStatus === 'Pending' ;
106+
107+ return addressMatch && statusMatch ;
108+ } ,
71109 ) ;
72110 } ,
73111 ) ;
@@ -90,13 +128,24 @@ export const useOperatorStatsData = (
90128 : apiRx . query . services ?. blueprints ?. entries ( ) . pipe (
91129 map ( ( blueprints ) => {
92130 const publishedBlueprints = blueprints . filter (
93- ( [ _ , optBlueprint ] ) => {
94- if ( optBlueprint . isNone ) {
131+ ( [ , optBlueprint ] ) => {
132+ const unwrapped = (
133+ optBlueprint as Option <
134+ ITuple <
135+ [
136+ AccountId32 ,
137+ TanglePrimitivesServicesServiceServiceBlueprint ,
138+ ]
139+ >
140+ >
141+ ) . unwrapOr ( null ) ;
142+
143+ if ( unwrapped === null ) {
95144 return false ;
96145 }
97146
98- const blueprint = optBlueprint . unwrap ( ) ;
99- const publisher = blueprint [ 0 ] . toHuman ( ) ;
147+ const owner = unwrapped [ 0 ] ;
148+ const publisher = owner . toHuman ( ) ;
100149 return publisher === operatorAddress ;
101150 } ,
102151 ) ;
@@ -120,11 +169,13 @@ export const useOperatorStatsData = (
120169 : apiRx . query . services ?. instances . entries ( ) . pipe (
121170 map ( ( instances ) => {
122171 const deployedServices = instances . filter ( ( [ _ , instance ] ) => {
123- if ( instance . isNone ) {
172+ const unwrapped = (
173+ instance as Option < TanglePrimitivesServicesService >
174+ ) . unwrapOr ( null ) ;
175+ if ( unwrapped === null ) {
124176 return false ;
125177 }
126- const detailed = instance . unwrap ( ) ;
127- return detailed . owner . toHuman ( ) === operatorAddress ;
178+ return unwrapped . owner . toHuman ( ) === operatorAddress ;
128179 } ) ;
129180 return {
130181 deployedServices : deployedServices . length ,
@@ -162,7 +213,7 @@ export const useOperatorStatsData = (
162213 ) ,
163214 ) ;
164215 } ,
165- [ operatorAddress ] ,
216+ [ operatorAddress , network . ss58Prefix ] ,
166217 ) ,
167218 ) ;
168219
0 commit comments