Skip to content

Commit 8eca356

Browse files
committed
test: v2
1 parent 1d63ea1 commit 8eca356

File tree

9 files changed

+327
-348
lines changed

9 files changed

+327
-348
lines changed

packages/bridge-status-controller/src/bridge-status-controller.ts

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
MAX_ATTEMPTS,
3535
REFRESH_INTERVAL_MS,
3636
} from './constants';
37-
import selectSubmitStrategy from './submit-flows';
37+
import { executeApproval, executeBatch, executeTrade } from './submit-flows';
3838
import type {
3939
BridgeStatusControllerState,
4040
StartPollingForBridgeTxStatusArgsSerialized,
@@ -890,9 +890,12 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
890890

891891
const startTime = Date.now();
892892

893-
const tradeMeta = await this.#trace(
893+
return await this.#trace(
894894
getTraceParams(quoteResponse, isStxEnabledOnClient),
895-
async (): Promise<TransactionMeta | undefined> => {
895+
async () => {
896+
let tradeTxMeta: TransactionMeta | undefined;
897+
let approvalTxId: string | undefined;
898+
896899
try {
897900
const isBridgeTx = isCrossChain(
898901
quoteResponse.quote.srcChainId,
@@ -917,12 +920,6 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
917920
[formatChainIdToHex(quoteResponse.quote.srcChainId)],
918921
);
919922

920-
const submitStrategy = selectSubmitStrategy({
921-
quoteResponse,
922-
isStxEnabledOnClient,
923-
isDelegatedAccount,
924-
});
925-
926923
const params = {
927924
quoteResponse,
928925
isStxEnabledOnClient,
@@ -937,39 +934,71 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
937934
fetchFn: this.#fetchFn,
938935
bridgeApiBaseUrl: this.#config.customBridgeApiBaseUrl,
939936
};
940-
const execute = submitStrategy.execute(params);
941-
942-
let tradeTxMeta: TransactionMeta | undefined;
943-
944-
// Each submission strategy determines when to return values, which means these values can be returned in any order
945-
for await (const value of execute) {
946-
if (value.historyItem) {
947-
this.#addTxToHistory({
948-
...value.historyItem,
949-
quoteResponse,
950-
accountAddress: selectedAccount.address,
951-
isStxEnabled: isStxEnabledOnClient,
952-
startTime,
953-
location,
954-
abTests,
955-
activeAbTests,
956-
slippagePercentage: 0, // TODO include slippage provided by quote if using dynamic slippage, or slippage from quote request
957-
});
958-
}
959-
if (value.tradeMeta) {
960-
this.#rekeyHistoryItem(actionId, value.tradeMeta);
961-
tradeTxMeta = value.tradeMeta;
962-
}
963-
if (value.pollingToken) {
964-
this.#startPollingForTxId(value.pollingToken);
965-
}
966-
if (value.completedEventPayload) {
967-
this.#trackUnifiedSwapBridgeEvent(
968-
UnifiedSwapBridgeEventName.Completed,
969-
value.completedEventPayload,
970-
);
971-
}
937+
938+
const partialHistoryItem = {
939+
quoteResponse,
940+
accountAddress: selectedAccount.address,
941+
isStxEnabled: isStxEnabledOnClient,
942+
startTime,
943+
location,
944+
abTests,
945+
activeAbTests,
946+
slippagePercentage: 0, // TODO include slippage provided by quote if using dynamic slippage, or slippage from quote request
947+
};
948+
949+
const {
950+
tradeMeta: batchTradeTxMeta,
951+
approvalTxId: batchApprovalTxId,
952+
} = await executeBatch(params);
953+
if (batchApprovalTxId) {
954+
approvalTxId = batchApprovalTxId;
955+
}
956+
if (batchTradeTxMeta) {
957+
tradeTxMeta = batchTradeTxMeta;
958+
this.#addTxToHistory({
959+
bridgeTxMeta: tradeTxMeta,
960+
approvalTxId,
961+
...partialHistoryItem,
962+
});
963+
}
964+
965+
const { approvalTxId: approvalTxIdResult } =
966+
await executeApproval(params);
967+
if (approvalTxIdResult) {
968+
approvalTxId = approvalTxIdResult;
969+
this.#addTxToHistory({
970+
approvalTxId,
971+
...partialHistoryItem,
972+
});
973+
}
974+
975+
const {
976+
tradeMeta: tradeTxMetaResult,
977+
pollingToken,
978+
completedEventPayload,
979+
} = await executeTrade(params);
980+
if (tradeTxMetaResult) {
981+
tradeTxMeta = tradeTxMetaResult;
982+
this.#rekeyHistoryItem(actionId, tradeTxMetaResult);
983+
// TODO this should onkly happen if there was no prior approval, check for actionId
984+
this.#addTxToHistory({
985+
approvalTxId,
986+
bridgeTxMeta: tradeTxMeta,
987+
...partialHistoryItem,
988+
});
989+
}
990+
991+
if (pollingToken) {
992+
this.#startPollingForTxId(pollingToken);
993+
}
994+
if (completedEventPayload) {
995+
this.#trackUnifiedSwapBridgeEvent(
996+
UnifiedSwapBridgeEventName.Completed,
997+
tradeTxMeta?.id,
998+
completedEventPayload,
999+
);
9721000
}
1001+
9731002
return tradeTxMeta;
9741003
} catch (error) {
9751004
!quoteResponse.featureId &&
@@ -982,16 +1011,13 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
9821011
},
9831012
);
9841013
}
1014+
if (!tradeTxMeta) {
1015+
throw new Error(
1016+
'Failed to submit cross-chain swap transaction: no trade meta found',
1017+
);
1018+
}
9851019
},
9861020
);
987-
988-
if (!tradeMeta) {
989-
throw new Error(
990-
'Failed to submit cross-chain swap transaction: no trade meta found',
991-
);
992-
}
993-
994-
return tradeMeta;
9951021
};
9961022

