Skip to content
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
70 changes: 70 additions & 0 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
import {
type TransactionParams,
TransactionStatus,
TransactionType,
} from '@metamask/transaction-controller';
import type { Hex } from '@metamask/utils';
import nock from 'nock';
Expand Down Expand Up @@ -1493,6 +1494,75 @@ describe('SmartTransactionsController', () => {
});
});

it('includes signedTransactionsWithMetadata in request body as rawTxsWithMetadata', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

const signedTransactionsWithMetadata = [
{
tx: signedTransaction,
metadata: {
txType: TransactionType.swap,
},
},
];

let requestBody: any;
nock(API_BASE_URL)
.post(
`/networks/${ethereumChainIdDec}/submitTransactions?stxControllerVersion=${packageJson.version}`,
(body) => {
requestBody = body;
return true;
},
)
.reply(200, submitTransactionsApiResponse);

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

// Verify the request includes rawTxsWithMetadata
expect(requestBody).toBeDefined();
expect(requestBody.rawTxsWithMetadata).toStrictEqual(
signedTransactionsWithMetadata,
);
});
});

it('omits rawTxsWithMetadata from request body when signedTransactionsWithMetadata is not provided', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
const submitTransactionsApiResponse =
createSubmitTransactionsApiResponse();

let requestBody: any;
nock(API_BASE_URL)
.post(
`/networks/${ethereumChainIdDec}/submitTransactions?stxControllerVersion=${packageJson.version}`,
(body) => {
requestBody = body;
return true;
},
)
.reply(200, submitTransactionsApiResponse);

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

// Verify the request does not include rawTxsWithMetadata
expect(requestBody).toBeDefined();
expect(requestBody.rawTxsWithMetadata).toBeUndefined();
});
});

it('works without txParams', async () => {
await withController(async ({ controller }) => {
const signedTransaction = createSignedTransaction();
Expand Down
4 changes: 4 additions & 0 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
IndividualTxFees,
SignedCanceledTransaction,
SignedTransaction,
SignedTransactionWithMetadata,
SmartTransaction,
SmartTransactionsStatus,
UnsignedTransaction,
Expand Down Expand Up @@ -388,9 +389,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

Check warning on line 392 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 392 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 394 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 394 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 @@ -415,7 +416,7 @@
}

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

Check warning on line 419 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 419 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 @@ -482,7 +483,7 @@
ethQuery = new EthQuery(provider);
}

this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 486 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 486 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 @@ -903,10 +904,12 @@
txParams,
signedTransactions,
signedCanceledTransactions = [],
signedTransactionsWithMetadata,
networkClientId,
}: {
signedTransactions: SignedTransaction[];
signedCanceledTransactions?: SignedCanceledTransaction[];
signedTransactionsWithMetadata?: SignedTransactionWithMetadata[];
transactionMeta?: TransactionMeta;
txParams?: TransactionParams;
networkClientId?: NetworkClientId;
Expand All @@ -930,6 +933,7 @@
body: JSON.stringify({
rawTxs: signedTransactions,
rawCancelTxs: signedCanceledTransactions,
rawTxsWithMetadata: signedTransactionsWithMetadata,
}),
},
),
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export {
type IndividualTxFees,
type FeatureFlags,
type SmartTransaction,
type SentinelMeta,
type SignedTransactionWithMetadata,
type SmartTransactionsNetworkConfig,
type SmartTransactionsFeatureFlagsConfig,
SmartTransactionMinedTx,
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { NetworkClientId } from '@metamask/network-controller';
import type { TransactionType } from '@metamask/transaction-controller';
import type { CaipChainId, Hex } from '@metamask/utils';

import type { SmartTransactionsNetworkConfig } from './featureFlags';

export type SentinelMeta = {
txType?: TransactionType;
};

/** API */
export enum APIType {
'GET_FEES',
Expand Down Expand Up @@ -126,6 +131,11 @@ export type UnsignedTransaction = any;
// TODO
export type SignedTransaction = any;

export type SignedTransactionWithMetadata = {
tx: string;
metadata?: SentinelMeta;
};

// TODO
export type SignedCanceledTransaction = any;

Expand Down
Loading