Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 13c1ef7

Browse files
feat: pop upgrade (#1303)
* feat: pop upgrade
1 parent fe4afe6 commit 13c1ef7

File tree

7 files changed

+82
-14
lines changed

7 files changed

+82
-14
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"dependencies": {
3232
"@babylonlabs-io/babylon-proto-ts": "1.7.1",
33-
"@babylonlabs-io/btc-staking-ts": "2.3.4",
33+
"@babylonlabs-io/btc-staking-ts": "2.4.1",
3434
"@babylonlabs-io/core-ui": "1.10.1",
3535
"@babylonlabs-io/wallet-connector": "1.10.0",
3636
"@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",

src/ui/common/api/getNetworkInfo.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getPublicKeyNoCoord } from "@babylonlabs-io/btc-staking-ts";
22

33
import { ClientError, ERROR_CODES } from "@/ui/common/errors";
44

5-
import { NetworkInfo } from "../types/networkInfo";
5+
import { NetworkInfo, NetworkUpgradeConfig } from "../types/networkInfo";
66

77
import { apiWrapper } from "./apiWrapper";
88

@@ -25,6 +25,7 @@ interface NetworkInfoAPI {
2525
bbn: BbnParams[];
2626
btc: BtcCheckpointParams[];
2727
};
28+
network_upgrade?: NetworkUpgradeConfig;
2829
}
2930

3031
export interface BbnParams {
@@ -55,7 +56,7 @@ export const getNetworkInfo = async (): Promise<NetworkInfo> => {
5556
"Error getting network info",
5657
);
5758
const { data } = response;
58-
const { params, staking_status } = data.data;
59+
const { params, staking_status, network_upgrade } = data.data;
5960

6061
const stakingVersions = (params.bbn || [])
6162
.sort((a, b) => a.version - b.version)
@@ -159,5 +160,6 @@ export const getNetworkInfo = async (): Promise<NetworkInfo> => {
159160
genesisParam: genesisEpochCheckParam,
160161
},
161162
},
163+
networkUpgrade: network_upgrade,
162164
};
163165
};

src/ui/common/context/rpc/BbnRpcProvider.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,33 @@ import { ClientError, ERROR_CODES } from "@/ui/common/errors";
1313

1414
interface BbnRpcContextType {
1515
queryClient: QueryClient | undefined;
16+
tmClient: Tendermint34Client | undefined;
1617
isLoading: boolean;
1718
error: Error | null;
1819
reconnect: () => Promise<void>;
1920
}
2021

2122
const BbnRpcContext = createContext<BbnRpcContextType>({
2223
queryClient: undefined,
24+
tmClient: undefined,
2325
isLoading: true,
2426
error: null,
2527
reconnect: async () => {},
2628
});
2729

