|
1 | 1 | import { BigNumberish, Contract, providers, Signer } from 'ethers' |
2 | 2 |
|
3 | 3 | import { TokenSymbol } from './constants' |
| 4 | +import { getCachedTokensSync, TOKEN_ADDRESSES_BY_CHAIN } from './constants/tokens' |
4 | 5 | import { Address } from './interfaces' |
5 | | -import { TradablePair } from './mento' |
| 6 | +import { Token, TradablePair } from './mento' |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Gets the chain ID from a signer or provider |
@@ -131,20 +132,44 @@ export async function increaseAllowance( |
131 | 132 | * @param symbol the token symbol to find (case-insensitive) |
132 | 133 | * @returns the token address if found, null otherwise |
133 | 134 | */ |
134 | | -export function findTokenBySymbolInTradablePairs( |
135 | | - pairs: readonly TradablePair[], |
136 | | - symbol: string |
137 | | -): string | null { |
138 | | - for (const pair of pairs) { |
139 | | - for (const asset of pair.assets) { |
140 | | - if (asset.symbol.toLowerCase() === symbol.toLowerCase()) { |
141 | | - return asset.address |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - return null |
| 135 | +export function findTokenAddressBySymbolInTradablePairs( |
| 136 | + symbol: TokenSymbol, |
| 137 | + pairs: readonly TradablePair[] |
| 138 | +): Address | null { |
| 139 | + return ( |
| 140 | + pairs |
| 141 | + .flatMap((pair) => pair.assets) |
| 142 | + .find((asset) => asset.symbol.toLowerCase() === symbol.toLowerCase()) |
| 143 | + ?.address ?? null |
| 144 | + ) |
146 | 145 | } |
147 | 146 |
|
148 | 147 | export function capitalize(str: string) { |
149 | 148 | return str.charAt(0).toUpperCase() + str.slice(1) |
| 149 | +}/** |
| 150 | + * Helper function to get token address by symbol for a specific chain |
| 151 | + * @param symbol - The token symbol |
| 152 | + * @param chainId - The chain ID |
| 153 | + * @returns The token address or undefined if not found |
| 154 | + */ |
| 155 | + |
| 156 | +export function getTokenAddress( |
| 157 | + symbol: TokenSymbol, |
| 158 | + chainId: number |
| 159 | +): string | undefined { |
| 160 | + return TOKEN_ADDRESSES_BY_CHAIN[chainId]?.[symbol] |
| 161 | +} |
| 162 | +/** |
| 163 | + * Helper function to find a token by symbol in the cached tokens |
| 164 | + * @param symbol - The token symbol to search for |
| 165 | + * @param chainId - The chain ID |
| 166 | + * @returns The token object or undefined if not found |
| 167 | + */ |
| 168 | + |
| 169 | +export function findTokenBySymbol( |
| 170 | + symbol: string, |
| 171 | + chainId: number |
| 172 | +): Token | undefined { |
| 173 | + const tokens = getCachedTokensSync(chainId) |
| 174 | + return tokens.find((token) => token.symbol === symbol) |
150 | 175 | } |
0 commit comments