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

Commit 971b2c4

Browse files
committed
feat(BABY): add reward service
1 parent 7277fa2 commit 971b2c4

6 files changed

Lines changed: 55 additions & 9 deletions

File tree

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
@@ -29,7 +29,7 @@
2929
"node": "24.2.0"
3030
},
3131
"dependencies": {
32-
"@babylonlabs-io/babylon-proto-ts": "1.6.2",
32+
"@babylonlabs-io/babylon-proto-ts": "1.7.1",
3333
"@babylonlabs-io/btc-staking-ts": "2.3.4",
3434
"@babylonlabs-io/core-ui": "1.6.0",
3535
"@babylonlabs-io/wallet-connector": "1.8.4",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import babylon from "@/infrastructure/babylon";
22
import { useClientQuery } from "@/ui/common/hooks/client/useClient";
33

4-
const BABY_DELEGATIONS_KEY = "BABY_VALIDATORS_KEY";
4+
const BABY_DELEGATIONS_KEY = "BABY_DELEGATIONS_KEY";
55

66
export function useValidators(
77
address: string,
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import babylon from "@/infrastructure/babylon";
22
import { useClientQuery } from "@/ui/common/hooks/client/useClient";
33

4-
const BABY_REWARDS_KEY = "BABY_VALIDATORS_KEY";
4+
const BABY_REWARDS_KEY = "BABY_REWARDS_KEY";
55

6-
export function useValidators(
6+
export function useRewards(
77
address: string,
88
{ enabled = true }: { enabled?: boolean } = {},
99
) {
1010
return useClientQuery({
1111
queryKey: [BABY_REWARDS_KEY, address],
1212
queryFn: () => babylon.client.baby.getRewards(address),
13-
enabled,
13+
enabled: Boolean(address) && enabled,
1414
});
1515
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useCallback } from "react";
2+
3+
import babylon from "@/infrastructure/babylon";
4+
import { useRewards } from "@/ui/baby/hooks/api/useRewards";
5+
import { useError } from "@/ui/common/context/Error/ErrorProvider";
6+
import { useCosmosWallet } from "@/ui/common/context/wallet/CosmosWalletProvider";
7+
import { useBbnTransaction } from "@/ui/common/hooks/client/rpc/mutation/useBbnTransaction";
8+
import { useLogger } from "@/ui/common/hooks/useLogger";
9+
10+
export function useRewardService() {
11+
const { bech32Address } = useCosmosWallet();
12+
const { data: rewards = [], refetch: refetchRewards } =
13+
useRewards(bech32Address);
14+
const { signBbnTx, sendBbnTx } = useBbnTransaction();
15+
const { handleError } = useError();
16+
const logger = useLogger();
17+
18+
const claimReward = useCallback(
19+
async (validatorAddress: string) => {
20+
try {
21+
if (!bech32Address) throw Error("Babylon Wallet is not connected");
22+
23+
const claimRewardMsg = babylon.txs.baby.createClaimRewardMsg({
24+
validatorAddress,
25+
delegatorAddress: bech32Address,
26+
});
27+
const signedTx = await signBbnTx(claimRewardMsg);
28+
const result = await sendBbnTx(signedTx);
29+
30+
await logger.info("Baby Staking: claim reward", {
31+
txHash: result?.txHash,
32+
});
33+
await refetchRewards();
34+
} catch (error: any) {
35+
handleError({ error });
36+
logger.error(error);
37+
}
38+
},
39+
[logger, bech32Address, signBbnTx, sendBbnTx, refetchRewards, handleError],
40+
);
41+
42+
return {
43+
rewards,
44+
claimReward,
45+
};
46+
}

0 commit comments

Comments
 (0)