1
- const CHAIN_MAPPINGS = {
1
+ type ChainMappings = {
2
+ [ key : string ] : string [ ]
3
+ } ;
4
+
5
+ const CHAIN_MAPPINGS : ChainMappings = {
2
6
eth : [ 'eth' , 'ethereum' , 'ether' , 'mainnet' ] ,
3
7
bsc : [ 'bsc' , 'binance' , 'bnb' , 'binance smart chain' , 'smartchain' ] ,
4
8
polygon : [ 'polygon' , 'matic' , 'poly' ] ,
@@ -86,13 +90,17 @@ export interface TokenInfo {
86
90
87
91
function normalizeChainName ( chain : string ) : string | null {
88
92
const normalizedInput = chain . toLowerCase ( ) . trim ( ) ;
89
-
93
+
94
+ // First try exact matches
90
95
for ( const [ standardName , variations ] of Object . entries ( CHAIN_MAPPINGS ) ) {
91
- if ( variations . some ( ( v : string ) => normalizedInput . includes ( v ) ) ) {
96
+ // Check for exact matches first
97
+ if ( variations . includes ( normalizedInput ) ) {
92
98
return standardName ;
93
99
}
94
100
}
95
101
102
+ // If no exact match is found, return the original input
103
+ // This allows for new/unknown chains to pass through
96
104
return normalizedInput ;
97
105
}
98
106
@@ -110,18 +118,20 @@ export function extractTokenInfo(message: string): TokenInfo {
110
118
const prepositionPattern = / (?: o n | f o r | i n | a t | c h a i n ) \s + ( [ a - z A - Z 0 - 9 ] + ) / i;
111
119
const prepositionMatch = cleanMessage . match ( prepositionPattern ) ;
112
120
113
- // 2. Look for chain names anywhere in the message
114
- for ( const [ chainName , variations ] of Object . entries ( CHAIN_MAPPINGS ) ) {
115
- if ( variations . some ( ( v : string ) => cleanMessage . includes ( v ) ) ) {
116
- result . chain = chainName ;
117
- break ;
118
- }
119
- }
120
-
121
- // If chain wasn't found in mappings but was found through preposition pattern,
122
- // use the exact chain name provided
123
- if ( ! result . chain && prepositionMatch ?. [ 1 ] ) {
121
+ if ( prepositionMatch ?. [ 1 ] ) {
124
122
result . chain = normalizeChainName ( prepositionMatch [ 1 ] ) ;
123
+ } else {
124
+ // 2. Look for exact chain matches in the message
125
+ for ( const [ chainName , variations ] of Object . entries ( CHAIN_MAPPINGS ) ) {
126
+ if ( variations . some ( v => {
127
+ // Split message into words and check for exact word matches
128
+ const words = cleanMessage . split ( / \s + / ) ;
129
+ return words . includes ( v ) ;
130
+ } ) ) {
131
+ result . chain = chainName ;
132
+ break ;
133
+ }
134
+ }
125
135
}
126
136
127
137
// Find token address using different patterns
0 commit comments