Skip to content

Commit

Permalink
feat: use network info instead of params to make it more generic (#397)
Browse files Browse the repository at this point in the history
* feat: use network info instead of params to make it more generic
  • Loading branch information
jrwbabylonlab authored Nov 28, 2024
1 parent f119259 commit 6771bca
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 85 deletions.
50 changes: 31 additions & 19 deletions src/app/api/getParams.ts → src/app/api/getNetworkInfo.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { getPublicKeyNoCoord } from "@babylonlabs-io/btc-staking-ts";
import { AxiosResponse } from "axios";

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

import { apiWrapper } from "./apiWrapper";

export interface ParamsDataResponse {
data: ParamsAPI;
interface NetworkInfoDataResponse {
data: NetworkInfoAPI;
}

export interface BtcCheckpointParams {
version: number;
btc_confirmation_depth: number;
}

export interface ParamsAPI {
bbn: BbnParams[];
btc: BtcCheckpointParams[];
interface StakingStatus {
is_staking_open: boolean;
}

interface NetworkInfoAPI {
staking_status: StakingStatus;
params: {
bbn: BbnParams[];
btc: BtcCheckpointParams[];
};
}

export interface BbnParams {
Expand All @@ -37,14 +44,14 @@ export interface BbnParams {
delegation_creation_base_gas_fee: number;
}

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

const params = data.data;
const { params, staking_status } = data.data;

const stakingVersions = params.bbn
.sort((a, b) => a.version - b.version) // Sort by version ascending
Expand Down Expand Up @@ -104,15 +111,20 @@ export const getParams = async (): Promise<Params> => {
}

return {
bbnStakingParams: {
latestParam: latestStakingParam,
versions: stakingVersions,
genesisParam: genesisStakingParam,
stakingStatus: {
isStakingOpen: staking_status.is_staking_open,
},
btcEpochCheckParams: {
latestParam: latestEpochCheckParam,
versions: epochCheckVersions,
genesisParam: genesisEpochCheckParam,
params: {
bbnStakingParams: {
latestParam: latestStakingParam,
versions: stakingVersions,
genesisParam: genesisStakingParam,
},
btcEpochCheckParams: {
latestParam: latestEpochCheckParam,
versions: epochCheckVersions,
genesisParam: genesisEpochCheckParam,
},
},
};
};
7 changes: 4 additions & 3 deletions src/app/components/FAQ/FAQ.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Heading } from "@babylonlabs-io/bbn-core-ui";

import { useParams } from "@/app/hooks/api/useParams";
import { useNetworkInfo } from "@/app/hooks/api/useNetworkInfo";
import { getNetworkConfig } from "@/config/network.config";

import { questions } from "./data/questions";
Expand All @@ -9,9 +9,10 @@ interface FAQProps {}

export const FAQ: React.FC<FAQProps> = () => {
const { coinName, networkName } = getNetworkConfig();
const { data: params } = useParams();
const { data: networkInfo } = useNetworkInfo();
const confirmationDepth =
params?.btcEpochCheckParams?.latestParam?.btcConfirmationDepth || 10;
networkInfo?.params.btcEpochCheckParams?.latestParam
?.btcConfirmationDepth || 10;

return (
<div className="container mx-auto flex flex-col gap-2 p-6">
Expand Down
7 changes: 4 additions & 3 deletions src/app/components/Modals/PreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Text,
} from "@babylonlabs-io/bbn-core-ui";

import { useParams } from "@/app/hooks/api/useParams";
import { useNetworkInfo } from "@/app/hooks/api/useNetworkInfo";
import { useIsMobileView } from "@/app/hooks/useBreakpoint";
import { getNetworkConfig } from "@/config/network.config";
import { blocksToDisplayTime } from "@/utils/blocksToDisplayTime";
Expand Down Expand Up @@ -48,9 +48,10 @@ export const PreviewModal = ({
const isMobileView = useIsMobileView();
const { coinName } = getNetworkConfig();

const { data: params } = useParams();
const { data: networkInfo } = useNetworkInfo();
const confirmationDepth =
params?.btcEpochCheckParams?.latestParam?.btcConfirmationDepth || 10;
networkInfo?.params.btcEpochCheckParams?.latestParam
?.btcConfirmationDepth || 10;

const DialogComponent = isMobileView ? MobileDialog : Dialog;

Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export const Staking = () => {
useLocalStorage<boolean>("bbn-staking-cancelFeedbackModalOpened ", false);

const { createDelegationEoi, estimateStakingFee } = useTransactionService();
const { params } = useAppState();
const latestParam = params?.bbnStakingParams?.latestParam;
const { networkInfo } = useAppState();
const latestParam = networkInfo?.params.bbnStakingParams?.latestParam;

const [pendingVerificationOpen, setPendingVerificationOpen] = useState(false);
const [stakingTxHashHex, setStakingTxHashHex] = useState<
Expand Down
7 changes: 4 additions & 3 deletions src/app/components/Summary/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FaBitcoin } from "react-icons/fa";
import { Tooltip } from "react-tooltip";

import { useBTCWallet } from "@/app/context/wallet/BTCWalletProvider";
import { useParams } from "@/app/hooks/api/useParams";
import { useNetworkInfo } from "@/app/hooks/api/useNetworkInfo";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { useAppState } from "@/app/state";
import { useDelegationState } from "@/app/state/DelegationState";
Expand All @@ -24,9 +24,10 @@ export const Summary = () => {

const { coinName } = getNetworkConfig();
const onMainnet = getNetworkConfig().network === Network.MAINNET;
const { data: params } = useParams();
const { data: networkInfo } = useNetworkInfo();
const confirmationDepth =
params?.btcEpochCheckParams?.latestParam?.btcConfirmationDepth || 10;
networkInfo?.params.btcEpochCheckParams?.latestParam
?.btcConfirmationDepth || 10;

if (!address) return;

Expand Down
13 changes: 13 additions & 0 deletions src/app/hooks/api/useNetworkInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getNetworkInfo } from "@/app/api/getNetworkInfo";
import { useAPIQuery } from "@/app/hooks/api/useApi";
import { NetworkInfo } from "@/app/types/networkInfo";

export const NETWORK_INFO_KEY = "NETWORK_INFO";

export function useNetworkInfo({ enabled = true }: { enabled?: boolean } = {}) {
return useAPIQuery<NetworkInfo>({
queryKey: [NETWORK_INFO_KEY],
queryFn: getNetworkInfo,
enabled,
});
}
13 changes: 0 additions & 13 deletions src/app/hooks/api/useParams.ts

This file was deleted.

41 changes: 25 additions & 16 deletions src/app/hooks/services/useTransactionService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { EOIStepStatus } from "@/app/components/Modals/EOIModal";
import { useBTCWallet } from "@/app/context/wallet/BTCWalletProvider";
import { useCosmosWallet } from "@/app/context/wallet/CosmosWalletProvider";
import { useAppState } from "@/app/state";
import { BbnStakingParamsVersion, Params } from "@/app/types/params";
import { BbnStakingParamsVersion, Params } from "@/app/types/networkInfo";
import {
clearTxSignatures,
extractSchnorrSignaturesFromTransaction,
Expand Down Expand Up @@ -51,7 +51,7 @@ export enum SigningStep {
}

export const useTransactionService = () => {
const { availableUTXOs: inputUTXOs, params } = useAppState();
const { availableUTXOs: inputUTXOs, networkInfo } = useAppState();
const { data: networkFees } = useNetworkFees();
const { defaultFeeRate } = getFeeRateFromMempool(networkFees);

Expand All @@ -70,8 +70,8 @@ export const useTransactionService = () => {
pushTx,
} = useBTCWallet();

const latestParam = params?.bbnStakingParams?.latestParam;
const genesisParam = params?.bbnStakingParams?.genesisParam;
const latestParam = networkInfo?.params.bbnStakingParams?.latestParam;
const genesisParam = networkInfo?.params.bbnStakingParams?.genesisParam;

/**
* Create the delegation EOI
Expand Down Expand Up @@ -287,15 +287,15 @@ export const useTransactionService = () => {
stakingTxHex: string,
) => {
// Perform checks
if (!params?.bbnStakingParams.versions.length) {
if (!networkInfo?.params.bbnStakingParams.versions.length) {
throw new Error("Staking parameters not loaded");
}
if (!btcConnected || !btcNetwork)
throw new Error("BTC Wallet not connected");

validateStakingInput(stakingInput);

const p = getParamByVersion(params, paramVersion);
const p = getParamByVersion(networkInfo.params, paramVersion);
if (!p) throw new Error("Staking params not loaded");

const staking = new Staking(
Expand Down Expand Up @@ -327,7 +327,7 @@ export const useTransactionService = () => {
[
btcConnected,
btcNetwork,
params,
networkInfo,
address,
publicKeyNoCoord,
inputUTXOs,
Expand All @@ -348,7 +348,10 @@ export const useTransactionService = () => {
}[],
) => {
// Perform checks
if (!params || params.bbnStakingParams.versions.length === 0) {
if (
!networkInfo ||
networkInfo.params.bbnStakingParams.versions.length === 0
) {
throw new Error("Params not loaded");
}
if (!btcConnected || !btcNetwork)
Expand All @@ -358,7 +361,7 @@ export const useTransactionService = () => {

const stakingTx = await Transaction.fromHex(stakingTxHex);

const p = getParamByVersion(params, paramVersion);
const p = getParamByVersion(networkInfo.params, paramVersion);
if (!p) throw new Error("Staking params not loaded");

const staking = new Staking(
Expand Down Expand Up @@ -405,7 +408,7 @@ export const useTransactionService = () => {
console.log("unbonding tx id", signedUnbondingTx.getId());
},
[
params,
networkInfo,
btcConnected,
btcNetwork,
address,
Expand All @@ -429,15 +432,18 @@ export const useTransactionService = () => {
unbondingTxHex: string,
) => {
// Perform checks
if (!params || params.bbnStakingParams.versions.length === 0) {
if (
!networkInfo ||
networkInfo.params.bbnStakingParams.versions.length === 0
) {
throw new Error("Params not loaded");
}
if (!btcConnected || !btcNetwork)
throw new Error("BTC Wallet not connected");

validateStakingInput(stakingInput);

const p = getParamByVersion(params, paramVersion);
const p = getParamByVersion(networkInfo.params, paramVersion);
if (!p) throw new Error("Staking params not loaded");

const staking = new Staking(
Expand Down Expand Up @@ -468,7 +474,7 @@ export const useTransactionService = () => {
);
},
[
params,
networkInfo,
btcConnected,
btcNetwork,
address,
Expand All @@ -493,15 +499,18 @@ export const useTransactionService = () => {
stakingTxHex: string,
) => {
// Perform checks
if (!params || params.bbnStakingParams.versions.length === 0) {
if (
!networkInfo ||
networkInfo.params.bbnStakingParams.versions.length === 0
) {
throw new Error("Params not loaded");
}
if (!btcConnected || !btcNetwork)
throw new Error("BTC Wallet not connected");

validateStakingInput(stakingInput);

const p = getParamByVersion(params, paramVersion);
const p = getParamByVersion(networkInfo.params, paramVersion);
if (!p) throw new Error("Staking params not loaded");

const staking = new Staking(
Expand Down Expand Up @@ -531,7 +540,7 @@ export const useTransactionService = () => {
);
},
[
params,
networkInfo,
btcConnected,
btcNetwork,
address,
Expand Down
Loading

0 comments on commit 6771bca

Please sign in to comment.