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
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,22 @@ describe('TransactionController', () => {
);
});

it('skips approval if option explicitly false', async () => {
const controller = newController();

await controller.addTransaction(
{
from: ACCOUNT_MOCK,
to: ACCOUNT_MOCK,
},
{
requireApproval: false,
},
);

expect(delayMessengerMock.call).toHaveBeenCalledTimes(0);
});

it.each([
['mainnet', MOCK_MAINNET_NETWORK],
['custom network', MOCK_CUSTOM_NETWORK],
Expand Down
25 changes: 19 additions & 6 deletions packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,19 @@ export class TransactionController extends BaseController<
* @param opts - Additional options to control how the transaction is added.
* @param opts.deviceConfirmedOn - An enum to indicate what device confirmed the transaction.
* @param opts.origin - The origin of the transaction request, such as a dApp hostname.
* @param opts.requireApproval - Whether the transaction requires approval by the user, defaults to true unless explicitly disabled.
* @returns Object containing a promise resolving to the transaction hash if approved.
*/
async addTransaction(
transaction: Transaction,
{
deviceConfirmedOn,
origin,
requireApproval,
}: {
deviceConfirmedOn?: WalletDevice;
origin?: string;
requireApproval?: boolean | undefined;
} = {},
): Promise<Result> {
const { chainId, networkId } = this.getChainAndNetworkId();
Expand Down Expand Up @@ -383,7 +386,9 @@ export class TransactionController extends BaseController<
this.hub.emit(`unapprovedTransaction`, transactionMeta);

return {
result: this.processApproval(transactionMeta),
result: this.processApproval(transactionMeta, {
requireApproval,
}),
transactionMeta,
};
}
Expand Down Expand Up @@ -886,16 +891,24 @@ export class TransactionController extends BaseController<

private async processApproval(
transactionMeta: TransactionMeta,
{ shouldShowRequest = true } = {},
{
requireApproval,
shouldShowRequest = true,
}: {
requireApproval?: boolean | undefined;
shouldShowRequest?: boolean;
},
): Promise<string> {
const transactionId = transactionMeta.id;
let resultCallbacks: AcceptResultCallbacks | undefined;

try {
const acceptResult = await this.requestApproval(transactionMeta, {
shouldShowRequest,
});
resultCallbacks = acceptResult.resultCallbacks;
if (requireApproval !== false) {
const acceptResult = await this.requestApproval(transactionMeta, {
shouldShowRequest,
});
resultCallbacks = acceptResult.resultCallbacks;
}

const { meta, isCompleted } = this.isTransactionCompleted(transactionId);

Expand Down