Skip to content

Commit

Permalink
297 v2 adopt params endpoint (#303)
Browse files Browse the repository at this point in the history
* adopt params endpoint

* resolve comments

* resolve comments

* resolve comments
  • Loading branch information
jeremy-babylonlabs authored Nov 8, 2024
1 parent 643eece commit 98859da
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 10 deletions.
79 changes: 79 additions & 0 deletions src/app/api/getParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { getPublicKeyNoCoord } from "@babylonlabs-io/btc-staking-ts";
import { AxiosResponse } from "axios";

import { Params } from "../types/params";

import { apiWrapper } from "./apiWrapper";

export interface ParamsDataResponse {
data: ParamsAPI;
}

export interface ParamsAPI {
bbn: BbnParams[];
}

export interface BbnParams {
version: number;
covenant_pks: string[];
covenant_quorum: number;
min_staking_value_sat: number;
max_staking_value_sat: number;
min_staking_time_blocks: number;
max_staking_time_blocks: number;
slashing_pk_script: string;
min_slashing_tx_fee_sat: number;
slashing_rate: string;
min_unbonding_time_blocks: number;
unbonding_fee_sat: number;
min_commission_rate: string;
max_active_finality_providers: number;
delegation_creation_base_gas_fee: number;
}

export const getParams = async (): Promise<Params> => {
const { data } = (await apiWrapper(
"GET",
"/v2/global-params",
"Error getting params",
)) as AxiosResponse<ParamsDataResponse>;

const params = data.data;

const versions = params.bbn
.sort((a, b) => a.version - b.version) // Sort by version ascending
.map((v) => ({
version: v.version,
covenantNoCoordPks: v.covenant_pks.map((pk) =>
String(getPublicKeyNoCoord(pk)),
),
covenantQuorum: v.covenant_quorum,
minStakingValueSat: v.min_staking_value_sat,
maxStakingValueSat: v.max_staking_value_sat,
minStakingTimeBlocks: v.min_staking_time_blocks,
maxStakingTimeBlocks: v.max_staking_time_blocks,
unbondingTime: v.min_unbonding_time_blocks,
unbondingFeeSat: v.unbonding_fee_sat,
minCommissionRate: v.min_commission_rate,
maxActiveFinalityProviders: v.max_active_finality_providers,
delegationCreationBaseGasFee: v.delegation_creation_base_gas_fee,
slashing: {
slashingPkScriptHex: v.slashing_pk_script,
slashingRate: parseFloat(v.slashing_rate),
minSlashingTxFeeSat: v.min_slashing_tx_fee_sat,
},
maxStakingAmountSat: v.max_staking_value_sat,
minStakingAmountSat: v.min_staking_value_sat,
}));

const latestVersion = versions.reduce((prev, current) =>
current.version > prev.version ? current : prev,
);

return {
bbnStakingParams: {
latestVersion,
versions,
},
};
};
20 changes: 12 additions & 8 deletions src/app/api/getPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ export const getStakersPoints = async (
export const getDelegationPointsByStakingTxHashHexes = async (
stakingTxHashHexes: string[],
): Promise<DelegationPointsAPI[]> => {
const response = await pointsApiWrapper(
"POST",
"/v1/points/delegations",
"Error getting delegation points by staking transaction hashes",
{ staking_tx_hash_hex: stakingTxHashHexes },
);
try {
const response = await pointsApiWrapper(
"POST",
"/v1/points/delegations",
"Error getting delegation points by staking transaction hashes",
{ staking_tx_hash_hex: stakingTxHashHexes },
);

const responseData: PaginatedDelegationsPointsAPIResponse = response.data;
return responseData.data;
const responseData: PaginatedDelegationsPointsAPIResponse = response.data;
return responseData.data;
} catch (error) {
throw error;
}
};
15 changes: 15 additions & 0 deletions src/app/hooks/api/useParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getParams } from "@/app/api/getParams";
import { useAPIQuery } from "@/app/hooks/api/useApi";
import { Params } from "@/app/types/params";

export const PARAMS_KEY = "PARAMS";

export function useParams({ enabled = true }: { enabled?: boolean } = {}) {
const data = useAPIQuery<Params>({
queryKey: [PARAMS_KEY],
queryFn: getParams,
enabled,
});

return data;
}
29 changes: 27 additions & 2 deletions src/app/state/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useMemo, useState, type PropsWithChildren } from "react";

