Skip to content
Draft
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 @@ -30,15 +30,14 @@ import {
ETH_USDT_ADDRESS,
} from './constants/bridge';
import { ChainId, RequestStatus } from './types';
import type { BridgeControllerMessenger, QuoteResponse, TxData } from './types';
import type { BridgeControllerMessenger, QuoteResponse } from './types';
import * as balanceUtils from './utils/balance';
import { formatChainIdToDec } from './utils/caip-formatters';
import * as featureFlagUtils from './utils/feature-flags';
import * as fetchUtils from './utils/fetch';
import {
TokenFeatureType,
QuoteStreamCompleteReason,
} from './utils/validators';
import { QuoteStreamCompleteReason } from './validators/quote-stream-complete';
import { TokenFeatureType } from './validators/token-feature';
import type { TxData } from './validators/trade';

type RootMessenger = Messenger<
MockAnyNamespace,
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/bridge-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
MetricsSwapType,
UnifiedSwapBridgeEventName,
} from './utils/metrics/constants';
import { FeatureId } from './utils/validators';
import { FeatureId } from './validators/feature-flags';

const EMPTY_INIT_STATE = DEFAULT_BRIDGE_CONTROLLER_STATE;

Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/bridge-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import {
} from './utils/quote';
import { appendFeesToQuotes } from './utils/quote-fees';
import { getMinimumBalanceForRentExemptionInLamports } from './utils/snaps';
import type { FeatureId } from './utils/validators';
import type { FeatureId } from './validators/feature-flags';

