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

feat: Hide the smart transaction status page if we return a txHash asap #12622

Merged
merged 1 commit into from
Dec 11, 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
32 changes: 24 additions & 8 deletions app/util/smart-transactions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,37 +283,53 @@ describe('Smart Transactions utils', () => {
});
describe('getShouldStartFlow', () => {
it('returns true for Send transaction', () => {
const res = getShouldStartApprovalRequest(false, true, false, false);
const res = getShouldStartApprovalRequest(false, true, false, false, false);
expect(res).toBe(true);
});
it('returns false for Send transaction when mobileReturnTxHashAsap is true', () => {
const res = getShouldStartApprovalRequest(false, true, false, false, true);
expect(res).toBe(false);
});
it('returns true for Dapp transaction', () => {
const res = getShouldStartApprovalRequest(true, false, false, false);
const res = getShouldStartApprovalRequest(true, false, false, false, false);
expect(res).toBe(true);
});
it('returns false for Dapp transaction when mobileReturnTxHashAsap is true', () => {
const res = getShouldStartApprovalRequest(true, false, false, false, true);
expect(res).toBe(false);
});
it('returns true for Swap approve transaction', () => {
const res = getShouldStartApprovalRequest(false, false, true, false);
const res = getShouldStartApprovalRequest(false, false, true, false, false);
expect(res).toBe(true);
});
it('returns false for Swap transaction', () => {
const res = getShouldStartApprovalRequest(false, false, false, true);
const res = getShouldStartApprovalRequest(false, false, false, true, false);
expect(res).toBe(false);
});
});
describe('getShouldUpdateFlow', () => {
it('returns true for Send transaction', () => {
const res = getShouldUpdateApprovalRequest(false, true, false);
const res = getShouldUpdateApprovalRequest(false, true, false, false);
expect(res).toBe(true);
});
it('returns false for Send transaction when mobileReturnTxHashAsap is true', () => {
const res = getShouldUpdateApprovalRequest(false, true, false, true);
expect(res).toBe(false);
});
it('returns true for Dapp transaction', () => {
const res = getShouldUpdateApprovalRequest(true, false, false);
const res = getShouldUpdateApprovalRequest(true, false, false, false);
expect(res).toBe(true);
});
it('returns false for Dapp transaction when mobileReturnTxHashAsap is true', () => {
const res = getShouldUpdateApprovalRequest(true, false, false, true);
expect(res).toBe(false);
});
it('returns true for Swap transaction', () => {
const res = getShouldUpdateApprovalRequest(false, false, true);
const res = getShouldUpdateApprovalRequest(false, false, true, false);
expect(res).toBe(true);
});
it('returns false for Swap approve transaction', () => {
const res = getShouldUpdateApprovalRequest(false, false, false);
const res = getShouldUpdateApprovalRequest(false, false, false, false);
expect(res).toBe(false);
});
});
Expand Down
8 changes: 6 additions & 2 deletions app/util/smart-transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ export const getShouldStartApprovalRequest = (
isSend: boolean,
isSwapApproveTx: boolean,
hasPendingApprovalForSwapApproveTx: boolean,
mobileReturnTxHashAsap: boolean,
): boolean =>
isDapp || isSend || isSwapApproveTx || !hasPendingApprovalForSwapApproveTx;
!mobileReturnTxHashAsap &&
(isDapp || isSend || isSwapApproveTx || !hasPendingApprovalForSwapApproveTx);

export const getShouldUpdateApprovalRequest = (
isDapp: boolean,
isSend: boolean,
isSwapTransaction: boolean,
): boolean => isDapp || isSend || isSwapTransaction;
mobileReturnTxHashAsap: boolean,
): boolean =>
!mobileReturnTxHashAsap && (isDapp || isSend || isSwapTransaction);

const waitForSmartTransactionConfirmationDone = (
controllerMessenger: ControllerMessenger,
Expand Down
63 changes: 63 additions & 0 deletions app/util/smart-transactions/smart-publish-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,69 @@ describe('submitSmartTransactionHook', () => {
);
});