9971023
/**

packages/bridge-status-controller/src/submit-flows/batch.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
33
import { isEvmTxData } from '@metamask/bridge-controller';
44

5-
import {
6-
SubmissionResultGenerator,
7-
MatchesFlow,
8-
SubmitStrategy,
9-
} from './types';
5+
import { MatchesFlow, SubmitStrategy, ExecuteParams } from './types';
106
import {
117
addTransactionBatch,
128
getAddTransactionBatchParams,
@@ -17,6 +13,10 @@ const matchesFlow: MatchesFlow = ({
1713
isStxEnabledOnClient,
1814
isDelegatedAccount,
1915
}) => {
16+
// TODO support intent approvals
17+
if (quoteResponse.quote.intent) {
18+
return false;
19+
}
2020
return (
2121
isStxEnabledOnClient ||
2222
quoteResponse.quote.gasIncluded7702 ||
@@ -32,14 +32,14 @@ const matchesFlow: MatchesFlow = ({
3232
* @param args.quoteResponse - The quote response
3333
* @param args.messenger - The messenger
3434
* @param args.requireApproval - Whether to require approval for the transaction
35-
* @yields The approvalMeta and tradeMeta for the batched transaction
35+
* @returns The approvalMeta and tradeMeta for the batched transaction
3636
*/
37-
async function* submitBatchHandler({
37+
const executeBatch = async ({
3838
requireApproval,
3939
quoteResponse,
4040
messenger,
4141
isBridgeTx,
42-
}: Parameters<SubmissionResultGenerator>[0]) {
42+
}: ExecuteParams) => {
4343
if (!isEvmTxData(quoteResponse.trade)) {
4444
throw new Error(
4545
'Failed to submit cross-chain swap transaction: trade is not an EVM transaction',
@@ -62,18 +62,13 @@ async function* submitBatchHandler({
6262
messenger,
6363
transactionParams,
6464
);
65-
yield {
65+
return {
6666
tradeMeta,
67-
historyItem: {
68-
approvalTxId: approvalMeta?.id,
69-
bridgeTxMeta: tradeMeta, // Only the id field is used by the BridgeStatusController
70-
quoteResponse,
71-
slippagePercentage: 0, // TODO include slippage provided by quote if using dynamic slippage, or slippage from quote request
72-
},
67+
approvalTxId: approvalMeta?.id,
7368
};
74-
}
69+
};
7570

7671
export const submitBatch: SubmitStrategy = {
7772
matchesFlow,
78-
execute: submitBatchHandler,
73+
executeBatch,
7974
};
Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
12
/* eslint-disable @typescript-eslint/explicit-function-return-type */
23
import { isEvmTxData } from '@metamask/bridge-controller';
34
import { TransactionType } from '@metamask/transaction-controller';
45

5-
import {
6-
SubmissionResultGenerator,
7-
SubmitStrategy,
8-
ExecuteParams,
9-
} from './types';
6+
import { SubmitStrategy, ExecuteParams, MatchesFlow } from './types';
107
import { getApprovalTraceParams } from '../utils/trace';
118
import {
129
handleApprovalDelay,
1310
handleMobileHardwareWalletDelay,
1411
submitEvmTransaction,
1512
} from '../utils/transaction';
1613

14+
const matchesFlow: MatchesFlow = ({
15+
quoteResponse,
16+
isStxEnabledOnClient,
17+
isDelegatedAccount,
18+
}) => {
19+
return !(
20+
isStxEnabledOnClient ||
21+
quoteResponse.quote.gasIncluded7702 ||
22+
isDelegatedAccount
23+
);
24+
};
25+
1726
export const handleApprovalTx = async ({
1827
quoteResponse,
1928
messenger,
@@ -28,6 +37,7 @@ export const handleApprovalTx = async ({
2837
if (!approval || !isEvmTxData(approval)) {
2938
return {};
3039
}
40+
3141
if (resetApproval) {
3242
await submitEvmTransaction({
3343
messenger,
@@ -50,41 +60,15 @@ export const handleApprovalTx = async ({
5060
return { approvalTxId: approvalTxMeta?.id };
5161
};
5262

53-
async function* submitEvmHandler(
54-
...args: Parameters<SubmissionResultGenerator>
55-
) {
56-
const {
57-
quoteResponse,
58-
messenger,
59-
traceFn,
60-
isBridgeTx,
61-
requireApproval,
62-
actionId,
63-
isStxEnabledOnClient,
64-
} = args[0];
63+
const handleTradeTx = async (args: ExecuteParams) => {
64+
const { quoteResponse, messenger, isBridgeTx, requireApproval, actionId } =
65+
args;
6566
if (!isEvmTxData(quoteResponse.trade)) {
6667
throw new Error(
6768
'Failed to submit cross-chain swap transaction: trade is not an EVM transaction',
6869
);
6970
}
7071

71-
// Set approval time and id if an approval tx is needed
72-
const { approvalTxId } = await traceFn(
73-
getApprovalTraceParams(quoteResponse, isStxEnabledOnClient),
74-
async () => {
75-
return await handleApprovalTx(...args);
76-
},
77-
);
78-
79-
// Add pre-submission history keyed by actionId
80-
// This ensures we have quote data available if transaction fails during submission
81-
// Set approval time and id if an approval tx is needed
82-
yield {
83-
historyItem: {
84-
approvalTxId,
85-
},
86-
};
87-
8872
// Pass txFee when gasIncluded is true to use the quote's gas fees
8973
// instead of re-estimating (which would fail for max native token swaps)
9074
const tradeMeta = await submitEvmTransaction({
@@ -98,10 +82,15 @@ async function* submitEvmHandler(
9882
actionId,
9983
});
10084

101-
yield { approvalTxId, tradeMeta };
102-
}
85+
return { tradeMeta };
86+
};
10387

10488
export const defaultSubmitHandler: SubmitStrategy = {
105-
matchesFlow: () => true,
106-
execute: submitEvmHandler,
89+
matchesFlow,
90+
executeApproval: async (args) =>
91+
await args.traceFn(
92+
getApprovalTraceParams(args.quoteResponse, args.isStxEnabledOnClient),
93+
async () => handleApprovalTx(args),
94+
),
95+
executeTrade: handleTradeTx,
10796
};

0 commit comments

Comments
 (0)