Skip to content

Namadillo: Optimistically updating staking rewards balance #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions apps/namadillo/src/atoms/staking/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ export const createWithdrawTxAtomFamily = atomFamily((id: string) => {
});
});

export const claimRewardsAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
return {
mutationKey: ["create-claim-tx"],
enabled: chain.isSuccess,
mutationFn: async ({
params,
gasConfig,
account,
}: BuildTxAtomParams<ClaimRewardsMsgValue>) => {
return createClaimTx(chain.data!, account, params, gasConfig);
},
};
});

export const claimableRewardsAtom = atomWithQuery<AddressBalance>((get) => {
const account = get(defaultAccountAtom);
const chainParameters = get(chainParametersAtom);
Expand All @@ -134,6 +119,21 @@ export const claimableRewardsAtom = atomWithQuery<AddressBalance>((get) => {
};
});

export const claimRewardsAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
return {
mutationKey: ["create-claim-tx"],
enabled: chain.isSuccess,
mutationFn: async ({
params,
gasConfig,
account,
}: BuildTxAtomParams<ClaimRewardsMsgValue>) => {
return createClaimTx(chain.data!, account, params, gasConfig);
},
};
});

export const claimAndStakeRewardsAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
const claimableRewards = get(claimableRewardsAtom);
Expand Down
5 changes: 5 additions & 0 deletions apps/namadillo/src/atoms/staking/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
WithdrawProps,
WrapperTxProps,
} from "@namada/types";
import { queryClient } from "App/Common/QueryProvider";
import { getSdkInstance } from "hooks";
import { TransactionPair, buildTxPair } from "lib/query";
import { Address, AddressBalance, ChainSettings, GasConfig } from "types";
Expand Down Expand Up @@ -159,3 +160,7 @@ export const createClaimAndStakeTx = async (
account.address
);
};

export const clearClaimRewards = (accountAddress: string): void => {
queryClient.setQueryData(["claim-rewards", accountAddress], () => ({}));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try this as invalidateQueries https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation ? I think this is the "correct way" to clear queries for react-query

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually not. I'm overriding the result of the claim-rewards query, so no rewards are available :) . But maybe I can be more explicit on this. Will make a small change before merging

};
15 changes: 11 additions & 4 deletions apps/namadillo/src/hooks/useTransactionCallbacks.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { accountBalanceAtom } from "atoms/accounts";
import { accountBalanceAtom, defaultAccountAtom } from "atoms/accounts";
import { shouldUpdateBalanceAtom, shouldUpdateProposalAtom } from "atoms/etc";
import { claimableRewardsAtom } from "atoms/staking";
import { claimableRewardsAtom, clearClaimRewards } from "atoms/staking";
import { useAtomValue, useSetAtom } from "jotai";
import { useTransactionEventListener } from "utils";

export const useTransactionCallback = (): void => {
const { refetch: refetchBalances } = useAtomValue(accountBalanceAtom);
const { refetch: refetchRewards } = useAtomValue(claimableRewardsAtom);
const { data: account } = useAtomValue(defaultAccountAtom);
const shouldUpdateBalance = useSetAtom(shouldUpdateBalanceAtom);

const onBalanceUpdate = (): void => {
// TODO: refactor this after event subscription is enabled on indexer
shouldUpdateBalance(true);
refetchBalances();
refetchRewards();

const timePolling = 6 * 1000;
setTimeout(() => shouldUpdateBalance(false), timePolling);

if (account?.address) {
clearClaimRewards(account.address);
setTimeout(() => refetchRewards(), timePolling);
}
};

useTransactionEventListener("Bond.Success", onBalanceUpdate);
useTransactionEventListener("Unbond.Success", onBalanceUpdate);
useTransactionEventListener("Withdraw.Success", onBalanceUpdate);
useTransactionEventListener("Redelegate.Success", onBalanceUpdate);
useTransactionEventListener("ClaimRewards.Success", onBalanceUpdate);
useTransactionEventListener("ClaimRewards.Success", onBalanceUpdate, [
account,
]);
Copy link
Collaborator

@euharrison euharrison Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea! I'm asking myself if this object changes for whatever reason and if we should use account?.address instead


const shouldUpdateProposal = useSetAtom(shouldUpdateProposalAtom);

Expand Down
5 changes: 3 additions & 2 deletions apps/namadillo/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ export const proposalIdToString = (proposalId: bigint): string =>

export const useTransactionEventListener = <T extends keyof WindowEventMap>(
event: T,
handler: (this: Window, ev: WindowEventMap[T]) => void
handler: (this: Window, ev: WindowEventMap[T]) => void,
deps: React.DependencyList = []
): void => {
useEffect(() => {
window.addEventListener(event, handler);
return () => {
window.removeEventListener(event, handler);
};
}, []);
}, deps);
};

const secondsToDateTime = (seconds: bigint): DateTime =>
Expand Down
Loading