|
| 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 | +} |
0 commit comments