Skip to content

Commit 42b9e6c

Browse files
committed
chore: return native and solana tokens in fetchBridgeTokens
1 parent 5f6df52 commit 42b9e6c

File tree

4 files changed

+50
-62
lines changed

4 files changed

+50
-62
lines changed

packages/bridge-controller/src/types.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ export enum SortOrder {
9999
ETA_ASC = 'time_descending',
100100
}
101101

102+
/**
103+
* This is the interface for the asset object returned by the bridge-api
104+
* This type is used in the QuoteResponse and in the fetchBridgeTokens response
105+
*/
106+
export type BridgeAsset = {
107+
chainId: ChainId;
108+
address: string;
109+
symbol: string;
110+
name: string;
111+
decimals: number;
112+
icon?: string;
113+
iconUrl: string;
114+
assetId: string;
115+
};
116+
117+
/**
118+
* This is the interface for the token object used in the extension client
119+
* In addition to the {@link BridgeAsset} fields, it includes balance information
120+
*/
102121
export type BridgeToken = {
103122
address: string;
104123
symbol: string;
@@ -129,16 +148,6 @@ export type FeatureFlagResponse = {
129148
[BridgeFlag.MOBILE_CONFIG]: FeatureFlagResponsePlatformConfig;
130149
};
131150

132-
export type BridgeAsset = {
133-
chainId: ChainId;
134-
address: string;
135-
symbol: string;
136-
name: string;
137-
decimals: number;
138-
icon?: string;
139-
iconUrl: string;
140-
assetId: string;
141-
};
142151

143152
/**
144153
* This is the interface for the quote request sent to the bridge-api

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ export const sumHexes = (...hexStrings: string[]): Hex => {
5959
* @param chainId - The hex encoded chain ID of the default swaps token to check
6060
* @returns Whether the address is the provided chain's default token address
6161
*/
62-
export const isSwapsDefaultTokenAddress = (address: string, chainId: Hex) => {
62+
export const isSwapsDefaultTokenAddress = (
63+
address: string,
64+
chainId: Hex | CaipChainId,
65+
) => {
6366
if (!address || !chainId) {
6467
return false;
6568
}
@@ -80,7 +83,10 @@ export const isSwapsDefaultTokenAddress = (address: string, chainId: Hex) => {
8083
* @param chainId - The hex encoded chain ID of the default swaps token to check
8184
* @returns Whether the symbol is the provided chain's default token symbol
8285
*/
83-
export const isSwapsDefaultTokenSymbol = (symbol: string, chainId: Hex) => {
86+
export const isSwapsDefaultTokenSymbol = (
87+
symbol: string,
88+
chainId: Hex | CaipChainId,
89+
) => {
8490
if (!symbol || !chainId) {
8591
return false;
8692
}
@@ -103,6 +109,7 @@ export const isNativeAddress = (address?: string | null) =>
103109
address === AddressZero || // bridge and swap apis set the native asset address to zero
104110
address === '' || // assets controllers set the native asset address to an empty string
105111
!address ||
112+
address.endsWith('11111111111111111111111111111111') ||
106113
[DEFAULT_SOLANA_TOKEN_ADDRESS].some(
107114
(assetId) => assetId.includes(address) && !isStrictHexString(address),
108115
); // multichain native assets are represented in caip format

packages/bridge-controller/src/utils/fetch.ts

+8-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import type { Hex } from '@metamask/utils';
2-
import { Duration, hexToNumber } from '@metamask/utils';
1+
import type { CaipChainId, Hex } from '@metamask/utils';
2+
import { Duration } from '@metamask/utils';
33

4-
import {
5-
isSwapsDefaultTokenAddress,
6-
isSwapsDefaultTokenSymbol,
7-
} from './bridge';
84
import {
95
formatAddressToString,
106
formatChainIdToCaip,
@@ -16,15 +12,14 @@ import {
1612
validateSwapsTokenObject,
1713
} from './validators';
1814
import { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';
19-
import type { SwapsTokenObject } from '../constants/tokens';
20-
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../constants/tokens';
2115
import type {
2216
QuoteResponse,
2317
BridgeFeatureFlags,
2418
FetchFunction,
2519
ChainConfiguration,
2620
GenericQuoteRequest,
2721
QuoteRequest,
22+
BridgeAsset,
2823
} from '../types';
2924
import { BridgeFlag, BridgeFeatureFlagsKey } from '../types';
3025

@@ -94,13 +89,13 @@ export async function fetchBridgeFeatureFlags(
9489
* @returns A list of enabled (unblocked) tokens
9590
*/
9691
export async function fetchBridgeTokens(
97-
chainId: Hex,
92+
chainId: Hex | CaipChainId,
9893
clientId: string,
9994
fetchFn: FetchFunction,
10095
bridgeApiBaseUrl: string,
101-
): Promise<Record<string, SwapsTokenObject>> {
96+
): Promise<Record<string, BridgeAsset>> {
10297
// TODO make token api v2 call
103-
const url = `${bridgeApiBaseUrl}/getTokens?chainId=${hexToNumber(chainId)}`;
98+
const url = `${bridgeApiBaseUrl}/getTokens?chainId=${formatChainIdToDec(chainId)}`;
10499

105100
// TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:
106101
// If we allow selecting dest networks which the user has not imported,
@@ -111,24 +106,9 @@ export async function fetchBridgeTokens(
111106
functionName: 'fetchBridgeTokens',
112107
});
113108

114-
const nativeToken =
115-
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
116-
chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP
117-
];
118-
119-
const transformedTokens: Record<string, SwapsTokenObject> = {};
120-
if (nativeToken) {
121-
transformedTokens[nativeToken.address] = nativeToken;
122-
}
123-
109+
const transformedTokens: Record<string, BridgeAsset> = {};
124110
tokens.forEach((token: unknown) => {
125-
if (
126-
validateSwapsTokenObject(token) &&
127-
!(
128-
isSwapsDefaultTokenSymbol(token.symbol, chainId) ||
129-
isSwapsDefaultTokenAddress(token.address, chainId)
130-
)
131-
) {
111+
if (validateSwapsTokenObject(token)) {
132112
transformedTokens[token.address] = token;
133113
}
134114
});

packages/bridge-controller/src/utils/validators.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import {
1111
optional,
1212
enums,
1313
define,
14-
size,
1514
union,
1615
} from '@metamask/superstruct';
1716
import { isStrictHexString } from '@metamask/utils';
1817

19-
import type { SwapsTokenObject } from '../constants/tokens';
20-
import type { FeatureFlagResponse, QuoteResponse } from '../types';
18+
import type { BridgeAsset, FeatureFlagResponse, QuoteResponse } from '../types';
2119
import { ActionTypes, BridgeFlag, FeeType } from '../types';
2220

2321
const HexAddressSchema = define('HexAddress', (v: unknown) =>
@@ -35,10 +33,16 @@ const TruthyDigitStringSchema = define(
3533
truthyString(v as string) && Boolean((v as string).match(/^\d+$/u)),
3634
);
3735

38-
const SwapsTokenObjectSchema = type({
39-
decimals: number(),
36+
const ChainIdSchema = number();
37+
38+
const BridgeAssetSchema = type({
39+
chainId: ChainIdSchema,
4040
address: string(),
41-
symbol: size(string(), 1, 12),
41+
assetId: string(),
42+
symbol: string(),
43+
name: string(),
44+
decimals: number(),
45+
icon: optional(string()),
4246
});
4347

4448
export const validateFeatureFlagsResponse = (
@@ -67,23 +71,11 @@ export const validateFeatureFlagsResponse = (
6771

6872
export const validateSwapsTokenObject = (
6973
data: unknown,
70-
): data is SwapsTokenObject => {
71-
return is(data, SwapsTokenObjectSchema);
74+
): data is BridgeAsset => {
75+
return is(data, BridgeAssetSchema);
7276
};
7377

7478
export const validateQuoteResponse = (data: unknown): data is QuoteResponse => {
75-
const ChainIdSchema = number();
76-
77-
const BridgeAssetSchema = type({
78-
chainId: ChainIdSchema,
79-
address: string(),
80-
assetId: string(),
81-
symbol: string(),
82-
name: string(),
83-
decimals: number(),
84-
icon: optional(string()),
85-
});
86-
8779
const FeeDataSchema = type({
8880
amount: TruthyDigitStringSchema,
8981
asset: BridgeAssetSchema,
@@ -111,10 +103,10 @@ export const validateQuoteResponse = (data: unknown): data is QuoteResponse => {
111103
const QuoteSchema = type({
112104
requestId: string(),
113105
srcChainId: ChainIdSchema,
114-
srcAsset: SwapsTokenObjectSchema,
106+
srcAsset: BridgeAssetSchema,
115107
srcTokenAmount: string(),
116108
destChainId: ChainIdSchema,
117-
destAsset: SwapsTokenObjectSchema,
109+
destAsset: BridgeAssetSchema,
118110
destTokenAmount: string(),
119111
feeData: record(enums(Object.values(FeeType)), FeeDataSchema),
120112
bridgeId: string(),

0 commit comments

Comments
 (0)