Skip to content

If mined status is not mined and cancel reason not set, then show the cancel link, refactoring #66

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 2 commits into from
Apr 25, 2022
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
2 changes: 2 additions & 0 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const createStateAfterPending = () => {
{
uuid: 'uuid1',
status: 'pending',
cancellable: true,
statusMetadata: {
cancellationFeeWei: 0,
cancellationReason: 'not_cancelled',
Expand Down Expand Up @@ -179,6 +180,7 @@ const createStateAfterSuccess = () => {
{
uuid: 'uuid2',
status: 'success',
cancellable: false,
statusMetadata: {
cancellationFeeWei: 36777567771000,
cancellationReason: 'not_cancelled',
Expand Down
27 changes: 7 additions & 20 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ import {
generateHistoryEntry,
getStxProcessingTime,
handleFetch,
isSmartTransactionCancellable,
} from './utils';
import { CHAIN_IDS } from './constants';

const { safelyExecute } = util;

// TODO: JSDoc all methods
// TODO: Remove all comments (* ! ?)
const SECOND = 1000;
const MINUTE = SECOND * 60;

export const DEFAULT_INTERVAL = SECOND * 5;
export const CANCELLABLE_INTERVAL = MINUTE;

export interface SmartTransactionsControllerConfig extends BaseConfig {
interval: number;
Expand Down Expand Up @@ -447,10 +443,13 @@ export default class SmartTransactionsController extends BaseController<

const data = await this.fetch(url);

Object.entries(data).forEach(([uuid, smartTransaction]) => {
Object.entries(data).forEach(([uuid, stxStatus]) => {
this.updateSmartTransaction({
statusMetadata: smartTransaction as SmartTransactionsStatus,
status: calculateStatus(smartTransaction as SmartTransactionsStatus),
statusMetadata: stxStatus as SmartTransactionsStatus,
status: calculateStatus(stxStatus as SmartTransactionsStatus),
cancellable: isSmartTransactionCancellable(
stxStatus as SmartTransactionsStatus,
),
uuid,
});
});
Expand Down Expand Up @@ -590,18 +589,6 @@ export default class SmartTransactionsController extends BaseController<
cancellable: true,
});

setTimeout(() => {
if (!this.isNewSmartTransaction(data.uuid)) {
// Only do this for an existing smart transaction. If an STX is not in the list anymore
// (e.g. because it was cancelled and a new one was submitted, which deletes the first one),
// do not try to update the old one, because it would create a new one with most
// of the required STX params missing. It would only have "uuid" and "cancellable" params.
this.updateSmartTransaction({
uuid: data.uuid,
cancellable: false,
});
}
}, CANCELLABLE_INTERVAL);
nonceLock.releaseLock();
return data;
}
Expand Down
40 changes: 40 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,44 @@ describe('src/utils.js', () => {
expect(processingTime).toStrictEqual(3);
});
});

describe('isSmartTransactionCancellable', () => {
const createStxStatus = (customProps = {}) => {
return {
error: undefined,
minedTx: SmartTransactionMinedTx.NOT_MINED,
cancellationFeeWei: 10000,
cancellationReason: SmartTransactionCancellationReason.NOT_CANCELLED,
deadlineRatio: 10,
minedHash: undefined,
...customProps,
};
};

it('returns true if minedTx is NOT_MINED and cancellationReason is NOT_CANCELLED', () => {
const stxStatus = createStxStatus();
expect(utils.isSmartTransactionCancellable(stxStatus)).toBe(true);
});

it('returns true if minedTx is NOT_MINED and cancellationReason is undefined', () => {
const stxStatus = createStxStatus({
cancellationReason: undefined,
});
expect(utils.isSmartTransactionCancellable(stxStatus)).toBe(true);
});

it('returns false if minedTx is NOT_MINED and cancellationReason is USER_CANCELLED', () => {
const stxStatus = createStxStatus({
cancellationReason: SmartTransactionCancellationReason.USER_CANCELLED,
});
expect(utils.isSmartTransactionCancellable(stxStatus)).toBe(false);
});

it('returns false if minedTx is CANCELLED and cancellationReason is NOT_CANCELLED', () => {
const stxStatus = createStxStatus({
minedTx: SmartTransactionMinedTx.CANCELLED,
});
expect(utils.isSmartTransactionCancellable(stxStatus)).toBe(false);
});
});
});
37 changes: 24 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function isSmartTransactionPending(smartTransaction: SmartTransaction) {
}

export const isSmartTransactionStatusResolved = (
status: SmartTransactionsStatus | string,
) => status === 'uuid_not_found';
stxStatus: SmartTransactionsStatus | string,
) => stxStatus === 'uuid_not_found';

// TODO use actual url once API is defined
export function getAPIRequestURL(apiType: APIType, chainId: string): string {
Expand Down Expand Up @@ -53,8 +53,8 @@ export function getAPIRequestURL(apiType: APIType, chainId: string): string {
}
}

export const calculateStatus = (status: SmartTransactionsStatus) => {
if (isSmartTransactionStatusResolved(status)) {
export const calculateStatus = (stxStatus: SmartTransactionsStatus) => {
if (isSmartTransactionStatusResolved(stxStatus)) {
return SmartTransactionStatuses.RESOLVED;
}
const cancellations = [
Expand All @@ -64,28 +64,28 @@ export const calculateStatus = (status: SmartTransactionsStatus) => {
SmartTransactionCancellationReason.INVALID_NONCE,
SmartTransactionCancellationReason.USER_CANCELLED,
];
if (status?.minedTx === SmartTransactionMinedTx.NOT_MINED) {
if (stxStatus?.minedTx === SmartTransactionMinedTx.NOT_MINED) {
if (
status.cancellationReason ===
stxStatus.cancellationReason ===
SmartTransactionCancellationReason.NOT_CANCELLED
) {
return SmartTransactionStatuses.PENDING;
}

const isCancellation =
cancellations.findIndex(
(cancellation) => cancellation === status.cancellationReason,
(cancellation) => cancellation === stxStatus.cancellationReason,
) > -1;
if (status.cancellationReason && isCancellation) {
return cancellationReasonToStatusMap[status.cancellationReason];
if (stxStatus.cancellationReason && isCancellation) {
return cancellationReasonToStatusMap[stxStatus.cancellationReason];
}
} else if (status?.minedTx === SmartTransactionMinedTx.SUCCESS) {
} else if (stxStatus?.minedTx === SmartTransactionMinedTx.SUCCESS) {
return SmartTransactionStatuses.SUCCESS;
} else if (status?.minedTx === SmartTransactionMinedTx.CANCELLED) {
} else if (stxStatus?.minedTx === SmartTransactionMinedTx.CANCELLED) {
return SmartTransactionStatuses.CANCELLED;
} else if (status?.minedTx === SmartTransactionMinedTx.REVERTED) {
} else if (stxStatus?.minedTx === SmartTransactionMinedTx.REVERTED) {
return SmartTransactionStatuses.REVERTED;
} else if (status?.minedTx === SmartTransactionMinedTx.UNKNOWN) {
} else if (stxStatus?.minedTx === SmartTransactionMinedTx.UNKNOWN) {
return SmartTransactionStatuses.UNKNOWN;
}
return SmartTransactionStatuses.UNKNOWN;
Expand Down Expand Up @@ -172,3 +172,14 @@ export async function handleFetch(request: string, options?: RequestInit) {
}
return json;
}

export const isSmartTransactionCancellable = (
stxStatus: SmartTransactionsStatus,
): boolean => {
return (
stxStatus.minedTx === SmartTransactionMinedTx.NOT_MINED &&
(!stxStatus.cancellationReason ||
stxStatus.cancellationReason ===
SmartTransactionCancellationReason.NOT_CANCELLED)
);
};