@@ -22,21 +22,38 @@ async function findTokensInPool(
2222 console . log ( 'Available tokens:' , availableTokens ) ;
2323 console . log ( 'Looking for tokens:' , { coinInType, coinOutType } ) ;
2424
25- // Find matching tokens (case-insensitive and partial match )
25+ // Find matching tokens (case-insensitive and handle both full addresses and short names )
2626 const findToken = ( searchToken : string ) => {
27- return availableTokens . find ( ( token ) =>
28- token . toLowerCase ( ) . includes ( searchToken . toLowerCase ( ) ) ,
27+ // If it's a full address, try exact match first
28+ const exactMatch = availableTokens . find (
29+ ( token ) => token . toLowerCase ( ) === searchToken . toLowerCase ( ) ,
2930 ) ;
31+ if ( exactMatch ) return exactMatch ;
32+
33+ // For short names like 'SUI' or 'WSB', look for them in the token addresses
34+ return availableTokens . find ( ( token ) => {
35+ // Normalize the token string to handle malformed addresses
36+ const normalizedToken = token . replace ( / : { 2 , } / g, '::' ) . toLowerCase ( ) ;
37+ const parts = normalizedToken . split ( '::' ) ;
38+
39+ // Get the token name (last part) and clean it
40+ const tokenName = parts [ parts . length - 1 ] ;
41+ const searchName = searchToken . toLowerCase ( ) ;
42+
43+ return (
44+ tokenName === searchName ||
45+ tokenName . includes ( searchName ) ||
46+ searchName . includes ( tokenName )
47+ ) ;
48+ } ) ;
3049 } ;
3150
3251 const matchedCoinIn = findToken ( coinInType ) ;
3352 const matchedCoinOut = findToken ( coinOutType ) ;
3453
3554 if ( ! matchedCoinIn || ! matchedCoinOut ) {
3655 throw new Error (
37- `Tokens not found in pool. Available tokens: ${ availableTokens . join (
38- ', ' ,
39- ) } `,
56+ `Tokens not found in pool. Available tokens: ${ availableTokens . join ( ', ' ) } ` ,
4057 ) ;
4158 }
4259
0 commit comments