-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2858047
commit cfaa174
Showing
10 changed files
with
233 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { isValidUrl } from "@/utils/url"; | ||
|
||
import { Pagination } from "../types/api"; | ||
import { | ||
FinalityProvider, | ||
FinalityProviderState, | ||
} from "../types/finalityProviders"; | ||
|
||
import { apiWrapper } from "./apiWrapper"; | ||
|
||
export interface PaginatedFinalityProviders { | ||
finalityProviders: FinalityProvider[]; | ||
pagination: Pagination; | ||
} | ||
|
||
interface FinalityProvidersAPIResponse { | ||
data: FinalityProviderAPI[]; | ||
pagination: Pagination; | ||
} | ||
|
||
interface FinalityProviderAPI { | ||
description: DescriptionAPI; | ||
state: FinalityProviderState; | ||
commission: string; | ||
btc_pk: string; | ||
active_tvl: number; | ||
total_tvl: number; | ||
active_delegations: number; | ||
total_delegations: number; | ||
} | ||
|
||
interface DescriptionAPI { | ||
moniker: string; | ||
identity: string; | ||
website: string; | ||
security_contact: string; | ||
details: string; | ||
} | ||
|
||
export const getFinalityProvidersV2 = async ({ | ||
key, | ||
pk, | ||
sortBy, | ||
order, | ||
name, | ||
}: { | ||
key: string; | ||
name?: string; | ||
sortBy?: string; | ||
order?: "asc" | "desc"; | ||
pk?: string; | ||
}): Promise<PaginatedFinalityProviders> => { | ||
const params = { | ||
pagination_key: key, | ||
finality_provider_pk: pk, | ||
sort_by: sortBy, | ||
order, | ||
name, | ||
}; | ||
|
||
const response = await apiWrapper( | ||
"GET", | ||
"/v2/finality-providers", | ||
"Error getting finality providers", | ||
{ query: params }, | ||
); | ||
|
||
const finalityProvidersAPIResponse: FinalityProvidersAPIResponse = | ||
response.data; | ||
const finalityProvidersAPI: FinalityProviderAPI[] = | ||
finalityProvidersAPIResponse.data; | ||
|
||
const finalityProviders = finalityProvidersAPI.map( | ||
(fp: FinalityProviderAPI): FinalityProvider => ({ | ||
description: { | ||
moniker: fp.description.moniker, | ||
identity: fp.description.identity, | ||
website: isValidUrl(fp.description.website) | ||
? fp.description.website | ||
: "", | ||
securityContact: fp.description.security_contact, | ||
details: fp.description.details, | ||
}, | ||
state: fp.state, | ||
commission: fp.commission, | ||
btcPk: fp.btc_pk, | ||
activeTVLSat: fp.active_tvl, | ||
totalTVLSat: fp.total_tvl, | ||
activeDelegations: fp.active_delegations, | ||
totalDelegations: fp.total_delegations, | ||
}), | ||
); | ||
|
||
const pagination: Pagination = { | ||
next_key: finalityProvidersAPIResponse.pagination.next_key, | ||
}; | ||
|
||
return { finalityProviders, pagination }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/app/components/Delegations/components/DelegationStatus.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useInfiniteQuery } from "@tanstack/react-query"; | ||
import { useEffect } from "react"; | ||
|
||
import { | ||
type PaginatedFinalityProviders, | ||
getFinalityProvidersV2, | ||
} from "@/app/api/getFinalityProvidersV2"; | ||
import { ONE_MINUTE } from "@/app/constants"; | ||
import { useError } from "@/app/context/Error/ErrorContext"; | ||
import { ErrorState } from "@/app/types/errors"; | ||
|
||
const FINALITY_PROVIDERS_KEY = "GET_FINALITY_PROVIDERS_V2_KEY"; | ||
|
||
interface Params { | ||
pk?: string; | ||
name?: string; | ||
sortBy?: string; | ||
order?: "asc" | "desc"; | ||
} | ||
|
||
export function useFinalityProvidersV2({ | ||
pk, | ||
sortBy, | ||
order, | ||
name, | ||
}: Params = {}) { | ||
const { isErrorOpen, handleError, captureError } = useError(); | ||
|
||
const query = useInfiniteQuery({ | ||
queryKey: [FINALITY_PROVIDERS_KEY], | ||
queryFn: ({ pageParam = "" }) => | ||
getFinalityProvidersV2({ key: pageParam, pk, sortBy, order, name }), | ||
getNextPageParam: (lastPage) => | ||
lastPage?.pagination?.next_key !== "" | ||
? lastPage?.pagination?.next_key | ||
: null, | ||
initialPageParam: "", | ||
refetchInterval: ONE_MINUTE, | ||
placeholderData: (prev) => prev, | ||
select: (data) => { | ||
const flattenedData = data.pages.reduce<PaginatedFinalityProviders>( | ||
(acc, page) => { | ||
acc.finalityProviders.push(...page.finalityProviders); | ||
acc.pagination = page.pagination; | ||
return acc; | ||
}, | ||
{ finalityProviders: [], pagination: { next_key: "" } }, | ||
); | ||
return flattenedData; | ||
}, | ||
retry: (failureCount) => { | ||
return !isErrorOpen && failureCount <= 3; | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
handleError({ | ||
error: query.error, | ||
hasError: query.isError, | ||
errorState: ErrorState.SERVER_ERROR, | ||
refetchFunction: query.refetch, | ||
}); | ||
captureError(query.error); | ||
}, [query.isError, query.error, query.refetch, handleError, captureError]); | ||
|
||
return query; | ||
} |
Oops, something went wrong.