Skip to content

Commit

Permalink
phase 1 fp name (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs authored Jan 16, 2025
1 parent 2858047 commit cfaa174
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 57 deletions.
13 changes: 5 additions & 8 deletions src/app/api/getFinalityProviders.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { isValidUrl } from "@/utils/url";

import { Pagination } from "../types/api";
import {
FinalityProvider,
FinalityProviderState,
} from "../types/finalityProviders";
import { FinalityProviderV1 } from "../types/finalityProviders";

import { apiWrapper } from "./apiWrapper";

export interface PaginatedFinalityProviders {
finalityProviders: FinalityProvider[];
finalityProviders: FinalityProviderV1[];
pagination: Pagination;
}

Expand All @@ -20,7 +17,7 @@ interface FinalityProvidersAPIResponse {

interface FinalityProviderAPI {
description: DescriptionAPI;
state: FinalityProviderState;
state: "active" | "standby";
commission: string;
btc_pk: string;
active_tvl: number;
Expand Down Expand Up @@ -60,7 +57,7 @@ export const getFinalityProviders = async ({

const response = await apiWrapper(
"GET",
"/v2/finality-providers",
"/v1/finality-providers",
"Error getting finality providers",
{ query: params },
);
Expand All @@ -71,7 +68,7 @@ export const getFinalityProviders = async ({
finalityProvidersAPIResponse.data;

const finalityProviders = finalityProvidersAPI.map(
(fp: FinalityProviderAPI): FinalityProvider => ({
(fp: FinalityProviderAPI): FinalityProviderV1 => ({
description: {
moniker: fp.description.moniker,
identity: fp.description.identity,
Expand Down
99 changes: 99 additions & 0 deletions src/app/api/getFinalityProvidersV2.ts
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 };
};
16 changes: 5 additions & 11 deletions src/app/components/Delegations/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type Delegation as DelegationInterface,
DelegationState,
} from "@/app/types/delegations";
import { Hint } from "@/components/common/Hint";
import { getNetworkConfigBTC } from "@/config/network/btc";
import { satoshiToBtc } from "@/utils/btc";
import { getState, getStateTooltip } from "@/utils/getState";
Expand All @@ -23,7 +24,6 @@ import { trim } from "@/utils/trim";
import { VerificationModal } from "../Modals/VerificationModal";

import { DelegationCell } from "./components/DelegationCell";
import { DelegationStatus } from "./components/DelegationStatus";

interface DelegationProps {
delegation: DelegationInterface;
Expand Down Expand Up @@ -74,7 +74,7 @@ export const Delegation: React.FC<DelegationProps> = ({
resetRegistration: handleCloseRegistration,
} = useDelegationState();
const { registerPhase1Delegation } = useRegistrationService();
const { getFinalityProvider } = useFinalityProviderState();
const { getFinalityProviderName } = useFinalityProviderState();
const { coinName, mempoolApiUrl } = getNetworkConfigBTC();

useEffect(() => {
Expand Down Expand Up @@ -118,10 +118,9 @@ export const Delegation: React.FC<DelegationProps> = ({

<DelegationCell
order="order-4 lg:order-2"
className="pt-6 lg:pt-0 text-right lg:text-left"
className="text-right lg:text-left"
>
{getFinalityProvider(finalityProviderPkHex)?.description?.moniker ??
"-"}
{getFinalityProviderName(finalityProviderPkHex) ?? "-"}
</DelegationCell>

<DelegationCell
Expand Down Expand Up @@ -155,12 +154,7 @@ export const Delegation: React.FC<DelegationProps> = ({
order="order-5"
className="relative flex justify-end lg:justify-start"
>
<DelegationStatus
state={renderState()}
tooltip={renderStateTooltip()}
stakingTxHashHex={stakingTxHashHex}
isOverflow={isOverflow && isActive}
/>
<Hint tooltip={renderStateTooltip()}>{renderState()}</Hint>
</DelegationCell>

<DelegationCell order="order-6">
Expand Down
33 changes: 0 additions & 33 deletions src/app/components/Delegations/components/DelegationStatus.tsx

This file was deleted.

4 changes: 3 additions & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ body.modal-open {

.tooltip-wrap {
max-width: 25rem;
white-space: normal;
white-space: pre-wrap;
word-wrap: break-word;
height: fit-content !important;
z-index: 9999;
}

.tooltip-wrap[data-popper-placement*="top"] .tooltip-arrow {
Expand Down
2 changes: 1 addition & 1 deletion src/app/hooks/client/api/useFinalityProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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";
const FINALITY_PROVIDERS_KEY = "GET_FINALITY_PROVIDERS_V1_KEY";

interface Params {
pk?: string;
Expand Down
67 changes: 67 additions & 0 deletions src/app/hooks/client/api/useFinalityProvidersV2.ts
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;
}
Loading

0 comments on commit cfaa174

Please sign in to comment.