it('submits a smart transaction without the smart transaction status page', async () => {
withRequest(
async ({ request, controllerMessenger, submitSignedTransactionsSpy }) => {
request.featureFlags.smartTransactions.mobileReturnTxHashAsap = true;
setImmediate(() => {
controllerMessenger.publish(
'SmartTransactionsController:smartTransaction',
{
status: 'pending',
statusMetadata: {
minedHash: '',
},
uuid: 'uuid',
} as SmartTransaction,
);

controllerMessenger.publish(
'SmartTransactionsController:smartTransaction',
{
status: 'success',
statusMetadata: {
minedHash: transactionHash,
},
uuid: 'uuid',
} as SmartTransaction,
);
});
const result = await submitSmartTransactionHook(request);

expect(result).toEqual({ transactionHash });
const { txParams, chainId } = request.transactionMeta;

expect(
request.transactionController.approveTransactionsWithSameNonce,
).toHaveBeenCalledWith(
[
{
...txParams,
maxFeePerGas: '0x2fd8a58d7',
maxPriorityFeePerGas: '0xaa0f8a94',
chainId,
value: undefined,
},
],
{ hasNonce: true },
);
expect(submitSignedTransactionsSpy).toHaveBeenCalledWith({
signedTransactions: [createSignedTransaction()],
signedCanceledTransactions: [],
txParams,
transactionMeta: request.transactionMeta,
});

expect(
request.approvalController.addAndShowApprovalRequest,
).not.toHaveBeenCalled();
expect(
request.approvalController.updateRequestState,
).not.toHaveBeenCalled();
},
);
});

describe('MM Swaps', () => {
it('starts an approval and does not end it if there is an swap tx that requires allowance', async () => {
withRequest(
Expand Down
13 changes: 7 additions & 6 deletions app/util/smart-transactions/smart-publish-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class SmartTransactionHook {

#shouldStartApprovalRequest: boolean;
#shouldUpdateApprovalRequest: boolean;
#mobileReturnTxHashAsap: boolean;

constructor(request: SubmitSmartTransactionRequest) {
const {
Expand All @@ -116,6 +117,8 @@ class SmartTransactionHook {
this.#chainId = transactionMeta.chainId;
this.#txParams = transactionMeta.txParams;
this.#controllerMessenger = controllerMessenger;
this.#mobileReturnTxHashAsap =
this.#featureFlags?.smartTransactions?.mobileReturnTxHashAsap ?? false;

const {
isDapp,
Expand Down Expand Up @@ -143,11 +146,13 @@ class SmartTransactionHook {
this.#isSend,
this.#isSwapApproveTx,
Boolean(approvalIdForPendingSwapApproveTx),
this.#mobileReturnTxHashAsap,
);
this.#shouldUpdateApprovalRequest = getShouldUpdateApprovalRequest(
this.#isDapp,
this.#isSend,
this.#isSwapTransaction,
this.#mobileReturnTxHashAsap,
);
}

Expand Down Expand Up @@ -221,9 +226,7 @@ class SmartTransactionHook {
);
throw error;
} finally {
const mobileReturnTxHashAsap =
this.#featureFlags?.smartTransactions?.mobileReturnTxHashAsap;
if (!mobileReturnTxHashAsap) {
if (!this.#mobileReturnTxHashAsap) {
this.#cleanup();
}
}
Expand Down Expand Up @@ -266,10 +269,8 @@ class SmartTransactionHook {
uuid: string,
) => {
let transactionHash: string | undefined | null;
const mobileReturnTxHashAsap =
this.#featureFlags?.smartTransactions?.mobileReturnTxHashAsap;

if (mobileReturnTxHashAsap && submitTransactionResponse?.txHash) {
if (this.#mobileReturnTxHashAsap && submitTransactionResponse?.txHash) {
transactionHash = submitTransactionResponse.txHash;
} else {
transactionHash = await this.#waitForTransactionHash({
Expand Down
Loading