Skip to content
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

[#522] fix bugs in usePendingTransaction #553

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions govtool/frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
"operator-linebreak": "off",
"implicit-arrow-linebreak": "off",
"consistent-return": "off",
"no-console": ["error", { allow: ["warn", "error"] }],
"no-shadow": "off",
"function-paren-newline": "off",
"object-curly-newline": "off",
Expand Down
1 change: 0 additions & 1 deletion govtool/frontend/src/context/getUtxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export const getUtxos = async (
return utxos;
} catch (err) {
Sentry.captureException(err);
// eslint-disable-next-line no-console
console.error(err);
}
};
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 All @@ -105,13 +100,13 @@ export const usePendingTransaction = ({
await wait(DB_SYNC_REFRESH_TIME);
}
}

if (isTransactionExpired(transaction.time)) {
addErrorAlert(t(`alerts.${type}.failed`));
resetTransaction();
}
}
}

if (isTransactionExpired(transaction.time)) {
addErrorAlert(t(`alerts.${type}.failed`));
resetTransaction();
}
};

let interval = setInterval(checkTransaction, TRANSACTION_REFRESH_TIME);
Expand All @@ -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
Loading
Loading