diff --git a/src/endpoints/tokens/token.service.ts b/src/endpoints/tokens/token.service.ts index 1f2a70635..1aa0eda8a 100644 --- a/src/endpoints/tokens/token.service.ts +++ b/src/endpoints/tokens/token.service.ts @@ -68,11 +68,13 @@ export class TokenService { async isToken(identifier: string): Promise { const tokens = await this.getAllTokens(); - return tokens.find(x => x.identifier === identifier) !== undefined; + const lowercaseIdentifier = identifier.toLowerCase(); + return tokens.find(x => x.identifier.toLowerCase() === lowercaseIdentifier) !== undefined; } - async getToken(identifier: string, supplyOptions?: TokenSupplyOptions): Promise { + async getToken(rawIdentifier: string, supplyOptions?: TokenSupplyOptions): Promise { const tokens = await this.getAllTokens(); + const identifier = this.normalizeIdentifierCase(rawIdentifier); let token = tokens.find(x => x.identifier === identifier); if (!TokenUtils.isToken(identifier)) { @@ -101,6 +103,15 @@ export class TokenService { return token; } + normalizeIdentifierCase(identifier: string): string { + const [ticker, randomSequence] = identifier.split("-"); + if (!ticker || !randomSequence) { + return identifier.toUpperCase(); + } + + return `${ticker.toUpperCase()}-${randomSequence.toLowerCase()}`; + } + async getTokens(queryPagination: QueryPagination, filter: TokenFilter): Promise { const { from, size } = queryPagination; diff --git a/src/test/unit/services/tokens.spec.ts b/src/test/unit/services/tokens.spec.ts index 248b22333..faa0f32bb 100644 --- a/src/test/unit/services/tokens.spec.ts +++ b/src/test/unit/services/tokens.spec.ts @@ -239,6 +239,27 @@ describe('Token Service', () => { })); }); + it('should return token case insensitive', async () => { + const data = require('../../mocks/tokens.mock.json'); + + tokenService.getAllTokens = jest.fn().mockResolvedValue(data); + + tokenService.applyTickerFromAssets = jest.fn().mockResolvedValue(undefined); + tokenService.applySupply = jest.fn().mockResolvedValue(undefined); + tokenService.getTokenRoles = jest.fn().mockResolvedValue([]); + + const result = await tokenService.getToken('wEglD-bd4D79'); + expect(tokenService.getAllTokens).toHaveBeenCalledTimes(1); + expect(tokenService.applyTickerFromAssets).toHaveBeenCalledTimes(1); + expect(tokenService.applySupply).toHaveBeenCalledTimes(1); + expect(tokenService.getTokenRoles).toHaveBeenCalledTimes(1); + expect(result).toEqual(expect.objectContaining({ + identifier: 'WEGLD-bd4d79', + type: 'FungibleESDT', + price: 41.626458658528016, + })); + }); + it('should return undefined if identifier does not exist in getAllTokens', async () => { tokenService.getAllTokens = jest.fn().mockResolvedValue([]); tokenService.applyTickerFromAssets = jest.fn().mockResolvedValue(undefined);