Skip to content

Commit 4866c3c

Browse files
committed
chore: addressed PR feedback
1 parent ec37d47 commit 4866c3c

File tree

10 files changed

+51
-81
lines changed

10 files changed

+51
-81
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@mento-protocol/mento-sdk",
33
"description": "Official SDK for interacting with the Mento Protocol",
4-
"version": "1.15.2",
4+
"version": "1.15.4",
55
"license": "MIT",
66
"author": "Mento Labs",
77
"keywords": [

scripts/cacheTokens/fileGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function writeToFile(
4949
scriptDir: string
5050
): string {
5151
// Determine the output directory (src/constants/)
52-
const outputDir = path.resolve(scriptDir, '../../src/constants')
52+
const outputDir = path.resolve(scriptDir, `${process.cwd()}/src/constants`)
5353

5454
// Ensure the output directory exists
5555
if (!fs.existsSync(outputDir)) {

scripts/cacheTokens/tokensIndexGenerator.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,10 @@ export const TOKEN_ADDRESSES_BY_CHAIN: {
148148
} = {
149149
${tokenAddressesMapping}
150150
}
151-
152-
/**
153-
* Helper function to get token address by symbol for a specific chain
154-
* @param symbol - The token symbol
155-
* @param chainId - The chain ID
156-
* @returns The token address or undefined if not found
157-
*/
158-
export function getTokenAddress(
159-
symbol: TokenSymbol,
160-
chainId: number
161-
): string | undefined {
162-
return TOKEN_ADDRESSES_BY_CHAIN[chainId]?.[symbol]
163-
}
164-
165-
/**
166-
* Helper function to find a token by symbol in the cached tokens
167-
* @param symbol - The token symbol to search for
168-
* @param chainId - The chain ID
169-
* @returns The token object or undefined if not found
170-
*/
171-
export function findTokenBySymbol(
172-
symbol: string,
173-
chainId: number
174-
): Token | undefined {
175-
const tokens = getCachedTokensSync(chainId)
176-
return tokens.find((token) => token.symbol === symbol)
177-
}
178151
`
179152

180153
// Write the tokens.ts file to src/constants/
181-
const outputDir = path.resolve(scriptDir, '../../src/constants')
154+
const outputDir = path.resolve(scriptDir, `${process.cwd()}/src/constants`)
182155
const filePath = path.join(outputDir, 'tokens.ts')
183156
fs.writeFileSync(filePath, content)
184157

src/constants/tokens.11142220.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is auto-generated. Do not edit manually.
2-
// Generated on 2025-10-06T12:17:49.520Z
2+
// Generated on 2025-10-06T16:08:43.517Z
33

44
import { Token, TokenSymbol } from '../mento'
55

src/constants/tokens.42220.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is auto-generated. Do not edit manually.
2-
// Generated on 2025-10-06T12:17:30.183Z
2+
// Generated on 2025-10-06T16:08:23.483Z
33

44
import { Token, TokenSymbol } from '../mento'
55

src/constants/tokens.44787.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is auto-generated. Do not edit manually.
2-
// Generated on 2025-10-06T12:17:39.957Z
2+
// Generated on 2025-10-06T16:08:33.900Z
33

44
import { Token, TokenSymbol } from '../mento'
55

src/constants/tokens.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is auto-generated. Do not edit manually.
2-
// Generated on 2025-10-06T12:17:49.522Z
2+
// Generated on 2025-10-06T16:08:43.519Z
33

44
import { Token } from '../mento'
55

@@ -161,30 +161,3 @@ export const TOKEN_ADDRESSES_BY_CHAIN: {
161161
USDT: '0xCA53d9b72646B254d29EBeEb4c5cde7BB4bb59e0',
162162
},
163163
}
164-
165-
/**
166-
* Helper function to get token address by symbol for a specific chain
167-
* @param symbol - The token symbol
168-
* @param chainId - The chain ID
169-
* @returns The token address or undefined if not found
170-
*/
171-
export function getTokenAddress(
172-
symbol: TokenSymbol,
173-
chainId: number
174-
): string | undefined {
175-
return TOKEN_ADDRESSES_BY_CHAIN[chainId]?.[symbol]
176-
}
177-
178-
/**
179-
* Helper function to find a token by symbol in the cached tokens
180-
* @param symbol - The token symbol to search for
181-
* @param chainId - The chain ID
182-
* @returns The token object or undefined if not found
183-
*/
184-
export function findTokenBySymbol(
185-
symbol: string,
186-
chainId: number
187-
): Token | undefined {
188-
const tokens = getCachedTokensSync(chainId)
189-
return tokens.find((token) => token.symbol === symbol)
190-
}

src/mento.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class Mento {
267267
* @throws Error if no cached tokens are available for the current chain or if chainId is not yet initialized
268268
*/
269269
getTokens(): Token[] {
270-
if (this.cachedChainId === null) {
270+
if (!this.cachedChainId) {
271271
throw new Error(
272272
'Chain ID not yet initialized. Use Mento.create() to initialize the SDK, or use getTokensAsync() instead.'
273273
)
@@ -294,8 +294,7 @@ export class Mento {
294294
} = {}): Promise<Token[]> {
295295
// If cached is true, try to use the static cache first
296296
if (cached) {
297-
// eslint-disable-next-line @typescript-eslint/no-var-requires
298-
const { getCachedTokens } = require('./constants/tokens')
297+
const { getCachedTokens } = await import('./constants/tokens')
299298
const chainId = await this.chainId()
300299
const cachedTokens = await getCachedTokens(chainId)
301300
if (cachedTokens) {
@@ -905,7 +904,7 @@ export class Mento {
905904
}
906905

907906
async chainId(): Promise<number> {
908-
if (this.cachedChainId == null) {
907+
if (!this.cachedChainId) {
909908
this.cachedChainId = await getChainId(this.signerOrProvider)
910909
}
911910
return this.cachedChainId

src/routeFetching.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Contract, providers } from 'ethers'
1313
import { IMentoRouter__factory } from 'mento-router-ts'
1414
import { buildRouteDisplay } from '../scripts/quotes/spread'
1515
import { Mento, TradablePair } from './mento'
16-
import { findTokenBySymbolInTradablePairs } from './utils'
16+
import { findTokenAddressBySymbolInTradablePairs } from './utils'
1717

1818
// Simplified mock setup - only what's actually needed
1919
jest.mock('@mento-protocol/mento-core-ts', () => ({
@@ -128,7 +128,7 @@ describe('Route Fetching Logic', () => {
128128

129129
for (const [symbol, expectedAddress] of expectedTokens) {
130130
const displaySymbol = symbol === 'USDT' ? 'USD₮' : symbol
131-
const foundAddress = findTokenBySymbolInTradablePairs(allPairs, displaySymbol)
131+
const foundAddress = findTokenAddressBySymbolInTradablePairs(displaySymbol, allPairs)
132132

133133
expect(foundAddress).toBe(expectedAddress)
134134
}
@@ -195,7 +195,7 @@ describe('Route Fetching Logic', () => {
195195
describe('Error Handling', () => {
196196
it('should handle non-existent token addresses', () => {
197197
const fakeAddress = '0x1234567890123456789012345678901234567890'
198-
const result = findTokenBySymbolInTradablePairs(allPairs, fakeAddress)
198+
const result = findTokenAddressBySymbolInTradablePairs(fakeAddress, allPairs)
199199
expect(result).toBeNull()
200200
})
201201
})

src/utils.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BigNumberish, Contract, providers, Signer } from 'ethers'
22

33
import { TokenSymbol } from './constants'
4+
import { getCachedTokensSync, TOKEN_ADDRESSES_BY_CHAIN } from './constants/tokens'
45
import { Address } from './interfaces'
5-
import { TradablePair } from './mento'
6+
import { Token, TradablePair } from './mento'
67

78
/**
89
* Gets the chain ID from a signer or provider
@@ -131,20 +132,44 @@ export async function increaseAllowance(
131132
* @param symbol the token symbol to find (case-insensitive)
132133
* @returns the token address if found, null otherwise
133134
*/
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+
)
146145
}
147146

148147
export function capitalize(str: string) {
149148
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)
150175
}

0 commit comments

Comments
 (0)