Skip to content

Commit

Permalink
fix: rewards fetch shall fallback to 0 if BBN address never had deleg…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
jrwbabylonlab committed Dec 18, 2024
1 parent dd9154d commit 2a88fa1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
12 changes: 4 additions & 8 deletions src/app/components/PersonalBalance/PersonalBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ export function PersonalBalance() {
const {
balanceQuery: { data: cosmosBalance, isLoading: cosmosBalanceLoading },
} = useBbnQuery();
const { getRewards, claimRewards } = useRewardsService();

const { data: rewards, isLoading: rewardsLoading } = useQuery({
queryKey: [QUERY_KEYS.REWARDS],
queryFn: getRewards,
});
const { claimRewards } = useRewardsService();
const { rewardsQuery } = useBbnQuery();

const { data: btcBalance, isLoading: btcBalanceLoading } = useQuery({
queryKey: [QUERY_KEYS.BTC_BALANCE],
Expand Down Expand Up @@ -75,9 +71,9 @@ export function PersonalBalance() {
<div className="divider mx-0 my-2 md:divider-horizontal" />

<StatItem
loading={rewardsLoading}
loading={rewardsQuery.isLoading}
title="BBN Rewards"
value={`${ubbnToBbn(rewards ?? 0)} BBN`}
value={`${ubbnToBbn(rewardsQuery.data ?? 0)} BBN`}
actionComponent={{
title: "Claim",
onAction: claimRewards,
Expand Down
38 changes: 35 additions & 3 deletions src/app/hooks/client/rpc/queries/useBbnQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useClientQuery } from "../../useClient";
const BBN_BTCLIGHTCLIENT_TIP_KEY = "BBN_BTCLIGHTCLIENT_TIP";
const BBN_BALANCE_KEY = "BBN_BALANCE";
const BBN_REWARDS_KEY = "BBN_REWARDS";
const REWARD_GAUGE_KEY_BTC_DELEGATION = "btc_delegation";

/**
* Query service for Babylon which contains all the queries for
Expand All @@ -28,7 +29,7 @@ export const useBbnQuery = () => {

/**
* Gets the rewards from the user's account.
* @returns {Promise<Object>} - The rewards from the user's account.
* @returns {Promise<number>} - The rewards from the user's account.
*/
const rewardsQuery = useClientQuery({
queryKey: [BBN_REWARDS_KEY, bech32Address],
Expand All @@ -37,13 +38,44 @@ export const useBbnQuery = () => {
return undefined;
}
const { incentive } = setupIncentiveExtension(queryClient);

const req: incentivequery.QueryRewardGaugesRequest =
incentivequery.QueryRewardGaugesRequest.fromPartial({
address: bech32Address,
});

return incentive.RewardGauges(req);
let rewards;
try {
rewards = await incentive.RewardGauges(req);
} catch (error) {
// If error message contains "reward gauge not found", silently return 0
// This is to handle the case where the user has no rewards, meaning
// they have not staked
if (
error instanceof Error &&
error.message.includes("reward gauge not found")
) {
return 0;
}
throw error;
}
if (!rewards) {
return 0;
}

const coins =
rewards.rewardGauges[REWARD_GAUGE_KEY_BTC_DELEGATION]?.coins;
if (!coins) {
return 0;
}

const withdrawnCoins = rewards.rewardGauges[
REWARD_GAUGE_KEY_BTC_DELEGATION
]?.withdrawnCoins.reduce((acc, coin) => acc + Number(coin.amount), 0);

return (
coins.reduce((acc, coin) => acc + Number(coin.amount), 0) -
(withdrawnCoins || 0)
);
},
enabled: Boolean(queryClient && connected && bech32Address),
staleTime: ONE_MINUTE,
Expand Down
28 changes: 0 additions & 28 deletions src/app/hooks/services/useRewardsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,12 @@ import { BBN_REGISTRY_TYPE_URLS } from "@/utils/wallet/bbnRegistry";
import { useBbnTransaction } from "../client/rpc/mutation/useBbnTransaction";
import { useBbnQuery } from "../client/rpc/queries/useBbnQuery";

const REWARD_GAUGE_KEY_BTC_DELEGATION = "btc_delegation";

export const useRewardsService = () => {
const { bech32Address } = useCosmosWallet();

const { rewardsQuery } = useBbnQuery();
const { estimateBbnGasFee, sendBbnTx } = useBbnTransaction();

/**
* Gets the rewards from the user's account.
* @returns {Promise<number>} The rewards from the user's account.
*/
const getRewards = useCallback(async (): Promise<number> => {
const rewards = rewardsQuery.data;
if (!rewards) {
return 0;
}

const coins = rewards.rewardGauges[REWARD_GAUGE_KEY_BTC_DELEGATION]?.coins;
if (!coins) {
return 0;
}

const withdrawnCoins = rewards.rewardGauges[
REWARD_GAUGE_KEY_BTC_DELEGATION
]?.withdrawnCoins.reduce((acc, coin) => acc + Number(coin.amount), 0);

return (
coins.reduce((acc, coin) => acc + Number(coin.amount), 0) -
(withdrawnCoins || 0)
);
}, [rewardsQuery.data]);

/**
* Estimates the gas fee for claiming rewards.
* @returns {Promise<number>} The gas fee for claiming rewards.
Expand All @@ -62,7 +35,6 @@ export const useRewardsService = () => {
}, [bech32Address, sendBbnTx, rewardsQuery]);

return {
getRewards,
claimRewards,
estimateClaimRewardsGas,
};
Expand Down

0 comments on commit 2a88fa1

Please sign in to comment.