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

Save new event props to a newly created smart transaction, use both properties and sensitiveProperties for events #386

Merged
merged 2 commits into from
Jul 17, 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
22 changes: 17 additions & 5 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@
throw new Error('Invalid network client id');
}
}),
getMetaMetricsProps: jest.fn(async () => {
return Promise.resolve({
accountHardwareType: 'Ledger Hardware',
accountType: 'hardware',
deviceModel: 'ledger',
});
}),
});
// eslint-disable-next-line jest/prefer-spy-on
smartTransactionsController.subscribe = jest.fn();
Expand Down Expand Up @@ -761,17 +768,22 @@
`/networks/${ethereumChainIdDec}/submitTransactions?stxControllerVersion=${packageJson.version}`,
)
.reply(200, submitTransactionsApiResponse);

await smartTransactionsController.submitSignedTransactions({
signedTransactions: [signedTransaction],
signedCanceledTransactions: [signedCanceledTransaction],
txParams: createTxParams(),
});

expect(
const submittedSmartTransaction =
smartTransactionsController.state.smartTransactionsState
.smartTransactions[ChainId.mainnet][0].uuid,
).toBe('dP23W7c2kt4FK9TmXOkz1UM2F20');
.smartTransactions[ChainId.mainnet][0];
expect(submittedSmartTransaction.uuid).toBe(
'dP23W7c2kt4FK9TmXOkz1UM2F20',
);
expect(submittedSmartTransaction.accountHardwareType).toBe(
'Ledger Hardware',
);
expect(submittedSmartTransaction.accountType).toBe('hardware');
expect(submittedSmartTransaction.deviceModel).toBe('ledger');
});
});

Expand Down Expand Up @@ -1215,13 +1227,13 @@

describe('setStatusRefreshInterval', () => {
it('sets refresh interval if different', () => {
smartTransactionsController.setStatusRefreshInterval(100);

Check warning on line 1230 in src/SmartTransactionsController.test.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 1230 in src/SmartTransactionsController.test.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
expect(smartTransactionsController.config.interval).toBe(100);
});

it('does not set refresh interval if they are the same', () => {
const configureSpy = jest.spyOn(smartTransactionsController, 'configure');
smartTransactionsController.setStatusRefreshInterval(DEFAULT_INTERVAL);

Check warning on line 1236 in src/SmartTransactionsController.test.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 1236 in src/SmartTransactionsController.test.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
expect(configureSpy).toHaveBeenCalledTimes(0);
});
});
Expand Down
34 changes: 28 additions & 6 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SmartTransactionsStatus,
UnsignedTransaction,
GetTransactionsOptions,
MetaMetricsProps,
} from './types';
import { APIType, SmartTransactionStatuses } from './types';
import {
Expand All @@ -42,6 +43,7 @@
snapshotFromTxMeta,
getTxHash,
getSmartTransactionMetricsProperties,
getSmartTransactionMetricsSensitiveProperties,
} from './utils';

const SECOND = 1000;
Expand Down Expand Up @@ -100,6 +102,8 @@

private readonly getNetworkClientById: NetworkController['getNetworkClientById'];

private readonly getMetaMetricsProps: () => Promise<MetaMetricsProps>;

/* istanbul ignore next */
private async fetch(request: string, options?: RequestInit) {
const { clientId } = this.config;
Expand All @@ -123,6 +127,7 @@
getTransactions,
trackMetaMetricsEvent,
getNetworkClientById,
getMetaMetricsProps,
}: {
onNetworkStateChange: (
listener: (networkState: NetworkState) => void,
Expand All @@ -133,6 +138,7 @@
getTransactions: (options?: GetTransactionsOptions) => TransactionMeta[];
trackMetaMetricsEvent: any;
getNetworkClientById: NetworkController['getNetworkClientById'];
getMetaMetricsProps: () => Promise<MetaMetricsProps>;
},
config?: Partial<SmartTransactionsControllerConfig>,
state?: Partial<SmartTransactionsControllerState>,
Expand Down Expand Up @@ -181,6 +187,7 @@
this.getRegularTransactions = getTransactions;
this.trackMetaMetricsEvent = trackMetaMetricsEvent;
this.getNetworkClientById = getNetworkClientById;
this.getMetaMetricsProps = getMetaMetricsProps;

this.initializeSmartTransactionsForChainId();
this.#ensureUniqueSmartTransactions();
Expand Down Expand Up @@ -220,9 +227,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

Check warning on line 230 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 230 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
} else if (this.timeoutHandle && pendingTransactions?.length === 0) {
this.stop();

Check warning on line 232 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 232 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
}
}

Expand Down Expand Up @@ -280,7 +287,7 @@
return;
}
this.timeoutHandle = setInterval(() => {
safelyExecute(async () => this.updateSmartTransactions());

Check warning on line 290 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 290 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
}, this.config.interval);
await safelyExecute(async () => this.updateSmartTransactions());
}
Expand Down Expand Up @@ -316,7 +323,9 @@
this.trackMetaMetricsEvent({
event: MetaMetricsEventName.StxStatusUpdated,
category: MetaMetricsEventCategory.Transactions,
properties: getSmartTransactionMetricsProperties(updatedSmartTransaction),
properties: getSmartTransactionMetricsProperties(smartTransaction),
sensitiveProperties:
getSmartTransactionMetricsSensitiveProperties(smartTransaction),
});
}

