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
1 change: 1 addition & 0 deletions packages/bridge-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@metamask/auto-changelog": "^3.4.4",
"@metamask/eth-json-rpc-provider": "^4.1.8",
"@metamask/network-controller": "^22.2.1",
"@metamask/superstruct": "^3.1.0",
"@metamask/transaction-controller": "^46.0.0",
"@types/jest": "^27.4.1",
"deepmerge": "^4.2.2",
Expand Down
42 changes: 18 additions & 24 deletions packages/bridge-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,16 @@ export enum BridgeFlag {
type DecimalChainId = string;
export type GasMultiplierByChainId = Record<DecimalChainId, number>;

type FeatureFlagResponsePlatformConfig = {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<string, ChainConfiguration>;
};

export type FeatureFlagResponse = {
[BridgeFlag.EXTENSION_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<number, ChainConfiguration>;
};
[BridgeFlag.MOBILE_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<number, ChainConfiguration>;
};
[BridgeFlag.EXTENSION_CONFIG]: FeatureFlagResponsePlatformConfig;
[BridgeFlag.MOBILE_CONFIG]: FeatureFlagResponsePlatformConfig;
};

export type BridgeAsset = {
Expand Down Expand Up @@ -211,19 +208,16 @@ export enum BridgeFeatureFlagsKey {
MOBILE_CONFIG = 'mobileConfig',
}

type FeatureFlagsPlatformConfig = {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<Hex, ChainConfiguration>;
};

export type BridgeFeatureFlags = {
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<Hex, ChainConfiguration>;
};
[BridgeFeatureFlagsKey.MOBILE_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<Hex, ChainConfiguration>;
};
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: FeatureFlagsPlatformConfig;
[BridgeFeatureFlagsKey.MOBILE_CONFIG]: FeatureFlagsPlatformConfig;
};
export enum RequestStatus {
LOADING,
Expand Down
63 changes: 11 additions & 52 deletions packages/bridge-controller/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@ import {
getBridgeApiBaseUrl,
} from './bridge';
import {
FEATURE_FLAG_VALIDATORS,
QUOTE_VALIDATORS,
TX_DATA_VALIDATORS,
TOKEN_VALIDATORS,
validateResponse,
QUOTE_RESPONSE_VALIDATORS,
FEE_DATA_VALIDATORS,
validateFeatureFlagsResponse,
validateQuoteResponse,
validateSwapsTokenObject,
} from './validators';
import { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';
import type { SwapsTokenObject } from '../constants/tokens';
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../constants/tokens';
import type {
FeatureFlagResponse,
FeeData,
Quote,
QuoteRequest,
QuoteResponse,
TxData,
BridgeFeatureFlags,
FetchFunction,
ChainConfiguration,
} from '../types';
import { BridgeFlag, FeeType, BridgeFeatureFlagsKey } from '../types';
import { BridgeFlag, BridgeFeatureFlagsKey } from '../types';

// TODO put this back in once we have a fetchWithCache equivalent
// const CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;
Expand All @@ -50,17 +42,11 @@ export async function fetchBridgeFeatureFlags(
fetchFn: FetchFunction,
): Promise<BridgeFeatureFlags> {
const url = `${getBridgeApiBaseUrl()}/getAllFeatureFlags`;
const rawFeatureFlags = await fetchFn(url, {
const rawFeatureFlags: unknown = await fetchFn(url, {
headers: getClientIdHeader(clientId),
});

if (
validateResponse<FeatureFlagResponse>(
FEATURE_FLAG_VALIDATORS,
rawFeatureFlags,
url,
)
) {
if (validateFeatureFlagsResponse(rawFeatureFlags)) {
const getChainsObj = (chains: Record<number, ChainConfiguration>) =>
Object.entries(chains).reduce(
(acc, [chainId, value]) => ({
Expand Down Expand Up @@ -127,7 +113,7 @@ export async function fetchBridgeTokens(

tokens.forEach((token: unknown) => {
if (
validateResponse<SwapsTokenObject>(TOKEN_VALIDATORS, token, url, false) &&
validateSwapsTokenObject(token) &&
!(
isSwapsDefaultTokenSymbol(token.symbol, chainId) ||
isSwapsDefaultTokenAddress(token.address, chainId)
Expand Down Expand Up @@ -166,40 +152,13 @@ export async function fetchBridgeQuotes(
resetApproval: request.resetApproval ? 'true' : 'false',
});
const url = `${getBridgeApiBaseUrl()}/getQuote?${queryParams}`;
const quotes = await fetchFn(url, {
const quotes: unknown[] = await fetchFn(url, {
headers: getClientIdHeader(clientId),
signal,
});

const filteredQuotes = quotes.filter((quoteResponse: QuoteResponse) => {
const { quote, approval, trade } = quoteResponse;
return (
validateResponse<QuoteResponse>(
QUOTE_RESPONSE_VALIDATORS,
quoteResponse,
url,
) &&
validateResponse<Quote>(QUOTE_VALIDATORS, quote, url) &&
validateResponse<SwapsTokenObject>(
TOKEN_VALIDATORS,
quote.srcAsset,
url,
) &&
validateResponse<SwapsTokenObject>(
TOKEN_VALIDATORS,
quote.destAsset,
url,
) &&
validateResponse<TxData>(TX_DATA_VALIDATORS, trade, url) &&
validateResponse<FeeData>(
FEE_DATA_VALIDATORS,
quote.feeData[FeeType.METABRIDGE],
url,
) &&
(approval
? validateResponse<TxData>(TX_DATA_VALIDATORS, approval, url)
: true)
);
const filteredQuotes = quotes.filter((quoteResponse: unknown) => {
return validateQuoteResponse(quoteResponse);
});
return filteredQuotes;
return filteredQuotes as QuoteResponse[];
}
Loading
Loading