Skip to content

Commit 967a5b6

Browse files
committed
feat: fake the rwaData returned from backend to make sure it work for stock label.
1 parent 966a0d9 commit 967a5b6

File tree

3 files changed

+108
-11
lines changed

3 files changed

+108
-11
lines changed

packages/assets-controllers/src/token-service.ts

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import type { CaipChainId, Hex } from '@metamask/utils';
88

99
import { isTokenListSupportedForNetwork } from './assetsUtil';
1010

11-
export const TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';
11+
// export const TOKEN_END_POINT_API = 'https://token.api.cx.metamask.io';
12+
// TODO change it back after development is done
13+
export const TOKEN_END_POINT_API = 'https://token.dev-api.cx.metamask.io';
1214
export const TOKEN_METADATA_NO_SUPPORT_ERROR =
1315
'TokenService Error: Network does not support fetchTokenMetadata';
1416

@@ -18,7 +20,7 @@ export const TOKEN_METADATA_NO_SUPPORT_ERROR =
1820
* @param chainId - The chain ID of the network the tokens requested are on.
1921
* @returns The tokens URL.
2022
*/
21-
function getTokensURL(chainId: Hex) {
23+
function getTokensURL(chainId: Hex): string {
2224
const occurrenceFloor = chainId === ChainId['linea-mainnet'] ? 1 : 3;
2325

2426
return `${TOKEN_END_POINT_API}/tokens/${convertHexToDecimal(
@@ -33,7 +35,7 @@ function getTokensURL(chainId: Hex) {
3335
* @param tokenAddress - The token address.
3436
* @returns The token metadata URL.
3537
*/
36-
function getTokenMetadataURL(chainId: Hex, tokenAddress: string) {
38+
function getTokenMetadataURL(chainId: Hex, tokenAddress: string): string {
3739
return `${TOKEN_END_POINT_API}/token/${convertHexToDecimal(
3840
chainId,
3941
)}?address=${tokenAddress}`;
@@ -62,12 +64,12 @@ function getTokenSearchURL(
6264
query: string,
6365
limit = 10,
6466
includeMarketData = false,
65-
) {
67+
): string {
6668
const encodedQuery = encodeURIComponent(query);
6769
const encodedChainIds = chainIds
6870
.map((id) => encodeURIComponent(id))
6971
.join(',');
70-
return `${TOKEN_END_POINT_API}/tokens/search?networks=${encodedChainIds}&query=${encodedQuery}&limit=${limit}&includeMarketData=${includeMarketData}`;
72+
return `${TOKEN_END_POINT_API}/tokens/search?networks=${encodedChainIds}&query=${encodedQuery}&limit=${limit}&includeMarketData=${includeMarketData}&includeRwaData=true`;
7173
}
7274

7375
/**
@@ -142,16 +144,42 @@ export async function fetchTokenListByChainId(
142144
if (response) {
143145
const result = await parseJsonResponse(response);
144146
if (Array.isArray(result) && chainId === ChainId['linea-mainnet']) {
145-
return result.filter(
147+
const filteredResult = result.filter(
146148
(elm) =>
147-
elm.aggregators.includes('lineaTeam') || elm.aggregators.length >= 3,
149+
elm.aggregators.includes('lineaTeam') ?? elm.aggregators.length >= 3,
148150
);
151+
// TODO: remove this after development is done
152+
// if the filteredResult has an aggregator that includes 'Ondo' then append rwaData as metadata.
153+
const filteredResultWithRwaData = filteredResult.map((elm) => {
154+
const metadata = {
155+
rwaData: {
156+
instrumentType: 'stock',
157+
ticker: elm.name?.split(' ')[0] ?? '',
158+
market: {
159+
nextOpen: new Date(new Date().setHours(9, 0, 0, 0)).toISOString(),
160+
nextClose: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
161+
},
162+
nextPause: {
163+
start: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
164+
end: new Date(new Date().setHours(17, 0, 0, 0)).toISOString(),
165+
},
166+
},
167+
};
168+
if (elm.aggregators.includes('Ondo')) {
169+
return { ...elm, ...metadata };
170+
}
171+
return elm;
172+
});
173+
174+
console.log('filteredResult', filteredResult);
175+
return filteredResultWithRwaData;
149176
}
150177
return result;
151178
}
152179
return undefined;
153180
}
154181

182+
// TODO This end point already contain RwaData, so we don't need to append it here.
155183
/**
156184
* Search for tokens across one or more networks by query string using CAIP format chain IDs.
157185
*
@@ -180,7 +208,7 @@ export async function searchTokens(
180208
// The API returns an object with structure: { count: number, data: array, pageInfo: object }
181209
if (result && typeof result === 'object' && Array.isArray(result.data)) {
182210
return {
183-
count: result.count || result.data.length,
211+
count: result.count ?? result.data.length,
184212
data: result.data,
185213
};
186214
}
@@ -271,7 +299,21 @@ export async function getTrendingTokens({
271299

272300
// Validate that the API returned an array
273301
if (Array.isArray(result)) {
274-
return result;
302+
// TODO hack the results to include RwaData
303+
const filteredResultWithRwaData = result.map((elm) => {
304+
const metadata = {
305+
rwaData: {
306+
instrumentType: 'stock',
307+
ticker: elm.name?.split(' ')[0] ?? '',
308+
market: {
309+
nextOpen: new Date(new Date().setHours(9, 0, 0, 0)).toISOString(),
310+
nextClose: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
311+
},
312+
},
313+
};
314+
return { ...elm, ...metadata };
315+
});
316+
return filteredResultWithRwaData;
275317
}
276318

277319
// Handle non-expected responses
@@ -306,7 +348,31 @@ export async function fetchTokenMetadata<T>(
306348
const tokenMetadataURL = getTokenMetadataURL(chainId, tokenAddress);
307349
const response = await queryApi(tokenMetadataURL, abortSignal, timeout);
308350
if (response) {
309-
return parseJsonResponse(response) as Promise<T>;
351+
const result = await parseJsonResponse(response);
352+
if (Array.isArray(result)) {
353+
const filteredResultWithRwaData = result.map((elm) => {
354+
const metadata = {
355+
rwaData: {
356+
instrumentType: 'stock',
357+
ticker: elm.name?.split(' ')[0] ?? '',
358+
market: {
359+
nextOpen: new Date(new Date().setHours(9, 0, 0, 0)).toISOString(),
360+
nextClose: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
361+
},
362+
nextPause: {
363+
start: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
364+
end: new Date(new Date().setHours(17, 0, 0, 0)).toISOString(),
365+
},
366+
},
367+
};
368+
if (elm.aggregators.includes('Ondo')) {
369+
return { ...elm, ...metadata };
370+
}
371+
return elm;
372+
});
373+
return filteredResultWithRwaData as unknown as T;
374+
}
375+
return result as T;
310376
}
311377
return undefined;
312378
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,22 @@ export async function fetchBridgeTokens(
5353
const transformedTokens: Record<string, BridgeAsset> = {};
5454
tokens.forEach((token: unknown) => {
5555
if (validateSwapsTokenObject(token)) {
56-
transformedTokens[token.address] = token;
56+
// TODO hack the results to include RwaData
57+
const metadata = {
58+
rwaData: {
59+
instrumentType: 'stock',
60+
ticker: token.name?.split(' ')[0] ?? '',
61+
market: {
62+
nextOpen: new Date(new Date().setHours(9, 0, 0, 0)).toISOString(),
63+
nextClose: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
64+
},
65+
nextPause: {
66+
start: new Date(new Date().setHours(16, 0, 0, 0)).toISOString(),
67+
end: new Date(new Date().setHours(17, 0, 0, 0)).toISOString(),
68+
},
69+
},
70+
};
71+
transformedTokens[token.address] = { ...token, ...metadata };
5772
}
5873
});
5974
return transformedTokens;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
assert,
1717
pattern,
1818
intersection,
19+
date,
1920
} from '@metamask/superstruct';
2021
import { CaipAssetTypeStruct, isStrictHexString } from '@metamask/utils';
2122

@@ -86,6 +87,21 @@ export const BridgeAssetSchema = type({
8687
* URL for token icon
8788
*/
8889
iconUrl: optional(nullable(string())),
90+
91+
rwaData: optional(
92+
type({
93+
instrumentType: string(),
94+
ticker: string(),
95+
market: type({
96+
nextOpen: string(),
97+
nextClose: string(),
98+
}),
99+
nextPause: type({
100+
start: string(),
101+
end: string(),
102+
}),
103+
}),
104+
),
89105
});
90106

91107
const DefaultPairSchema = type({

0 commit comments

Comments
 (0)