import { useBTCTipHeight } from "@/app/hooks/api/useBTCTipHeight";
import { useOrdinals } from "@/app/hooks/api/useOrdinals";
import { useParams } from "@/app/hooks/api/useParams";
import { useUTXOs } from "@/app/hooks/api/useUTXOs";
import { useVersions } from "@/app/hooks/api/useVersions";
import { GlobalParamsVersion } from "@/app/types/globalParams";
Expand All @@ -13,6 +14,8 @@ import type {
UTXO,
} from "@/utils/wallet/btc_wallet_provider";

import { BbnStakingParamsVersion } from "../types/params";

import { DelegationState } from "./DelegationState";

const STATE_LIST = [DelegationState];
Expand All @@ -22,6 +25,8 @@ export interface AppState {
totalBalance: number;
nextVersion?: GlobalParamsVersion;
currentVersion?: GlobalParamsVersion;
latestBbnStakingParamsVersion?: BbnStakingParamsVersion;
bbnStakingParamsVersions?: BbnStakingParamsVersion[];
currentHeight?: number;
isApprochingNextVersion: boolean;
firstActivationHeight: number;
Expand Down Expand Up @@ -72,6 +77,13 @@ export function AppState({ children }: PropsWithChildren) {
isLoading: isVersionLoading,
isError: isVersionError,
} = useVersions();

const {
data: params,
isLoading: isParamsLoading,
isError: isParamsError,
} = useParams();

const {
data: height,
isLoading: isHeightLoading,
Expand All @@ -80,9 +92,17 @@ export function AppState({ children }: PropsWithChildren) {

// Computed
const isLoading =
isVersionLoading || isHeightLoading || isUTXOLoading || isOrdinalLoading;
isVersionLoading ||
isHeightLoading ||
isUTXOLoading ||
isOrdinalLoading ||
isParamsLoading;
const isError =
isHeightError || isVersionError || isUTXOError || isOrdinalError;
isHeightError ||
isVersionError ||
isUTXOError ||
isOrdinalError ||
isParamsError;

const ordinalMap: Record<string, InscriptionIdentifier> = useMemo(
() =>
Expand Down Expand Up @@ -115,6 +135,8 @@ export function AppState({ children }: PropsWithChildren) {
[versions, height],
);

const bbnStakingParams = useMemo(() => params?.bbnStakingParams, [params]);

// Handlers
const includeOrdinals = useCallback(() => setOrdinalsExcluded(false), []);
const excludeOrdinals = useCallback(() => setOrdinalsExcluded(true), []);
Expand All @@ -126,6 +148,8 @@ export function AppState({ children }: PropsWithChildren) {
currentHeight: height,
totalBalance,
...versionInfo,
latestBbnStakingParamsVersion: bbnStakingParams?.latestVersion,
bbnStakingParamsVersions: bbnStakingParams?.versions,
isError,
isLoading,
ordinalsExcluded,
Expand All @@ -137,6 +161,7 @@ export function AppState({ children }: PropsWithChildren) {
height,
totalBalance,
versionInfo,
bbnStakingParams,
isError,
isLoading,
ordinalsExcluded,
Expand Down
17 changes: 17 additions & 0 deletions src/app/types/params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StakingParams } from "@babylonlabs-io/btc-staking-ts";

export interface BbnStakingParamsVersion extends StakingParams {
version: number;
minCommissionRate: string;
maxActiveFinalityProviders: number;
delegationCreationBaseGasFee: number;
}

export interface BbnStakingParams {
latestVersion: BbnStakingParamsVersion;
versions: BbnStakingParamsVersion[];
}

export interface Params {
bbnStakingParams: BbnStakingParams;
}

0 comments on commit 98859da

Please sign in to comment.