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
1 change: 1 addition & 0 deletions packages/bridge-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Export `getQuoteRefreshRate` util that reads from the client's LaunchDarkly config ([#11181](https://github.com/MetaMask/core/pull/11181))
- Set QuoteRequest parameters based on `isMoneyAccount` value provided by clients. When isMoneyAcount=true, only request `relay` quotes from the backend ([#11602](https://github.com/MetaMask/core/pull/11602))
- Export getAccountByAddress and getFeatureFlags utils ([#11403](https://github.com/MetaMask/core/pull/11403))

Expand Down
8 changes: 2 additions & 6 deletions packages/bridge-controller/src/bridge-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { getAccountByAddress } from './utils/controllers/accounts.js';
import { getNetworkClientByChainId } from './utils/controllers/network.js';
import {
getBridgeFeatureFlags,
getQuoteRefreshRate,
hasMinimumRequiredVersion,
} from './utils/feature-flags.js';
import {
Expand Down Expand Up @@ -835,13 +836,8 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
// Assume that BatchSell quote requests all have the same source chain
// Use the first one to determine refresh rate
const { srcChainId } = state.quoteRequest[0];
const bridgeFeatureFlags = getBridgeFeatureFlags(this.messenger);

const refreshRateOverride = srcChainId
? bridgeFeatureFlags.chains[formatChainIdToCaip(srcChainId)]?.refreshRate
: undefined;
const defaultRefreshRate = bridgeFeatureFlags.refreshRate;
this.setIntervalLength(refreshRateOverride ?? defaultRefreshRate);
this.setIntervalLength(getQuoteRefreshRate(this.messenger, srcChainId));
};

readonly #fetchBridgeQuotes = async ({
Expand Down
5 changes: 4 additions & 1 deletion packages/bridge-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ export {

export { DEFAULT_FEATURE_FLAG_CONFIG } from './constants/bridge.js';

export { getBridgeFeatureFlags } from './utils/feature-flags.js';
export {
getBridgeFeatureFlags,
getQuoteRefreshRate,
} from './utils/feature-flags.js';

export { BRIDGE_DEFAULT_SLIPPAGE } from './utils/slippage.js';

Expand Down
20 changes: 20 additions & 0 deletions packages/bridge-controller/src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type {
FeatureFlagsPlatformConfig,
ChainConfiguration,
GenericQuoteRequest,
} from '../types.js';
import { validateFeatureFlagsResponse } from '../validators/feature-flags.js';
import { formatChainIdToCaip } from './caip-formatters.js';
Expand All @@ -14,8 +15,8 @@

export const formatFeatureFlags = (
bridgeFeatureFlags: FeatureFlagsPlatformConfig,
) => {

Check failure on line 18 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

typescript(explicit-function-return-type)

packages/bridge-controller/src/utils/feature-flags.ts:18:3: Missing return type on function.
const getChainsObj = (chains: Record<string, ChainConfiguration>) =>

Check failure on line 19 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

typescript(explicit-function-return-type)

packages/bridge-controller/src/utils/feature-flags.ts:19:69: Missing return type on function.
Object.entries(chains).reduce(
(acc, [chainId, value]) => ({
...acc,
Expand Down Expand Up @@ -57,7 +58,7 @@
* @returns The bridge feature flags
*/
export const getBridgeFeatureFlags = <
T extends {

Check failure on line 61 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

eslint(id-length)

packages/bridge-controller/src/utils/feature-flags.ts:61:3: Identifier name is too short (< 2).
call(
action: 'RemoteFeatureFlagController:getState',
): RemoteFeatureFlagControllerState;
Expand Down Expand Up @@ -90,7 +91,7 @@
export const hasMinimumRequiredVersion = (
clientVersion: string,
minRequiredVersion: string,
) => {

Check failure on line 94 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

typescript(explicit-function-return-type)

packages/bridge-controller/src/utils/feature-flags.ts:94:3: Missing return type on function.
const [clientMajor, clientMinor, clientPatch] = clientVersion
.split('.')
.map(Number);
Expand All @@ -109,3 +110,22 @@
clientPatch >= minRequiredPatch
);
};

export const getQuoteRefreshRate = <
T extends {

Check failure on line 115 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

eslint(id-length)

packages/bridge-controller/src/utils/feature-flags.ts:115:3: Identifier name is too short (< 2).
call(
action: 'RemoteFeatureFlagController:getState',
): RemoteFeatureFlagControllerState;
},
>(
messenger: T,
srcChainId?: GenericQuoteRequest['srcChainId'],
) => {

Check failure on line 123 in packages/bridge-controller/src/utils/feature-flags.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:oxlint)

typescript(explicit-function-return-type)

packages/bridge-controller/src/utils/feature-flags.ts:123:3: Missing return type on function.
const bridgeFeatureFlags = getBridgeFeatureFlags(messenger);

const refreshRateOverride = srcChainId
? bridgeFeatureFlags.chains[formatChainIdToCaip(srcChainId)]?.refreshRate
: undefined;
const defaultRefreshRate = bridgeFeatureFlags.refreshRate;
return refreshRateOverride ?? defaultRefreshRate;
};
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export const getControllerMessenger = (
'TransactionController:isAtomicBatchSupported',
'BridgeController:trackUnifiedSwapBridgeEvent',
'BridgeController:stopPollingForQuotes',
'BridgeController:fetchQuotes',
'RemoteFeatureFlagController:getState',
'AuthenticationController:getBearerToken',
'KeyringController:signTypedMessage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
} from './utils/bridge-status.js';
import {
getBatchSellTrades,
resolveValidQuotes,
stopPollingForQuotes,
trackMetricsEvent,
} from './utils/bridge.js';
Expand Down Expand Up @@ -111,6 +112,7 @@ import {
approvalUpdateHandler,
depositUpdateHandler,
tradeUpdateHandler,
withdrawUpdateHandler,
} from './utils/subscriptions.js';
import type {
SubscriptionHandlerInputs,
Expand All @@ -134,6 +136,7 @@ import {
hasNestedBridgeTransactions,
isTradeTx,
hasNestedDepositTransactions,
hasNestedWithdrawTransferTransactions,
} from './utils/transaction.js';

const metadata: StateMetadata<BridgeStatusControllerState> = {
Expand Down Expand Up @@ -304,6 +307,9 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
case TransactionType.swap:
handler = tradeUpdateHandler;
break;
case TransactionType.moneyAccountWithdraw:
handler = withdrawUpdateHandler;
break;
default:
// Otherwise check for matching nested transactions
if (
Expand All @@ -313,6 +319,8 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
handler = tradeUpdateHandler;
} else if (hasNestedDepositTransactions(args.txMeta)) {
handler = depositUpdateHandler;
} else if (hasNestedWithdrawTransferTransactions(args.txMeta)) {
handler = withdrawUpdateHandler;
} else {
// This handler only checks for failures, so it's safe to use
// for unrecognized transactions
Expand Down Expand Up @@ -488,6 +496,15 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
return;
}

// Return early if withdraw follow-up swap is already in flight
if (
historyItem.withdrawTxMeta?.batchId &&
historyItem.batchId &&
historyItem.withdrawTxMeta.batchId === txMeta.batchId
) {
return;
}

const { type, hash: txHash, id, chainId } = txMeta;

const isBridge =
Expand All @@ -503,6 +520,22 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
return;
}

// Execute swap (next)
const isWithdrawConfirmation =
Boolean(historyItem.next?.length) &&
Boolean(txMeta.batchId) &&
txMeta.batchId === historyItem.withdrawTxMeta?.batchId;
if (isWithdrawConfirmation) {
this.#updateHistoryItem({
historyKey,
historyItemOverrides: {
withdrawTxMeta: { hash: txHash },
},
});
await this.#nextTx(historyKey);
return;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

this.#updateHistoryItem({
historyKey,
historyItemOverrides: {
Expand Down Expand Up @@ -539,6 +572,9 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
...(updatedHistoryItem.depositTxMeta && {
depositTxMeta: { hash: txHash },
}),
...(updatedHistoryItem.withdrawTxMeta && {
withdrawTxMeta: { hash: txHash },
}),
},
});
await this.#nextTx(historyKey);
Expand Down Expand Up @@ -1263,11 +1299,20 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
historyItem.quote.srcChainId,
historyItem.quote.destChainId,
);

// Use tx hash that transferred funds to the user's wallet
const txHash = isBridgeTx
const preDepositTxHash = isBridgeTx
? historyItem.status.destChain?.txHash
: historyItem.status.srcChain.txHash;
const txHash = historyItem.withdrawTxMeta
? historyItem.withdrawTxMeta.hash
: preDepositTxHash;

const validQuotes = await resolveValidQuotes(
this.messenger,
historyItem,
quoteResponse,
operation,
);

const selectedAccount = getAccountByAddress(
this.messenger,
Expand All @@ -1278,6 +1323,7 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
`Account not found, skipping next action: ${historyItem.account}`,
);
}

const isDelegatedAccount = isNonEvmChainId(quoteResponse.quote.srcChainId)
? false
: await checkIsDelegatedAccount(
Expand All @@ -1299,7 +1345,7 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
clientId: this.#clientId,
bridgeApiBaseUrl: this.#config.customBridgeApiBaseUrl,
batchSellTrades: historyItem.batchSellData ?? null,
quoteResponses: [quoteResponse],
quoteResponses: validQuotes,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
isDelegatedAccount,
operation,
batchId,
Expand All @@ -1325,6 +1371,12 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
throw new Error('Transaction failed due to missing tx meta');
}
} catch (error) {
if (
error instanceof Error &&
error.message.includes('already started, skipping')
) {
return;
}
// Client submission hooks don't catch this error so use messenger to capture it
this.messenger.captureException?.(
error instanceof Error ? error : new Error(String(error)),
Expand Down
Loading
Loading