Skip to content

Commit

Permalink
adopt delegation endpoint (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs authored Nov 11, 2024
1 parent 2b96435 commit adf9a49
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/app/api/getDelegationsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { encode } from "url-safe-base64";

import { Pagination } from "../types/api";
import { DelegationV2, DelegationV2State } from "../types/delegationsV2";

import { apiWrapper } from "./apiWrapper";

export interface PaginatedDelegations {
delegations: DelegationV2[];
pagination: Pagination;
}

interface DelegationsAPIResponse {
data: DelegationV2API[];
pagination: Pagination;
}

interface DelegationV2API {
finality_provider_btc_pks_hex: string[];
params_version: string;
staker_btc_pk_hex: string;
staking_amount: string;
staking_time: string;
staking_tx_hash_hex: string;
start_height: number;
end_height: number;
state: DelegationV2State;
unbonding_time: string;
unbonding_tx: string;
}

export const getDelegationsV2 = async (
publicKeyNoCoord: string,
pageKey?: string,
): Promise<PaginatedDelegations> => {
if (!publicKeyNoCoord) {
throw new Error("No public key provided");
}
const params = {
staker_pk_hex: encode(publicKeyNoCoord),
pagination_key: pageKey ? encode(pageKey) : "",
};

const response = await apiWrapper(
"GET",
"/v2/staker/delegations",
"Error getting delegations v2",
params,
);

const delegationsAPIResponse: DelegationsAPIResponse = response.data;

const delegations: DelegationV2[] = delegationsAPIResponse.data.map(
(apiDelegation: DelegationV2API): DelegationV2 => ({
finalityProviderBtcPksHex: apiDelegation.finality_provider_btc_pks_hex,
paramsVersion: apiDelegation.params_version,
stakerBtcPkHex: apiDelegation.staker_btc_pk_hex,
stakingAmount: apiDelegation.staking_amount,
stakingTime: apiDelegation.staking_time,
stakingTxHashHex: apiDelegation.staking_tx_hash_hex,
startHeight: apiDelegation.start_height,
endHeight: apiDelegation.end_height,
state: apiDelegation.state,
unbondingTime: apiDelegation.unbonding_time,
unbondingTx: apiDelegation.unbonding_tx,
}),
);

const pagination: Pagination = {
next_key: delegationsAPIResponse.pagination.next_key,
};
return { delegations: delegations, pagination };
};
62 changes: 62 additions & 0 deletions src/app/hooks/api/useDelegationsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import { useEffect } from "react";

import {
getDelegationsV2,
type PaginatedDelegations,
} from "@/app/api/getDelegationsV2";
import { ONE_MINUTE } from "@/app/constants";
import { useError } from "@/app/context/Error/ErrorContext";
import { useBTCWallet } from "@/app/context/wallet/BTCWalletProvider";
import { ErrorState } from "@/app/types/errors";

export const DELEGATIONS_V2_KEY = "DELEGATIONS_V2";

export function useDelegationsV2({
enabled = true,
}: {
enabled?: boolean;
} = {}) {
const { publicKeyNoCoord } = useBTCWallet();
const { isErrorOpen, handleError, captureError } = useError();

const query = useInfiniteQuery({
queryKey: [DELEGATIONS_V2_KEY, publicKeyNoCoord],
queryFn: ({ pageParam = "" }) =>
getDelegationsV2(pageParam, publicKeyNoCoord),
getNextPageParam: (lastPage) =>
lastPage?.pagination?.next_key !== ""
? lastPage?.pagination?.next_key
: null,
initialPageParam: "",
refetchInterval: ONE_MINUTE,
enabled: Boolean(publicKeyNoCoord) && enabled,
select: (data) => {
const flattenedData = data.pages.reduce<PaginatedDelegations>(
(acc, page) => {
acc.delegations.push(...page.delegations);
acc.pagination = page.pagination;
return acc;
},
{ delegations: [], pagination: { next_key: "" } },
);

return flattenedData;
},
retry: (failureCount, _error) => {
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;
}
22 changes: 22 additions & 0 deletions src/app/types/delegationsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface DelegationV2 {
endHeight: number;
finalityProviderBtcPksHex: string[];
paramsVersion: string;
stakerBtcPkHex: string;
stakingAmount: string;
stakingTime: string;
stakingTxHashHex: string;
startHeight: number;
state: DelegationV2State;
unbondingTime: string;
unbondingTx: string;
}

export type DelegationV2State =
| "PENDING"
| "VERIFIED"
| "ACTIVE"
| "UNBONDING"
| "WITHDRAWABLE"
| "WITHDRAWN"
| "SLASHED";

0 comments on commit adf9a49

Please sign in to comment.