Skip to content

Commit

Permalink
feat: use BBN light client tip height instead from mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Dec 11, 2024
1 parent 6ace8f6 commit a4576d4
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 93 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": "22.3.0"
},
"dependencies": {
"@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.4",
"@babylonlabs-io/babylon-proto-ts": "0.0.3-canary.5",
"@babylonlabs-io/bbn-core-ui": "^0.4.1",
"@babylonlabs-io/bbn-wallet-connect": "^0.1.9",
"@babylonlabs-io/btc-staking-ts": "0.4.0-canary.3",
Expand Down
12 changes: 0 additions & 12 deletions src/app/hooks/client/api/useBTCTipHeight.ts

This file was deleted.

36 changes: 35 additions & 1 deletion src/app/hooks/client/query/useBbnQueryClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { incentivequery } from "@babylonlabs-io/babylon-proto-ts";
import {
btclightclientquery,
incentivequery,
} from "@babylonlabs-io/babylon-proto-ts";
import {
QueryClient,
createProtobufRpcClient,
Expand All @@ -8,8 +11,13 @@ import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { useCallback, useEffect, useState } from "react";

import { BBN_RPC_URL } from "@/app/common/rpc";
import { ONE_MINUTE } from "@/app/constants";
import { useCosmosWallet } from "@/app/context/wallet/CosmosWalletProvider";

import { useAPIQuery } from "../api/useApi";

export const BBN_BTCLIGHTCLIENT_TIP_KEY = "BBN_BTCLIGHTCLIENT_TIP";

/**
* Query service for Babylon which contains all the queries for
* interacting with Babylon RPC nodes
Expand Down Expand Up @@ -58,9 +66,25 @@ export const useBbnQueryClient = () => {
return Number(balance?.amount ?? 0);
}, [connected, queryClient, bech32Address]);

const btcTipQuery = useAPIQuery({
queryKey: [BBN_BTCLIGHTCLIENT_TIP_KEY],
queryFn: async () => {
if (!queryClient) {
return undefined;
}
const { btclightQueryClient } = setupBtclightClientExtension(queryClient);
const req = btclightclientquery.QueryTipRequest.fromPartial({});
const { header } = await btclightQueryClient.Tip(req);
return header;
},
enabled: Boolean(queryClient),
staleTime: ONE_MINUTE,
});

return {
getRewards,
getBalance,
btcTipQuery,
};
};

Expand All @@ -74,3 +98,13 @@ const setupIncentiveExtension = (
const incentiveQueryClient = new incentivequery.QueryClientImpl(rpc);
return { incentive: incentiveQueryClient };
};

const setupBtclightClientExtension = (
base: QueryClient,
): {
btclightQueryClient: btclightclientquery.QueryClientImpl;
} => {
const rpc = createProtobufRpcClient(base);
const btclightQueryClient = new btclightclientquery.QueryClientImpl(rpc);
return { btclightQueryClient };
};
23 changes: 10 additions & 13 deletions src/app/hooks/services/useDelegationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type ActionType = keyof typeof ACTIONS;
interface TxProps {
stakingTxHashHex: string;
stakingTxHex: string;
stakingHeight: number;
paramsVersion: number;
unbondingTxHex: string;
covenantUnbondingSignatures?: {
Expand Down Expand Up @@ -91,7 +90,7 @@ export function useDelegationService() {

[ACTIONS.UNBOUND]: async ({
stakingInput,
stakingHeight,
paramsVersion,
stakingTxHashHex,
stakingTxHex,
unbondingTxHex,
Expand All @@ -103,7 +102,7 @@ export function useDelegationService() {

await submitUnbondingTx(
stakingInput,
stakingHeight,
paramsVersion,
stakingTxHex,
unbondingTxHex,
covenantUnbondingSignatures.map((sig) => ({
Expand All @@ -121,12 +120,12 @@ export function useDelegationService() {
[ACTIONS.WITHDRAW_ON_EARLY_UNBOUNDING]: async ({
stakingTxHashHex,
stakingInput,
stakingHeight,
paramsVersion,
unbondingTxHex,
}: TxProps) => {
await submitEarlyUnbondedWithdrawalTx(
stakingInput,
stakingHeight,
paramsVersion,
unbondingTxHex,
);

Expand All @@ -139,7 +138,7 @@ export function useDelegationService() {
[ACTIONS.WITHDRAW_ON_EARLY_UNBOUNDING_SLASHING]: async ({
stakingTxHashHex,
stakingInput,
stakingHeight,
paramsVersion,
unbondingSlashingTxHex,
}) => {
if (!unbondingSlashingTxHex) {
Expand All @@ -150,7 +149,7 @@ export function useDelegationService() {

await submitEarlyUnbondedWithdrawalTx(
stakingInput,
stakingHeight,
paramsVersion,
unbondingSlashingTxHex,
);

Expand All @@ -162,13 +161,13 @@ export function useDelegationService() {

[ACTIONS.WITHDRAW_ON_TIMELOCK]: async ({
stakingInput,
stakingHeight,
paramsVersion,
stakingTxHashHex,
stakingTxHex,
}: TxProps) => {
await submitTimelockUnbondedWithdrawalTx(
stakingInput,
stakingHeight,
paramsVersion,
stakingTxHex,
);

Expand All @@ -180,7 +179,7 @@ export function useDelegationService() {

[ACTIONS.WITHDRAW_ON_TIMELOCK_SLASHING]: async ({
stakingInput,
stakingHeight,
paramsVersion,
stakingTxHashHex,
slashingTxHex,
}) => {
Expand All @@ -190,7 +189,7 @@ export function useDelegationService() {

await submitTimelockUnbondedWithdrawalTx(
stakingInput,
stakingHeight,
paramsVersion,
slashingTxHex,
);

Expand Down Expand Up @@ -245,7 +244,6 @@ export function useDelegationService() {
state,
slashingTxHex,
unbondingSlashingTxHex,
startHeight,
} = delegation;

const finalityProviderPk = finalityProviderBtcPksHex[0];
Expand All @@ -263,7 +261,6 @@ export function useDelegationService() {
await execute?.({
stakingTxHashHex,
stakingTxHex,
stakingHeight: startHeight,
paramsVersion,
unbondingTxHex,
covenantUnbondingSignatures,
Expand Down
Loading

0 comments on commit a4576d4

Please sign in to comment.