Expand Down Expand Up @@ -345,7 +354,7 @@
ethQuery = new EthQuery(networkClient.provider);
}

this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 357 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 357 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,
});
Expand Down Expand Up @@ -385,6 +394,16 @@
});
}

async #addMetaMetricsPropsToNewSmartTransaction(
smartTransaction: SmartTransaction,
) {
const metaMetricsProps = await this.getMetaMetricsProps();
smartTransaction.accountHardwareType =
metaMetricsProps?.accountHardwareType;
smartTransaction.accountType = metaMetricsProps?.accountType;
smartTransaction.deviceModel = metaMetricsProps?.deviceModel;
}

async #createOrUpdateSmartTransaction(
smartTransaction: SmartTransaction,
{
Expand Down Expand Up @@ -416,6 +435,7 @@
);

if (isNewSmartTransaction) {
await this.#addMetaMetricsPropsToNewSmartTransaction(smartTransaction);
// add smart transaction
const cancelledNonceIndex = currentSmartTransactions?.findIndex(
(stx: SmartTransaction) =>
Expand Down Expand Up @@ -487,7 +507,7 @@
.map((smartTransaction) => smartTransaction.uuid);

if (transactionsToUpdate.length > 0) {
this.fetchSmartTransactionsStatus(transactionsToUpdate, {

Check warning on line 510 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 510 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
networkClientId,
});
}
Expand Down Expand Up @@ -588,6 +608,8 @@
event: MetaMetricsEventName.StxConfirmed,
category: MetaMetricsEventCategory.Transactions,
properties: getSmartTransactionMetricsProperties(smartTransaction),
sensitiveProperties:
getSmartTransactionMetricsSensitiveProperties(smartTransaction),
});
this.#updateSmartTransaction(
{ ...smartTransaction, confirmed: true },
Expand Down Expand Up @@ -625,18 +647,18 @@
SmartTransactionsStatus
>;

Object.entries(data).forEach(([uuid, stxStatus]) => {
const smartTransaction = {
for (const [uuid, stxStatus] of Object.entries(data)) {
const smartTransaction: SmartTransaction = {
statusMetadata: stxStatus,
status: calculateStatus(stxStatus),
cancellable: isSmartTransactionCancellable(stxStatus),
uuid,
};
this.#createOrUpdateSmartTransaction(smartTransaction, {
await this.#createOrUpdateSmartTransaction(smartTransaction, {
chainId,
ethQuery,
});
});
}

return data;
}
Expand All @@ -649,7 +671,7 @@
nonceLock.releaseLock();
return {
...transaction,
nonce: `0x${nonce.toString(16)}`,

Check warning on line 674 in src/SmartTransactionsController.ts

View workflow job for this annotation

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

Invalid type "any" of template literal expression

Check warning on line 674 in src/SmartTransactionsController.ts

View workflow job for this annotation

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

Invalid type "any" of template literal expression
};
}

Expand Down Expand Up @@ -789,7 +811,7 @@
};

try {
this.#createOrUpdateSmartTransaction(
await this.#createOrUpdateSmartTransaction(
{
chainId,
nonceDetails,
Expand Down
3 changes: 3 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('default export', () => {
getTransactions: jest.fn(),
trackMetaMetricsEvent: jest.fn(),
getNetworkClientById: jest.fn(),
getMetaMetricsProps: jest.fn(async () => {
return Promise.resolve({});
}),
});
expect(controller).toBeInstanceOf(SmartTransactionsController);
jest.clearAllTimers();
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export type SmartTransaction = {
type?: string;
confirmed?: boolean;
cancellable?: boolean;
accountHardwareType?: string;
accountType?: string;
deviceModel?: string;
};

export type Fee = {
Expand Down Expand Up @@ -131,3 +134,9 @@ export type GetTransactionsOptions = {
filterToCurrentNetwork?: boolean;
limit?: number;
};

export type MetaMetricsProps = {
accountHardwareType?: string;
accountType?: string;
deviceModel?: string;
};
17 changes: 15 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ export const getSmartTransactionMetricsProperties = (
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,
Expand All @@ -250,3 +248,18 @@ export const getSmartTransactionMetricsProperties = (
stx_proxied: smartTransactionStatusMetadata?.proxied,
};
};

export const getSmartTransactionMetricsSensitiveProperties = (
smartTransaction: SmartTransaction,
) => {
if (!smartTransaction) {
return {};
}
return {
token_from_symbol: smartTransaction.sourceTokenSymbol,
token_to_symbol: smartTransaction.destinationTokenSymbol,
account_hardware_type: smartTransaction.accountHardwareType,
account_type: smartTransaction.accountType,
device_model: smartTransaction.deviceModel,
};
};
Loading