@@ -13,26 +13,37 @@ if (!rpcNodeURL) {
1313
1414// Check for AbortController support
1515const isAbortControllerSupported =
16- typeof window !== "undefined" && window . hasOwnProperty ( "AbortController" ) ;
17- const noOp = ( ) => null ;
16+ typeof window !== "undefined" && "AbortController" in window ;
17+ const noOp = ( ) : void => undefined ;
1818
1919// Initializes an AbortController if supported
20- const initAbortController = ( ) =>
20+ const initAbortController = ( ) : {
21+ abort : ( ) => void ;
22+ signal : AbortSignal | undefined ;
23+ } =>
2124 isAbortControllerSupported
2225 ? new AbortController ( )
23- : { abort : noOp , signal : { } } ;
26+ : { abort : noOp , signal : undefined } ;
27+
28+ type VoteAccountsResult = {
29+ result : { current : Array < { activatedStake : string } > } ;
30+ } ;
31+ type PerformanceSamplesResult = {
32+ result : Array < { numTransactions : number ; samplePeriodSecs : number } > ;
33+ } ;
34+ type TransactionCountResult = { result : number } ;
2435
2536/**
2637 * Fetches the superminority count.
2738 *
28- * @returns {Promise<number> } The count of superminority validators.
39+ * @returns {Promise<number | null > } The count of superminority validators.
2940 */
30- export const fetchSuperminority = async ( ) => {
41+ export const fetchSuperminority = async ( ) : Promise < number | null > => {
3142 try {
32- const voteAccounts = await makeRPCCall ( {
43+ const voteAccounts = ( await makeRPCCall ( {
3344 method : "getVoteAccounts" ,
34- rpcNodeURL,
35- } ) ;
45+ rpcNodeURL : rpcNodeURL ! ,
46+ } ) ) as VoteAccountsResult ;
3647
3748 // Sort validators by stake in ascending order
3849 const sortedValidators = voteAccounts . result . current . sort ( ( a , b ) => {
@@ -75,35 +86,51 @@ export const fetchSuperminority = async () => {
7586 * @param {number } sampleHistoryHours How many hours (60min.) the query should go back.
7687 * @param {boolean } getLiveTransactionCount
7788 * @param {boolean } getCurrentValidatorNodes
78- * @returns {{availableStats: boolean, avgTps: number, validators: number, totalTransactionCount: number, superminority: number} }
89+ * @returns {{availableStats: boolean, avgTps: number, validators: number, totalTransactionCount: number, superminority: number | null } }
7990 */
8091export const useTransactionStats = ( {
8192 visible,
8293 performanceUpdateSeconds,
8394 sampleHistoryHours,
8495 getLiveTransactionCount = true ,
8596 getCurrentValidatorNodes = true ,
86- } ) => {
97+ } : {
98+ visible : boolean ;
99+ performanceUpdateSeconds : number ;
100+ sampleHistoryHours : number ;
101+ getLiveTransactionCount ?: boolean ;
102+ getCurrentValidatorNodes ?: boolean ;
103+ } ) : {
104+ availableStats : boolean ;
105+ avgTps : number ;
106+ totalTransactionCount : number ;
107+ validators : number ;
108+ superminority : number | null ;
109+ } => {
87110 const [ availableStats , setAvailableStats ] = useState ( false ) ;
88111 const [ avgTps , setAvgTps ] = useState ( 0 ) ;
89112 const [ totalTransactionCount , setTotalTransactionCount ] = useState ( 0 ) ;
90113 const [ validators , setValidators ] = useState ( 0 ) ;
91- const [ superminority , setSuperminority ] = useState ( null ) ;
114+ const [ superminority , setSuperminority ] = useState < number | null > ( null ) ;
92115
93116 const getRPCData = useCallback (
94- async ( getValidatorNodes , getTransactionCount , abortSignal ) => {
117+ async (
118+ getValidatorNodes : boolean ,
119+ getTransactionCount : boolean ,
120+ abortSignal : AbortSignal | undefined ,
121+ ) : Promise < boolean > => {
95122 try {
96123 if ( rpcNodeURL ) {
97124 await Promise . all ( [
98125 ( async ( ) => {
99- const recentPerformanceSamples = await makeRPCCall ( {
126+ const recentPerformanceSamples = ( await makeRPCCall ( {
100127 abortSignal,
101128 method : "getRecentPerformanceSamples" ,
102129 params : [ 60 * sampleHistoryHours ] ,
103130 rpcNodeURL,
104- } ) ;
131+ } ) ) as PerformanceSamplesResult ;
105132 // Calculate average transactions per second
106- const short = recentPerformanceSamples . result . reduce (
133+ const short = recentPerformanceSamples . result . reduce < number [ ] > (
107134 ( shortResults , sample ) => {
108135 if ( sample . numTransactions !== 0 ) {
109136 shortResults . push (
@@ -122,23 +149,23 @@ export const useTransactionStats = ({
122149 if ( ! getValidatorNodes ) {
123150 return ;
124151 }
125- const voteAccounts = await makeRPCCall ( {
152+ const voteAccounts = ( await makeRPCCall ( {
126153 abortSignal,
127154 method : "getVoteAccounts" ,
128155 rpcNodeURL,
129- } ) ;
156+ } ) ) as VoteAccountsResult ;
130157 setValidators ( voteAccounts . result . current . length ) ;
131158 setAvailableStats ( true ) ;
132159 } ) ( ) ,
133160 ( async ( ) => {
134161 if ( ! getTransactionCount ) {
135162 return ;
136163 }
137- const transactionCount = await makeRPCCall ( {
164+ const transactionCount = ( await makeRPCCall ( {
138165 abortSignal,
139166 method : "getTransactionCount" ,
140167 rpcNodeURL,
141- } ) ;
168+ } ) ) as TransactionCountResult ;
142169 setTotalTransactionCount ( transactionCount . result ) ;
143170 setAvailableStats ( true ) ;
144171 } ) ( ) ,
@@ -148,7 +175,10 @@ export const useTransactionStats = ({
148175 }
149176 return false ;
150177 } catch ( error ) {
151- if ( error . name === "AbortError" || error . name === "TypeError" ) {
178+ if (
179+ error instanceof Error &&
180+ ( error . name === "AbortError" || error . name === "TypeError" )
181+ ) {
152182 return false ;
153183 }
154184 console . error ( "Error fetching RPC data:" , error ) ;
0 commit comments