-
Notifications
You must be signed in to change notification settings - Fork 15
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
f110d27
commit eae3417
Showing
16 changed files
with
452 additions
and
205 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 { | ||
FinalityProviderState, | ||
FinalityProviderV2, | ||
} from "../types/finalityProvidersV2"; | ||
|
||
import { apiWrapper } from "./apiWrapper"; | ||
|
||
export interface PaginatedFinalityProviders { | ||
finalityProviders: FinalityProviderV2[]; | ||
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): FinalityProviderV2 => ({ | ||
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
4 changes: 2 additions & 2 deletions
4
src/app/components/Staking/FinalityProviders/FinalityProviderFilter.tsx
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
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.