2830
export function BbnRpcProvider({ children }: { children: React.ReactNode }) {
2931
const [queryClient, setQueryClient] = useState<QueryClient>();
32+
const [tmClient, setTmClient] = useState<Tendermint34Client>();
3033
const [isLoading, setIsLoading] = useState(true);
3134
const [error, setError] = useState<Error | null>(null);
3235
const { rpc } = getNetworkConfigBBN();
3336

3437
const connect = useCallback(async () => {
3538
try {
36-
const tmClient = await Tendermint34Client.connect(rpc);
37-
const client = QueryClient.withExtensions(tmClient);
39+
const tmClientInstance = await Tendermint34Client.connect(rpc);
40+
const client = QueryClient.withExtensions(tmClientInstance);
3841
setQueryClient(client);
42+
setTmClient(tmClientInstance);
3943
setIsLoading(false);
4044
setError(null);
4145
} catch (err) {
@@ -73,7 +77,7 @@ export function BbnRpcProvider({ children }: { children: React.ReactNode }) {
7377

7478
return (
7579
<BbnRpcContext.Provider
76-
value={{ queryClient, isLoading, error, reconnect }}
80+
value={{ queryClient, tmClient, isLoading, error, reconnect }}
7781
>
7882
{children}
7983
</BbnRpcContext.Provider>

src/ui/common/hooks/client/rpc/queries/useBbnQuery.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
setupBankExtension,
99
} from "@cosmjs/stargate";
1010

11-
import { ONE_MINUTE } from "@/ui/common/constants";
11+
import { ONE_MINUTE, ONE_SECOND } from "@/ui/common/constants";
1212
import { useBbnRpc } from "@/ui/common/context/rpc/BbnRpcProvider";
1313
import { useCosmosWallet } from "@/ui/common/context/wallet/CosmosWalletProvider";
1414
import { ClientError } from "@/ui/common/errors";
@@ -21,6 +21,7 @@ import { useRpcErrorHandler } from "../useRpcErrorHandler";
2121
const BBN_BTCLIGHTCLIENT_TIP_KEY = "BBN_BTCLIGHTCLIENT_TIP";
2222
const BBN_BALANCE_KEY = "BBN_BALANCE";
2323
const BBN_REWARDS_KEY = "BBN_REWARDS";
24+
const BBN_HEIGHT_KEY = "BBN_HEIGHT";
2425
const REWARD_GAUGE_KEY_BTC_DELEGATION = "BTC_STAKER";
2526

2627
/**
@@ -30,7 +31,7 @@ const REWARD_GAUGE_KEY_BTC_DELEGATION = "BTC_STAKER";
3031
export const useBbnQuery = () => {
3132
const { isGeoBlocked, isLoading: isHealthcheckLoading } = useHealthCheck();
3233
const { bech32Address, connected } = useCosmosWallet();
33-
const { queryClient } = useBbnRpc();
34+
const { queryClient, tmClient } = useBbnRpc();
3435
const { hasRpcError, reconnect } = useRpcErrorHandler();
3536

3637
/**
@@ -143,10 +144,37 @@ export const useBbnQuery = () => {
143144
refetchInterval: false, // Disable automatic periodic refetching
144145
});
145146

147+
/**
148+
* Gets the current height of the Babylon Genesis chain.
149+
* @returns {Promise<number>} - The current height of the Babylon Genesis chain.
150+
*/
151+
const babyTipQuery = useClientQuery({
152+
queryKey: [BBN_HEIGHT_KEY],
153+
queryFn: async () => {
154+
if (!tmClient) {
155+
return 0;
156+
}
157+
try {
158+
const status = await tmClient.status();
159+
return status.syncInfo.latestBlockHeight;
160+
} catch (error) {
161+
throw new ClientError(
162+
ERROR_CODES.EXTERNAL_SERVICE_UNAVAILABLE,
163+
"Error getting Babylon chain height",
164+
{ cause: error as Error },
165+
);
166+
}
167+
},
168+
enabled: Boolean(tmClient && connected),
169+
staleTime: ONE_SECOND * 10,
170+
refetchInterval: false, // Disable automatic periodic refetching
171+
});
172+
146173
return {
147174
rewardsQuery,
148175
balanceQuery,
149176
btcTipQuery,
177+
babyTipQuery,
150178
hasRpcError,
151179
reconnectRpc: reconnect,
152180
queryClient,

src/ui/common/hooks/services/useStakingManagerService.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { BabylonBtcStakingManager } from "@babylonlabs-io/btc-staking-ts";
2-
import { useCallback } from "react";
2+
import { useCallback, useMemo } from "react";
33

4+
import { getNetworkConfigBBN } from "@/ui/common/config/network/bbn";
45
import { useBTCWallet } from "@/ui/common/context/wallet/BTCWalletProvider";
56
import { useCosmosWallet } from "@/ui/common/context/wallet/CosmosWalletProvider";
67
import { useBbnTransaction } from "@/ui/common/hooks/client/rpc/mutation/useBbnTransaction";
8+
import { useBbnQuery } from "@/ui/common/hooks/client/rpc/queries/useBbnQuery";
79
import { useEventBus } from "@/ui/common/hooks/useEventBus";
810
import { useLogger } from "@/ui/common/hooks/useLogger";
911
import { useAppState } from "@/ui/common/state";
1012

1113
export const useStakingManagerService = () => {
1214
const { networkInfo } = useAppState();
1315
const { signBbnTx } = useBbnTransaction();
16+
const {
17+
babyTipQuery: { data: bbnHeight = 0 },
18+
} = useBbnQuery();
1419
const logger = useLogger();
1520
const eventBus = useEventBus();
1621

@@ -24,6 +29,18 @@ export const useStakingManagerService = () => {
2429

2530
const versionedParams = networkInfo?.params.bbnStakingParams?.versions;
2631

32+
const networkUpgrade = useMemo(
33+
() => ({
34+
pop: {
35+
upgradeHeight: networkInfo?.networkUpgrade?.pop?.[0]?.height ?? 0,
36+
version: networkInfo?.networkUpgrade?.pop?.[0]?.version ?? 0,
37+
},
38+
}),
39+
[networkInfo?.networkUpgrade],
40+
);
41+
42+
const { chainId } = getNetworkConfigBBN();
43+
2744
const isLoading =
2845
!btcNetwork ||
2946
!cosmosConnected ||
@@ -56,6 +73,10 @@ export const useStakingManagerService = () => {
5673

5774
const bbnProvider = {
5875
signTransaction: signBbnTx,
76+
getCurrentHeight: async () => {
77+
return bbnHeight;
78+
},
79+
getChainId: async () => chainId,
5980
};
6081

6182
return new BabylonBtcStakingManager(
@@ -64,18 +85,22 @@ export const useStakingManagerService = () => {
6485
btcProvider,
6586
bbnProvider,
6687
eventBus,
88+
networkUpgrade,
6789
);
6890
}, [
6991
isLoading,
7092
btcNetwork,
7193
versionedParams,
94+
networkUpgrade,
95+
bbnHeight,
7296
logger,
7397
cosmosConnected,
7498
btcConnected,
7599
eventBus,
76100
signPsbt,
77101
signMessage,
78102
signBbnTx,
103+
chainId,
79104
]);
80105

81106
return {

src/ui/common/types/networkInfo.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { StakingParams } from "@babylonlabs-io/btc-staking-ts";
22

3+
export interface PopUpgradeConfig {
4+
height: number;
5+
version: number;
6+
}
7+
8+
export interface NetworkUpgradeConfig {
9+
pop?: PopUpgradeConfig[];
10+
}
11+
312
export interface BbnStakingParamsVersion extends StakingParams {
413
version: number;
514
minCommissionRate: string;
@@ -28,7 +37,6 @@ export interface BtcEpochCheckParams {
2837
latestParam: BtcEpochCheckParamsVersion;
2938
versions: BtcEpochCheckParamsVersion[];
3039
}
31-
3240
export interface StakingStatus {
3341
isStakingOpen: boolean;
3442
}
@@ -41,4 +49,5 @@ export interface Params {
4149
export interface NetworkInfo {
4250
stakingStatus: StakingStatus;
4351
params: Params;
52+
networkUpgrade?: NetworkUpgradeConfig;
4453
}

0 commit comments

Comments
 (0)