const metadata: StateMetadata<BridgeControllerState> = {
quoteRequest: {
Expand Down
31 changes: 14 additions & 17 deletions packages/bridge-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ export type {
Quote,
QuoteResponse,
FeeData,
TxData,
Intent,
IntentOrderLike,
BitcoinTradeData,
TronTradeData,
BridgeControllerState,
BridgeControllerAction,
BridgeControllerActions,
Expand Down Expand Up @@ -89,16 +86,22 @@ export {
type BridgeControllerStateChangeEvent,
} from './types';

export type {
TxData,
BitcoinTradeData,
TronTradeData,
Trade,
} from './validators/trade';
export { isBitcoinTrade, isTronTrade, isEvmTxData } from './validators/trade';
export { FeeType, ActionTypes } from './validators/quote-response';
export {
FeeType,
ActionTypes,
BridgeAssetSchema,
FeatureId,
TokenFeatureType,
validateQuoteStreamComplete,
QuoteStreamCompleteReason,
BatchSellTransactionType,
} from './utils/validators';
} from './validators/quote-stream-complete';
export { BatchSellTransactionType } from './validators/batch-sell';
export { TokenFeatureType } from './validators/token-feature';
export { BridgeAssetSchema } from './validators/bridge-asset';
export { FeatureId } from './validators/feature-flags';

export {
ALLOWED_BRIDGE_CHAIN_IDS,
Expand Down Expand Up @@ -168,13 +171,7 @@ export {
formatAddressToAssetId,
} from './utils/caip-formatters';

export {
extractTradeData,
isBitcoinTrade,
isTronTrade,
isEvmTxData,
type Trade,
} from './utils/trade-utils';
export { extractTradeData } from './utils/trade-utils';

export {
selectBridgeQuotes,
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
formatAddressToAssetId,
formatChainIdToHex,
} from './utils/caip-formatters';
import { BatchSellTransactionType } from './utils/validators';
import { BatchSellTransactionType } from './validators/batch-sell';

const MOCK_USDC_ADDRESS = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const MOCK_MUSD_ADDRESS = '0x12345A7890123456789012345678901234567890';
Expand Down
31 changes: 15 additions & 16 deletions packages/bridge-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,32 @@ import type {
import type { BridgeController } from './bridge-controller';
import type { BridgeControllerMethodActions } from './bridge-controller-method-action-types';
import type { BRIDGE_CONTROLLER_NAME } from './constants/bridge';
import type { SimulatedGasFeeLimitsSchema } from './validators/batch-sell';
import type { BatchSellTradesResponseSchema } from './validators/batch-sell';
import type { BridgeAssetSchema } from './validators/bridge-asset';
import type { FeatureId } from './validators/feature-flags';
import type {
BitcoinTradeDataSchema,
BridgeAssetSchema,
ChainConfigurationSchema,
ChainRankingSchema,
FeatureId,
PlatformConfigSchema,
} from './validators/feature-flags';
import type {
FeeDataSchema,
IntentSchema,
PlatformConfigSchema,
ProtocolSchema,
QuoteResponseSchema,
QuoteSchema,
StepSchema,
TokenFeatureSchema,
QuoteStreamCompleteSchema,
TronTradeDataSchema,
TxDataSchema,
BatchSellTradesResponseSchema,
GaslessPropertiesSchema,
SimulatedGasFeeLimitsSchema,
TxFeeGasLimitsSchema,
} from './utils/validators';
} from './validators/quote-response';
import type { QuoteStreamCompleteSchema } from './validators/quote-stream-complete';
import type { TokenFeatureSchema } from './validators/token-feature';
import type {
BitcoinTradeData,
TronTradeData,
TxData,
} from './validators/trade';

export type FetchFunction = (
input: RequestInfo | URL | string,
Expand Down Expand Up @@ -279,14 +283,9 @@ export type FeeData = Infer<typeof FeeDataSchema>;

export type Quote = Infer<typeof QuoteSchema>;

export type TxData = Infer<typeof TxDataSchema>;

export type Intent = Infer<typeof IntentSchema>;
export type IntentOrderLike = Intent['order'];

export type BitcoinTradeData = Infer<typeof BitcoinTradeDataSchema>;

export type TronTradeData = Infer<typeof TronTradeDataSchema>;
/**
* This is the type for the quote response from the bridge-api
* TxDataType can be overriden to be a string when the quote is non-evm
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/utils/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import type {
BridgeControllerState,
GenericQuoteRequest,
QuoteResponse,
TxData,
} from '../types';
import { ChainId } from '../types';
import type { TxData } from '../validators/trade';
import {
formatChainIdToCaip,
formatChainIdToDec,
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
DEFAULT_FEATURE_FLAG_CONFIG,
} from '../constants/bridge';
import type { FeatureFlagsPlatformConfig, ChainConfiguration } from '../types';
import { validateFeatureFlagsResponse } from '../validators/feature-flags';
import { formatChainIdToCaip } from './caip-formatters';
import { validateFeatureFlagsResponse } from './validators';

export const formatFeatureFlags = (
bridgeFeatureFlags: FeatureFlagsPlatformConfig,
Expand Down
9 changes: 5 additions & 4 deletions packages/bridge-controller/src/utils/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import mockBridgeQuotesErc20Erc20 from '../../tests/mock-quotes-erc20-erc20.json
import mockBridgeQuotesNativeErc20 from '../../tests/mock-quotes-native-erc20.json';
import { BridgeClientId, BRIDGE_PROD_API_BASE_URL } from '../constants/bridge';
import { QuoteResponse } from '../types';
import { BatchSellTransactionType } from '../validators/batch-sell';
import { FeatureId } from '../validators/feature-flags';
import {
fetchBridgeQuotes,
fetchBridgeTokens,
fetchAssetPrices,
fetchBatchSellTrades,
formatBatchSellTradesRequest,
} from './fetch';
import { BatchSellTransactionType, FeatureId } from './validators';

const mockFetchFn = jest.fn();

Expand Down Expand Up @@ -914,15 +915,15 @@ describe('fetch', () => {
).toMatchInlineSnapshot(`
[
{
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a value of type \`HexString\`, but received: \`1000\`",
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a string, but received: 1000",
"status": "rejected",
},
{
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a value of type \`HexString\`, but received: \`"1000"\`",
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a string matching \`/^0x[0-9a-f]+$/\` but received "1000"",
"status": "rejected",
},
{
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a value of type \`HexString\`, but received: \`291\`",
"reason": "Invalid batch simulation response. StructError: At path: transactions.0.maxFeePerGas -- Expected a string, but received: 291",
"status": "rejected",
},
]
Expand Down
18 changes: 8 additions & 10 deletions packages/bridge-controller/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ import type {
BatchSellTradesRequest,
BatchSellTradesResponse,
} from '../types';
import { validateBatchSellTradesResponse } from '../validators/batch-sell';
import { validateBridgeAsset } from '../validators/bridge-asset';
import type { FeatureId } from '../validators/feature-flags';
import { validateQuoteResponse } from '../validators/quote-response';
import { validateQuoteStreamComplete } from '../validators/quote-stream-complete';
import { validateTokenFeature } from '../validators/token-feature';
import { isEvmTxData } from '../validators/trade';
import { getEthUsdtResetData } from './bridge';
import {
formatAddressToAssetId,
formatAddressToCaipReference,
formatChainIdToDec,
} from './caip-formatters';
import { fetchServerEvents } from './fetch-server-events';
import { isEvmTxData } from './trade-utils';
import type { FeatureId } from './validators';
import {
validateQuoteResponse,
validateSwapsTokenObject,
validateTokenFeature,
validateQuoteStreamComplete,
validateBatchSellTradesResponse,
} from './validators';

export const getClientHeaders = ({
clientId,
Expand Down Expand Up @@ -76,7 +74,7 @@ export async function fetchBridgeTokens(

const transformedTokens: Record<string, BridgeAsset> = {};
tokens.forEach((token: unknown) => {
if (validateSwapsTokenObject(token)) {
if (validateBridgeAsset(token)) {
transformedTokens[token.address] = token;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
QuoteMetadata,
QuoteRequest,
QuoteResponse,
TxData,
} from '../../types';
import type { TxData } from '../../validators/trade';
import { getNativeAssetForChainId, isCrossChain } from '../bridge';
import {
formatAddressToAssetId,
Expand Down
5 changes: 3 additions & 2 deletions packages/bridge-controller/src/utils/quote-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import type {
QuoteResponse,
L1GasFees,
NonEvmFees,
TxData,
BridgeControllerMessenger,
} from '../types';
import { isTronTrade } from '../validators/trade';
import type { TxData } from '../validators/trade';
import { isNonEvmChainId, sumHexes } from './bridge';
import { formatChainIdToCaip } from './caip-formatters';
import { computeFeeRequest } from './snaps';
import { extractTradeData, isTronTrade } from './trade-utils';
import { extractTradeData } from './trade-utils';

/**
* Appends transaction fees for EVM chains to quotes
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-controller/src/utils/quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
Quote,
NonEvmFees,
L1GasFees,
TxData,
} from '../types';
import type { TxData } from '../validators/trade';
import {
isValidQuoteRequest,
getQuoteIdentifier,
Expand Down
8 changes: 3 additions & 5 deletions packages/bridge-controller/src/utils/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import type {
QuoteMetadata,
QuoteResponse,
NonEvmFees,
TxData,
} from '../types';
import { FeatureId } from '../validators/feature-flags';
import type { TxData } from '../validators/trade';
import { isNativeAddress, isNonEvmChainId } from './bridge';
import { FeatureId } from './validators';

export const isValidQuoteRequest = (
partialRequest: Partial<GenericQuoteRequest>,
Expand Down Expand Up @@ -152,9 +152,7 @@ export const calcSentAmount = (
const sentAmount = intent
? new BigNumber(srcTokenAmount)
: Object.values(feeData)
.filter(
(fee) => fee && fee.amount && fee.asset?.assetId === srcAsset.assetId,
)
.filter((fee) => fee?.amount && fee.asset?.assetId === srcAsset.assetId)
.reduce(
(acc, { amount }) => acc.plus(amount),
new BigNumber(srcTokenAmount),
Expand Down
16 changes: 8 additions & 8 deletions packages/bridge-controller/src/utils/trade-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { BitcoinTradeData, TronTradeData, TxData } from '../types';
import {
extractTradeData,
isEvmTxData,
isBitcoinTrade,
isTronTrade,
} from './trade-utils';
import type { Trade } from './trade-utils';
import type {
TxData,
BitcoinTradeData,
TronTradeData,
Trade,
} from '../validators/trade';
import { isEvmTxData, isBitcoinTrade, isTronTrade } from '../validators/trade';
import { extractTradeData } from './trade-utils';

describe('Trade utils', () => {
describe('isEvmTxData', () => {
Expand Down
48 changes: 6 additions & 42 deletions packages/bridge-controller/src/utils/trade-utils.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
import type { BitcoinTradeData, TronTradeData, TxData } from '../types';

// Union type representing all possible trade formats (EVM, Solana, Bitcoin, Tron)
export type Trade = TxData | string | BitcoinTradeData | TronTradeData;

/**
* Type guard to check if a trade is an EVM TxData object
*
* @param trade - The trade object to check
* @returns True if the trade is a TxData object with data property
*/
export const isEvmTxData = (trade: Trade): trade is TxData => {
return (
typeof trade === 'object' &&
trade !== null &&
'data' in trade &&
'chainId' in trade &&
'to' in trade
);
};

/**
* Type guard to check if a trade is a Bitcoin trade with unsignedPsbtBase64
*
* @param trade - The trade object to check
* @returns True if the trade is a Bitcoin trade with unsignedPsbtBase64 property
*/
export const isBitcoinTrade = (trade: Trade): trade is BitcoinTradeData => {
return (
typeof trade === 'object' && trade !== null && 'unsignedPsbtBase64' in trade
);
};

/**
* Type guard to check if a trade is a Tron trade with raw_data_hex
*
* @param trade - The trade object to check
* @returns True if the trade is a Tron trade with raw_data_hex property
*/
export const isTronTrade = (trade: Trade): trade is TronTradeData => {
return typeof trade === 'object' && trade !== null && 'raw_data_hex' in trade;
};
import {
Trade,
isBitcoinTrade,
isTronTrade,
isEvmTxData,
} from '../validators/trade';

/**
* Extracts the transaction data from different trade formats
Expand Down
Loading
Loading