Skip to content

Commit 08a92cb

Browse files
authored
Namadillo: Optimistically updating staking rewards balance (#1115)
* feat(namadillo): adding optimistic updates to staking rewards claiming * refactor(namadillo): making claim rewards object clearer * refactor(namadillo): using account?.address as a dependency instead of account only
1 parent efd9f3d commit 08a92cb

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

apps/namadillo/src/atoms/staking/atoms.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ export const createWithdrawTxAtomFamily = atomFamily((id: string) => {
9696
});
9797
});
9898

99-
export const claimRewardsAtom = atomWithMutation((get) => {
100-
const chain = get(chainAtom);
101-
return {
102-
mutationKey: ["create-claim-tx"],
103-
enabled: chain.isSuccess,
104-
mutationFn: async ({
105-
params,
106-
gasConfig,
107-
account,
108-
}: BuildTxAtomParams<ClaimRewardsMsgValue>) => {
109-
return createClaimTx(chain.data!, account, params, gasConfig);
110-
},
111-
};
112-
});
113-
11499
export const claimableRewardsAtom = atomWithQuery<AddressBalance>((get) => {
115100
const account = get(defaultAccountAtom);
116101
const chainParameters = get(chainParametersAtom);
@@ -134,6 +119,21 @@ export const claimableRewardsAtom = atomWithQuery<AddressBalance>((get) => {
134119
};
135120
});
136121

122+
export const claimRewardsAtom = atomWithMutation((get) => {
123+
const chain = get(chainAtom);
124+
return {
125+
mutationKey: ["create-claim-tx"],
126+
enabled: chain.isSuccess,
127+
mutationFn: async ({
128+
params,
129+
gasConfig,
130+
account,
131+
}: BuildTxAtomParams<ClaimRewardsMsgValue>) => {
132+
return createClaimTx(chain.data!, account, params, gasConfig);
133+
},
134+
};
135+
});
136+
137137
export const claimAndStakeRewardsAtom = atomWithMutation((get) => {
138138
const chain = get(chainAtom);
139139
const claimableRewards = get(claimableRewardsAtom);

apps/namadillo/src/atoms/staking/services.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
WithdrawProps,
1313
WrapperTxProps,
1414
} from "@namada/types";
15+
import { queryClient } from "App/Common/QueryProvider";
1516
import { getSdkInstance } from "hooks";
1617
import { TransactionPair, buildTxPair } from "lib/query";
1718
import { Address, AddressBalance, ChainSettings, GasConfig } from "types";
@@ -159,3 +160,11 @@ export const createClaimAndStakeTx = async (
159160
account.address
160161
);
161162
};
163+
164+
export const clearClaimRewards = (accountAddress: string): void => {
165+
const emptyClaimRewards = {};
166+
queryClient.setQueryData(
167+
["claim-rewards", accountAddress],
168+
() => emptyClaimRewards
169+
);
170+
};

apps/namadillo/src/hooks/useTransactionCallbacks.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
import { accountBalanceAtom } from "atoms/accounts";
1+
import { accountBalanceAtom, defaultAccountAtom } from "atoms/accounts";
22
import { shouldUpdateBalanceAtom, shouldUpdateProposalAtom } from "atoms/etc";
3-
import { claimableRewardsAtom } from "atoms/staking";
3+
import { claimableRewardsAtom, clearClaimRewards } from "atoms/staking";
44
import { useAtomValue, useSetAtom } from "jotai";
55
import { useTransactionEventListener } from "utils";
66

77
export const useTransactionCallback = (): void => {
88
const { refetch: refetchBalances } = useAtomValue(accountBalanceAtom);
99
const { refetch: refetchRewards } = useAtomValue(claimableRewardsAtom);
10+
const { data: account } = useAtomValue(defaultAccountAtom);
1011
const shouldUpdateBalance = useSetAtom(shouldUpdateBalanceAtom);
1112

1213
const onBalanceUpdate = (): void => {
1314
// TODO: refactor this after event subscription is enabled on indexer
1415
shouldUpdateBalance(true);
1516
refetchBalances();
16-
refetchRewards();
1717

1818
const timePolling = 6 * 1000;
1919
setTimeout(() => shouldUpdateBalance(false), timePolling);
20+
21+
if (account?.address) {
22+
clearClaimRewards(account.address);
23+
setTimeout(() => refetchRewards(), timePolling);
24+
}
2025
};
2126

2227
useTransactionEventListener("Bond.Success", onBalanceUpdate);
2328
useTransactionEventListener("Unbond.Success", onBalanceUpdate);
2429
useTransactionEventListener("Withdraw.Success", onBalanceUpdate);
2530
useTransactionEventListener("Redelegate.Success", onBalanceUpdate);
26-
useTransactionEventListener("ClaimRewards.Success", onBalanceUpdate);
31+
useTransactionEventListener("ClaimRewards.Success", onBalanceUpdate, [
32+
account?.address,
33+
]);
2734

2835
const shouldUpdateProposal = useSetAtom(shouldUpdateProposalAtom);
2936

apps/namadillo/src/utils/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ export const proposalIdToString = (proposalId: bigint): string =>
3636

3737
export const useTransactionEventListener = <T extends keyof WindowEventMap>(
3838
event: T,
39-
handler: (this: Window, ev: WindowEventMap[T]) => void
39+
handler: (this: Window, ev: WindowEventMap[T]) => void,
40+
deps: React.DependencyList = []
4041
): void => {
4142
useEffect(() => {
4243
window.addEventListener(event, handler);
4344
return () => {
4445
window.removeEventListener(event, handler);
4546
};
46-
}, []);
47+
}, deps);
4748
};
4849

4950
const secondsToDateTime = (seconds: bigint): DateTime =>

0 commit comments

Comments
 (0)