Skip to content

Commit

Permalink
Update metrics, refactoring (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan437 authored Jul 10, 2024
1 parent e3d25b7 commit b5ffebf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 47 deletions.
9 changes: 6 additions & 3 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,15 @@ describe('SmartTransactionsController', () => {
});

describe('trackStxStatusChange', () => {
it('does not track if no prevSmartTransactions', () => {
const smartTransaction = createStateAfterPending()[0];
it('tracks status change if prevSmartTransactions is undefined', () => {
const smartTransaction = {
...createStateAfterPending()[0],
swapMetaData: {},
};
smartTransactionsController.trackStxStatusChange(
smartTransaction as SmartTransaction,
);
expect(trackMetaMetricsEventSpy).not.toHaveBeenCalled();
expect(trackMetaMetricsEventSpy).toHaveBeenCalled();
});

it('does not track if smartTransaction and prevSmartTransaction have the same status', () => {
Expand Down
93 changes: 49 additions & 44 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import {
calculateStatus,
generateHistoryEntry,
getAPIRequestURL,
getStxProcessingTime,
handleFetch,
incrementNonceInHex,
isSmartTransactionCancellable,
isSmartTransactionPending,
replayHistory,
snapshotFromTxMeta,
getTxHash,
getSmartTransactionMetricsProperties,
} from './utils';

const SECOND = 1000;
Expand Down Expand Up @@ -303,38 +303,20 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
smartTransaction: SmartTransaction,
prevSmartTransaction?: SmartTransaction,
) {
if (!prevSmartTransaction) {
return; // Don't track the first STX, because it doesn't have all necessary params.
}

let updatedSmartTransaction = cloneDeep(smartTransaction);
updatedSmartTransaction = {
...cloneDeep(prevSmartTransaction),
...updatedSmartTransaction,
};

if (
!updatedSmartTransaction.swapMetaData ||
(updatedSmartTransaction.status === prevSmartTransaction.status &&
prevSmartTransaction.swapMetaData)
) {
if (updatedSmartTransaction.status === prevSmartTransaction?.status) {
return; // If status hasn't changed, don't track it again.
}

const sensitiveProperties = {
stx_status: updatedSmartTransaction.status,
token_from_symbol: updatedSmartTransaction.sourceTokenSymbol,
token_to_symbol: updatedSmartTransaction.destinationTokenSymbol,
processing_time: getStxProcessingTime(updatedSmartTransaction.time),
stx_enabled: true,
current_stx_enabled: true,
stx_user_opt_in: true,
};

this.trackMetaMetricsEvent({
event: MetaMetricsEventName.StxStatusUpdated,
category: MetaMetricsEventCategory.Transactions,
sensitiveProperties,
properties: getSmartTransactionMetricsProperties(updatedSmartTransaction),
});
}

Expand Down Expand Up @@ -363,13 +345,47 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
ethQuery = new EthQuery(networkClient.provider);
}

this.#updateSmartTransaction(smartTransaction, {
this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 348 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 348 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
chainId,
ethQuery,
});
}

async #updateSmartTransaction(
#updateSmartTransaction(
smartTransaction: SmartTransaction,
{
chainId = this.config.chainId,
}: {
chainId: Hex;
},
) {
const { smartTransactionsState } = this.state;
const { smartTransactions } = smartTransactionsState;
const currentSmartTransactions = smartTransactions[chainId] ?? [];
const currentIndex = currentSmartTransactions?.findIndex(
(stx) => stx.uuid === smartTransaction.uuid,
);
if (currentIndex === -1) {
return; // Smart transaction not found, don't update anything.
}
this.update({
smartTransactionsState: {
...smartTransactionsState,
smartTransactions: {
...smartTransactionsState.smartTransactions,
[chainId]: smartTransactionsState.smartTransactions[chainId].map(
(existingSmartTransaction, index) => {
return index === currentIndex
? { ...existingSmartTransaction, ...smartTransaction }
: existingSmartTransaction;
},
),
},
},
});
}

async #createOrUpdateSmartTransaction(
smartTransaction: SmartTransaction,
{
chainId = this.config.chainId,
Expand Down Expand Up @@ -451,20 +467,8 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
ethQuery,
});
} else {
this.update({
smartTransactionsState: {
...smartTransactionsState,
smartTransactions: {
...smartTransactionsState.smartTransactions,
[chainId]: smartTransactionsState.smartTransactions[chainId].map(
(item, index) => {
return index === currentIndex
? { ...item, ...smartTransaction }
: item;
},
),
},
},
this.#updateSmartTransaction(smartTransaction, {
chainId,
});
}
}
Expand Down Expand Up @@ -580,18 +584,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
baseFeePerGas,
);
}

this.trackMetaMetricsEvent({
event: MetaMetricsEventName.StxConfirmed,
category: MetaMetricsEventCategory.Transactions,
properties: getSmartTransactionMetricsProperties(smartTransaction),
});

this.#updateSmartTransaction(
{ ...smartTransaction, confirmed: true },
{
...smartTransaction,
confirmed: true,
chainId,
},
{ chainId, ethQuery },
);
}
} catch (error) {
Expand Down Expand Up @@ -630,7 +632,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
cancellable: isSmartTransactionCancellable(stxStatus),
uuid,
};
this.#updateSmartTransaction(smartTransaction, { chainId, ethQuery });
this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 635 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 635 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
chainId,
ethQuery,
});
});

return data;
Expand Down Expand Up @@ -784,7 +789,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
};

try {
this.#updateSmartTransaction(
this.#createOrUpdateSmartTransaction(

Check warning on line 792 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 792 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
{
chainId,
nonceDetails,
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,26 @@ export const getTxHash = (signedTxHex: any) => {
).hash();
return bytesToHex(txHashBytes);
};

export const getSmartTransactionMetricsProperties = (
smartTransaction: SmartTransaction,
) => {
if (!smartTransaction) {
return {};
}
const smartTransactionStatusMetadata = smartTransaction.statusMetadata;
return {
stx_status: smartTransaction.status,
token_from_symbol: smartTransaction.sourceTokenSymbol,
token_to_symbol: smartTransaction.destinationTokenSymbol,
type: smartTransaction.type,
processing_time: getStxProcessingTime(smartTransaction.time),
is_smart_transaction: true,
stx_enabled: true,
current_stx_enabled: true,
stx_user_opt_in: true,
stx_duplicated: smartTransactionStatusMetadata?.duplicated,
stx_timed_out: smartTransactionStatusMetadata?.timedOut,
stx_proxied: smartTransactionStatusMetadata?.proxied,
};
};

0 comments on commit b5ffebf

Please sign in to comment.