Skip to content

Commit

Permalink
Merge pull request #553 from IntersectMBO/transaction-state
Browse files Browse the repository at this point in the history
[#522] fix bugs in usePendingTransaction
  • Loading branch information
jdyczka authored Mar 26, 2024
2 parents 55a0f24 + 28d782c commit bc5665d
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 86 deletions.
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

0 comments on commit bc5665d

Please sign in to comment.