Skip to content
Open
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
212 changes: 211 additions & 1 deletion src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ import {
type SmartTransactionsControllerMessenger,
} from './SmartTransactionsController';
import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
import { SmartTransactionStatuses, ClientId } from './types';
import {
SmartTransactionStatuses,
ClientId,
TransactionFeature,
TransactionKind,
} from './types';
import * as utils from './utils';

type AllActions = MessengerActions<SmartTransactionsControllerMessenger>;
Expand Down Expand Up @@ -979,6 +984,211 @@ describe('SmartTransactionsController', () => {
);
});
});

describe('trackingHeaders', () => {
it('sends X-Transaction-Feature and X-Transaction-Kind headers when trackingHeaders is provided', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
trackingHeaders: {
feature: TransactionFeature.Send,
kind: TransactionKind.STX,
},
});

expect(handleFetchSpy).toHaveBeenCalledWith(
expect.stringContaining('/submitTransactions'),
expect.objectContaining({
headers: expect.objectContaining({
'Content-Type': 'application/json',
'X-Client-Id': ClientId.Mobile,
'X-Transaction-Feature': 'Send',
'X-Transaction-Kind': 'STX',
}),
}),
);

handleFetchSpy.mockRestore();
});
});

it('sends only X-Transaction-Feature header when only feature is provided', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
trackingHeaders: {
feature: TransactionFeature.Swap,
},
});

expect(handleFetchSpy).toHaveBeenCalledWith(
expect.stringContaining('/submitTransactions'),
expect.objectContaining({
headers: expect.objectContaining({
'Content-Type': 'application/json',
'X-Client-Id': ClientId.Mobile,
'X-Transaction-Feature': 'Swap',
}),
}),
);

// Ensure X-Transaction-Kind is NOT in the headers
const [, options] = handleFetchSpy.mock.calls[0];
expect(options?.headers).not.toHaveProperty('X-Transaction-Kind');

handleFetchSpy.mockRestore();
});
});

it('sends only X-Transaction-Kind header when only kind is provided', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
trackingHeaders: {
kind: TransactionKind.GaslessSendBundle,
},
});

expect(handleFetchSpy).toHaveBeenCalledWith(
expect.stringContaining('/submitTransactions'),
expect.objectContaining({
headers: expect.objectContaining({
'Content-Type': 'application/json',
'X-Client-Id': ClientId.Mobile,
'X-Transaction-Kind': 'GaslessSendBundle',
}),
}),
);

// Ensure X-Transaction-Feature is NOT in the headers
const [, options] = handleFetchSpy.mock.calls[0];
expect(options?.headers).not.toHaveProperty('X-Transaction-Feature');

handleFetchSpy.mockRestore();
});
});

it('does not include tracking headers when trackingHeaders is not provided', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
// No trackingHeaders provided
});

const [, options] = handleFetchSpy.mock.calls[0];
expect(options?.headers).not.toHaveProperty('X-Transaction-Feature');
expect(options?.headers).not.toHaveProperty('X-Transaction-Kind');

handleFetchSpy.mockRestore();
});
});

it('supports all TransactionFeature values', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

// Test each feature value
const features = Object.values(TransactionFeature);
for (const feature of features) {
handleFetchSpy.mockClear();

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
trackingHeaders: { feature },
});

expect(handleFetchSpy).toHaveBeenCalledWith(
expect.stringContaining('/submitTransactions'),
expect.objectContaining({
headers: expect.objectContaining({
'X-Transaction-Feature': feature,
}),
}),
);
}

handleFetchSpy.mockRestore();
});
});

it('supports all TransactionKind values', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const handleFetchSpy = jest
.spyOn(utils, 'handleFetch')
.mockResolvedValue(submitTransactionsApiResponse);

// Test each kind value
const kinds = Object.values(TransactionKind);
for (const kind of kinds) {
handleFetchSpy.mockClear();

await controller.submitSignedTransactions({
signedTransactions: [signedTransaction],
txParams: createTxParams(),
trackingHeaders: { kind },
});

expect(handleFetchSpy).toHaveBeenCalledWith(
expect.stringContaining('/submitTransactions'),
expect.objectContaining({
headers: expect.objectContaining({
'X-Transaction-Kind': kind,
}),
}),
);
}

handleFetchSpy.mockRestore();
});
});
});
});

