@@ -8,7 +8,9 @@ import type { CaipChainId, Hex } from '@metamask/utils';
88
99import { 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' ;
1214export 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}
0 commit comments