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

Commit d83709d

Browse files
committed
feat(BABY): add delegation service
1 parent 7c00b5a commit d83709d

3 files changed

Lines changed: 122 additions & 4 deletions

File tree

src/ui/baby/hooks/api/useDelegations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { useClientQuery } from "@/ui/common/hooks/client/useClient";
33

44
const BABY_DELEGATIONS_KEY = "BABY_DELEGATIONS_KEY";
55

6-
export function useValidators(
6+
export function useDelegations(
77
address: string,
8-
{ enabled }: { enabled: boolean },
8+
{ enabled = true }: { enabled?: boolean } = {},
99
) {
1010
return useClientQuery({
1111
queryKey: [BABY_DELEGATIONS_KEY, address],
1212
queryFn: () => babylon.client.baby.getDelegations(address),
13-
enabled,
13+
enabled: Boolean(address) && enabled,
1414
});
1515
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { useCallback } from "react";
2+
3+
import babylon from "@/infrastructure/babylon";
4+
import { useDelegations } from "@/ui/baby/hooks/api/useDelegations";
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+
interface StakingParams {
11+
validatorAddress: string;
12+
amount: string;
13+
}
14+
15+
export function useDelegationService() {
16+
const { bech32Address } = useCosmosWallet();
17+
const { data: delegations = [], refetch: refetchDelegations } =
18+
useDelegations(bech32Address);
19+
const { signBbnTx, sendBbnTx, estimateBbnGasFee } = useBbnTransaction();
20+
const { handleError } = useError();
21+
const logger = useLogger();
22+
23+
const stake = useCallback(
24+
async ({ validatorAddress, amount }: StakingParams) => {
25+
try {
26+
if (!bech32Address) throw Error("Babylon Wallet is not connected");
27+
28+
const stakeMsg = babylon.txs.baby.createStakeMsg({
29+
validatorAddress,
30+
delegatorAddress: bech32Address,
31+
amount: babylon.utils.babyToUbbn(Number(amount)),
32+
});
33+
const signedTx = await signBbnTx(stakeMsg);
34+
const result = await sendBbnTx(signedTx);
35+
36+
logger.info("Baby Staking: stake", {
37+
txHash: result?.txHash,
38+
});
39+
await refetchDelegations();
40+
} catch (error: any) {
41+
handleError({ error });
42+
logger.error(error);
43+
}
44+
},
45+
[
46+
bech32Address,
47+
signBbnTx,
48+
sendBbnTx,
49+
handleError,
50+
refetchDelegations,
51+
logger,
52+
],
53+
);
54+
55+
const unstake = useCallback(
56+
async ({ validatorAddress, amount }: StakingParams) => {
57+
try {
58+
if (!bech32Address) throw Error("Babylon Wallet is not connected");
59+
60+
const unstakeMsg = babylon.txs.baby.createUnstakeMsg({
61+
validatorAddress,
62+
delegatorAddress: bech32Address,
63+
amount: babylon.utils.babyToUbbn(Number(amount)),
64+
});
65+
const signedTx = await signBbnTx(unstakeMsg);
66+
const result = await sendBbnTx(signedTx);
67+
68+
logger.info("Baby Staking: unstake", {
69+
txHash: result?.txHash,
70+
});
71+
await refetchDelegations();
72+
} catch (error: any) {
73+
handleError({ error });
74+
logger.error(error);
75+
}
76+
},
77+
[
78+
bech32Address,
79+
signBbnTx,
80+
sendBbnTx,
81+
handleError,
82+
refetchDelegations,
83+
logger,
84+
],
85+
);
86+
87+
const estimateStakingFee = useCallback(
88+
async ({ validatorAddress, amount }: StakingParams) => {
89+
try {
90+
if (!bech32Address) throw Error("Babylon Wallet is not connected");
91+
92+
const stakeMsg = babylon.txs.baby.createStakeMsg({
93+
validatorAddress,
94+
delegatorAddress: bech32Address,
95+
amount: babylon.utils.babyToUbbn(Number(amount)),
96+
});
97+
const result = await estimateBbnGasFee(stakeMsg);
98+
99+
return result.amount.reduce(
100+
(sum, { amount }) => sum + Number(amount),
101+
0,
102+
);
103+
} catch (error: any) {
104+
handleError({ error });
105+
logger.error(error);
106+
return 0;
107+
}
108+
},
109+
[bech32Address, estimateBbnGasFee, handleError, logger],
110+
);
111+
112+
return {
113+
delegations,
114+
stake,
115+
unstake,
116+
estimateStakingFee,
117+
};
118+
}

src/ui/baby/hooks/services/useRewardService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function useRewardService() {
2727
const signedTx = await signBbnTx(claimRewardMsg);
2828
const result = await sendBbnTx(signedTx);
2929

30-
await logger.info("Baby Staking: claim reward", {
30+
logger.info("Baby Staking: claim reward", {
3131
txHash: result?.txHash,
3232
});
3333
await refetchRewards();

0 commit comments

Comments
 (0)