describe('fetchSmartTransactionsStatus', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
MetaMetricsProps,
FeatureFlags,
ClientId,
TransactionTrackingHeaders,
} from './types';
import { APIType, SmartTransactionStatuses } from './types';
import {
Expand Down Expand Up @@ -240,12 +241,22 @@
#trace: TraceCallback;

/* istanbul ignore next */
async #fetch(request: string, options?: RequestInit) {
async #fetch(
request: string,
options?: RequestInit,
trackingHeaders?: TransactionTrackingHeaders,
) {
const fetchOptions = {
...options,
headers: {
'Content-Type': 'application/json',
...(this.#clientId && { 'X-Client-Id': this.#clientId }),
...(trackingHeaders?.feature && {
'X-Transaction-Feature': trackingHeaders.feature,
}),
...(trackingHeaders?.kind && {
'X-Transaction-Kind': trackingHeaders.kind,
}),
},
};

Expand Down Expand Up @@ -335,9 +346,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

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

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

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

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

Expand All @@ -362,7 +373,7 @@
}

this.timeoutHandle = setInterval(() => {
safelyExecute(async () => this.updateSmartTransactions());

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

Check warning on line 376 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
}, this.#interval);
await safelyExecute(async () => this.updateSmartTransactions());
}
Expand Down Expand Up @@ -429,7 +440,7 @@
ethQuery = new EthQuery(provider);
}

this.#createOrUpdateSmartTransaction(smartTransaction, {

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

Check warning on line 443 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
chainId,
ethQuery,
});
Expand Down Expand Up @@ -846,12 +857,14 @@
signedTransactions,
signedCanceledTransactions = [],
networkClientId,
trackingHeaders,
}: {
signedTransactions: SignedTransaction[];
signedCanceledTransactions?: SignedCanceledTransaction[];
transactionMeta?: TransactionMeta;
txParams?: TransactionParams;
networkClientId?: NetworkClientId;
trackingHeaders?: TransactionTrackingHeaders;
}) {
const selectedNetworkClientId =
networkClientId ??
Expand All @@ -874,6 +887,7 @@
rawCancelTxs: signedCanceledTransactions,
}),
},
trackingHeaders,
),
);
const time = Date.now();
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export {
type IndividualTxFees,
type FeatureFlags,
type SmartTransaction,
type TransactionTrackingHeaders,
SmartTransactionMinedTx,
SmartTransactionCancellationReason,
SmartTransactionStatuses,
ClientId,
TransactionFeature,
TransactionKind,
} from './types';
export { MetaMetricsEventName, MetaMetricsEventCategory } from './constants';
export {
Expand Down
35 changes: 35 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,38 @@ export type FeatureFlags = {
extensionReturnTxHashAsap?: boolean;
};
};

/**
* Transaction feature - identifies which MetaMask feature initiated the transaction
*/
export enum TransactionFeature {
Swap = 'Swap',
Bridge = 'Bridge',
Send = 'Send',
Sell = 'Sell',
Perps = 'Perps',
Predictions = 'Predictions',
Card = 'Card',
Pay = 'Pay',
DAppTransaction = 'dAppTransaction',
Earn = 'Earn',
AccountUpgrade = 'AccountUpgrade',
}

/**
* Transaction kind - identifies the type of transaction mechanism used
*/
export enum TransactionKind {
Regular = 'Regular',
STX = 'STX',
GaslessSendBundle = 'GaslessSendBundle',
GaslessEIP7702 = 'GaslessEIP7702',
}

/**
* Optional headers for tracking transaction feature and kind
*/
export type TransactionTrackingHeaders = {
feature?: TransactionFeature;
kind?: TransactionKind;
};
Loading