( tokens: TokenlistToken[], reservedImportNames: Set<string>, chainTargetById: Map<number, ChainTarget>, )
| 125 | } |
| 126 | |
| 127 | async function getTokenDefinitions( |
| 128 | tokens: TokenlistToken[], |
| 129 | reservedImportNames: Set<string>, |
| 130 | chainTargetById: Map<number, ChainTarget>, |
| 131 | ) { |
| 132 | const tokenGroups = new Map<string, TokenDefinition>() |
| 133 | let sourceOrder = 0 |
| 134 | for (const token of tokens) { |
| 135 | const currency = await getTokenCurrency(token, chainTargetById) |
| 136 | const popular = token.popular ?? token.extensions?.popular |
| 137 | const key = `${token.symbol}:${token.name}:${token.decimals}` |
| 138 | const group = tokenGroups.get(key) ?? { |
| 139 | addresses: new Map<number, string>(), |
| 140 | currency, |
| 141 | decimals: token.decimals, |
| 142 | importName: '', |
| 143 | name: token.name, |
| 144 | popular, |
| 145 | sourceOrder: sourceOrder++, |
| 146 | symbol: token.symbol, |
| 147 | } |
| 148 | |
| 149 | const existing = group.addresses.get(token.chainId) |
| 150 | if (existing && existing.toLowerCase() !== token.address.toLowerCase()) |
| 151 | throw new Error( |
| 152 | `Duplicate token ${token.symbol} on chain ${token.chainId}: ${existing} and ${token.address}.`, |
| 153 | ) |
| 154 | |
| 155 | group.addresses.set(token.chainId, token.address) |
| 156 | group.currency ||= currency |
| 157 | group.popular ||= popular |
| 158 | tokenGroups.set(key, group) |
| 159 | } |
| 160 | |
| 161 | const usedImportNames = new Set(reservedImportNames) |
| 162 | return Array.from(tokenGroups.values()) |
| 163 | .sort((a, b) => a.sourceOrder - b.sourceOrder) |
| 164 | .map((token) => ({ |
| 165 | ...token, |
| 166 | importName: getUniqueImportName(token, usedImportNames), |
| 167 | })) |
| 168 | } |
| 169 | |
| 170 | async function getTokenCurrency( |
| 171 | token: TokenlistToken, |
no test coverage detected