Skip to content

Commit

Permalink
[#522] fix bugs in usePendingTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dyczka committed Mar 25, 2024
1 parent 4f56941 commit bb26945
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 55 deletions.
4 changes: 2 additions & 2 deletions govtool/frontend/src/context/pendingTransaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export type TransactionType =
export type TransactionStateWithoutResource = {
type: TransactionTypeWithoutResource;
transactionHash: string;
time: Date;
time: string;
resourceId?: never;
};

export type TransactionStateWithResource = {
type: TransactionTypeWithResource;
transactionHash: string;
time: Date;
time: string;
resourceId: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ const DB_SYNC_REFRESH_TIME = 3 * 1000; // 3 SECONDS
const DB_SYNC_MAX_ATTEMPTS = 10;

type UsePendingTransactionProps = {
dRepID: string;
isEnabled: boolean;
stakeKey: string | undefined;
};

export const usePendingTransaction = ({
isEnabled,
stakeKey,
dRepID,
}: UsePendingTransactionProps) => {
const { t } = useTranslation();
const { openModal, closeModal } = useModal<StatusModalState>();
Expand Down Expand Up @@ -57,11 +55,10 @@ export const usePendingTransaction = ({
const fromLocalStorage = getItemFromLocalStorage(
`${PENDING_TRANSACTION_KEY}_${stakeKey}`
);
setTransaction(
fromLocalStorage
? { ...fromLocalStorage, time: new Date(fromLocalStorage.time) }
: null
);
setTransaction({
...fromLocalStorage,
resourceId: fromLocalStorage?.resourceId ?? undefined,
});
}
}, [isEnabled, stakeKey]);

Expand All @@ -84,9 +81,7 @@ export const usePendingTransaction = ({
if (isEnabled) {
const desiredResult = getDesiredResult(
type,
dRepID,
resourceId,
stakeKey
);
const queryKey = getQueryKey(type, transaction);

Expand Down Expand Up @@ -120,7 +115,7 @@ export const usePendingTransaction = ({
if (isEnabled && transaction) {
addWarningAlert(t("alerts.transactionInProgress"), 10000);
}
}, [transaction]);
}, [isEnabled, transaction]);

const isPendingTransaction = useCallback(() => {
if (transaction) {
Expand All @@ -144,14 +139,17 @@ export const usePendingTransaction = ({

const updateTransaction = (data: Omit<TransactionState, "time">) => {
const newTransaction = {
time: new Date(),
time: new Date().toISOString(),
...data,
} as TransactionState;

setTransaction(newTransaction);
setItemToLocalStorage(
`${PENDING_TRANSACTION_KEY}_${stakeKey}`,
JSON.stringify(newTransaction)
{
...newTransaction,
resourceId: newTransaction.resourceId || null,
}
);
};

Expand All @@ -162,5 +160,5 @@ export const usePendingTransaction = ({
};
};

const isTransactionExpired = (time: Date): boolean =>
Date.now() - time.getTime() > TIME_TO_EXPIRE_TRANSACTION;
const isTransactionExpired = (time: string): boolean =>
Date.now() - new Date(time).getTime() > TIME_TO_EXPIRE_TRANSACTION;
11 changes: 4 additions & 7 deletions govtool/frontend/src/context/pendingTransaction/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { TransactionType, TransactionState } from "./types";

export const getDesiredResult = (
type: TransactionType,
dRepID: string,
resourceId: string | undefined,
stakeKey?: string
) => {
switch (type) {
case "delegate": {
// current delegation
if (resourceId === dRepID) return dRepID;
if (resourceId === "no confidence") return "drep_always_no_confidence";
if (resourceId === "abstain") return "drep_always_abstain";
return stakeKey;
return resourceId;
}
case "registerAsDrep":
case "registerAsSoleVoter":
Expand All @@ -38,9 +35,9 @@ export const getQueryKey = (
case "retireAsDrep":
case "registerAsSoleVoter":
case "retireAsSoleVoter":
return [QUERY_KEYS.useGetDRepInfoKey, transaction];
return [QUERY_KEYS.useGetDRepInfoKey, transaction?.transactionHash];
case "delegate":
return [QUERY_KEYS.getAdaHolderCurrentDelegationKey, transaction];
return [QUERY_KEYS.getAdaHolderCurrentDelegationKey, transaction?.transactionHash];
default:
return undefined;
}
Expand All @@ -58,7 +55,7 @@ export const refetchData = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = await queryClient.getQueryData<any>(queryKey);

if (type === "delegate") return data.currentDelegation;
if (type === "delegate") return data;
if (type === "registerAsDrep" || type === "retireAsDrep") return data.isRegisteredAsDRep;
if (type === "registerAsSoleVoter" || type === "retireAsSoleVoter") return data.isRegisteredAsSoleVoter;
return undefined;
Expand Down
38 changes: 15 additions & 23 deletions govtool/frontend/src/context/wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// TODO: enable eslint and fix all the errors with wallet refactor
/* eslint-disable */
// @ts-nocheck
import {
createContext,
useCallback,
Expand Down Expand Up @@ -71,7 +70,8 @@ import { getUtxos } from './getUtxos';
import { useModal, useSnackbar } from '.';
import {
PendingTransaction,
TransactionType,
TransactionStateWithResource,
TransactionStateWithoutResource,
usePendingTransaction,
} from './pendingTransaction';

Expand All @@ -97,6 +97,16 @@ type TreasuryProps = {
url: string;
};

type BuildSignSubmitConwayCertTxArgs = {
certBuilder?: CertificatesBuilder;
govActionBuilder?: VotingProposalBuilder;
votingBuilder?: VotingBuilder;
voterDeposit?: string;
} & (
| Pick<TransactionStateWithoutResource, 'type' | 'resourceId'>
| Pick<TransactionStateWithResource, 'type' | 'resourceId'>
);

interface CardanoContext {
address?: string;
disconnectWallet: () => Promise<void>;
Expand All @@ -119,24 +129,15 @@ interface CardanoContext {
type,
votingBuilder,
voterDeposit,
}: {
certBuilder?: CertificatesBuilder;
govActionBuilder?: VotingProposalBuilder;
resourceId?: string;
type: TransactionType;
votingBuilder?: VotingBuilder;
voterDeposit?: string;
}) => Promise<string>;
}: BuildSignSubmitConwayCertTxArgs) => Promise<string>;
buildDRepRegCert: (
url?: string,
hash?: string,
hash?: string,
) => Promise<CertificatesBuilder>;
buildVoteDelegationCert: (vote: string) => Promise<CertificatesBuilder>;
buildDRepUpdateCert: (
url?: string,
hash?: string,
hash?: string,
) => Promise<CertificatesBuilder>;
buildDRepRetirementCert: (
voterDeposit: string,
Expand All @@ -147,7 +148,6 @@ interface CardanoContext {
index: number,
cip95MetadataURL?: string,
cip95MetadataHash?: string,
cip95MetadataHash?: string,
) => Promise<VotingBuilder>;
pendingTransaction: PendingTransaction;
isPendingTransaction: () => boolean;
Expand Down Expand Up @@ -186,7 +186,6 @@ const CardanoProvider = (props: Props) => {
const [stakeKey, setStakeKey] = useState<string | undefined>(undefined);
const [stakeKeys, setStakeKeys] = useState<string[]>([]);
const [isMainnet, setIsMainnet] = useState<boolean>(false);
const { openModal, closeModal } = useModal<StatusModalState>();
const [registeredStakeKeysListState, setRegisteredPubStakeKeysState] =
useState<string[]>([]);
const [error, setError] = useState<string | undefined>(undefined);
Expand All @@ -201,7 +200,7 @@ const CardanoProvider = (props: Props) => {
const epochParams = getItemFromLocalStorage(PROTOCOL_PARAMS_KEY);

const { isPendingTransaction, updateTransaction, pendingTransaction } =
usePendingTransaction({ dRepID, isEnabled, stakeKey });
usePendingTransaction({ isEnabled, stakeKey });

const getChangeAddress = async (enabledApi: CardanoApiWallet) => {
try {
Expand Down Expand Up @@ -430,14 +429,7 @@ const CardanoProvider = (props: Props) => {
type,
votingBuilder,
voterDeposit,
}: {
certBuilder?: CertificatesBuilder;
govActionBuilder?: VotingProposalBuilder;
resourceId?: string;
type: TransactionType;
votingBuilder?: VotingBuilder;
voterDeposit?: string;
}) => {
}: BuildSignSubmitConwayCertTxArgs) => {
await checkIsMaintenanceOn();
const isPendingTx = isPendingTransaction();
if (isPendingTx) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { getAdaHolderCurrentDelegation } from "@services";
import { QUERY_KEYS } from "@consts";
import { useCardano } from "@context";

export const useGetAdaHolderCurrentDelegationQuery = (stakeKey?: string) => {
export const useGetAdaHolderCurrentDelegationQuery = (stakeKey: string | undefined) => {
const { pendingTransaction } = useCardano();

const { data, isLoading } = useQuery({
queryKey: [
QUERY_KEYS.getAdaHolderCurrentDelegationKey,
pendingTransaction.delegate,
pendingTransaction.delegate?.transactionHash,
],
queryFn: () => getAdaHolderCurrentDelegation({ stakeKey }),
enabled: !!stakeKey,
Expand Down
4 changes: 2 additions & 2 deletions govtool/frontend/src/hooks/queries/useGetDRepListQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export const useGetDRepListQuery = () => {
const { data, isLoading } = useQuery({
queryKey: [
QUERY_KEYS.useGetDRepListKey,
pendingTransaction.registerAsSoleVoter ||
(pendingTransaction.registerAsSoleVoter ||
pendingTransaction.registerAsDrep ||
pendingTransaction.retireAsSoleVoter ||
pendingTransaction.retireAsDrep,
pendingTransaction.retireAsDrep)?.transactionHash,
],
queryFn: getDRepList,
});
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/hooks/queries/useGetDRepVotesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const useGetDRepVotesQuery = (filters: string[], sorting: string) => {
const { data, isLoading, refetch, isRefetching } = useQuery({
queryKey: [
QUERY_KEYS.useGetDRepVotesKey,
pendingTransaction.vote,
pendingTransaction.vote?.transactionHash,
filters,
sorting,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const useGetProposalsInfiniteQuery = ({
dRepID,
filters,
isEnabled,
pendingTransaction.vote,
pendingTransaction.vote?.transactionHash,
sorting,
],
fetchProposals,
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/hooks/queries/useGetProposalsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useGetProposalsQuery = ({
filters,
sorting,
dRepID,
pendingTransaction.vote,
pendingTransaction.vote?.transactionHash,
],
fetchProposals,
);
Expand Down
4 changes: 2 additions & 2 deletions govtool/frontend/src/hooks/queries/useGetVoterInfoQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export const useGetVoterInfo = () => {
const { data } = useQuery({
queryKey: [
QUERY_KEYS.useGetDRepInfoKey,
pendingTransaction.registerAsDrep ||
(pendingTransaction.registerAsDrep ||
pendingTransaction.registerAsSoleVoter ||
pendingTransaction.retireAsDrep ||
pendingTransaction.retireAsSoleVoter,
pendingTransaction.retireAsSoleVoter)?.transactionHash,
],
enabled: !!dRepID,
queryFn: () => getVoterInfo(dRepID),
Expand Down

0 comments on commit bb26945

Please sign in to comment.