From 2f745e21f9f51c4fa667f2b39f4734bfaba6a6eb Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Fri, 8 Nov 2024 13:35:17 +0200 Subject: [PATCH 1/4] uppercase token identifier --- src/endpoints/tokens/token.service.ts | 25 ++++++++++++++++++++----- src/test/unit/services/tokens.spec.ts | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/endpoints/tokens/token.service.ts b/src/endpoints/tokens/token.service.ts index 1f2a70635..169710a40 100644 --- a/src/endpoints/tokens/token.service.ts +++ b/src/endpoints/tokens/token.service.ts @@ -68,14 +68,16 @@ 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 { const tokens = await this.getAllTokens(); - let token = tokens.find(x => x.identifier === identifier); + const uppercaseIdentifier = this.uppercaseToken(identifier); + let token = tokens.find(x => x.identifier === uppercaseIdentifier); - if (!TokenUtils.isToken(identifier)) { + if (!TokenUtils.isToken(uppercaseIdentifier)) { return undefined; } @@ -90,9 +92,9 @@ export class TokenService { await this.applySupply(token, supplyOptions); if (token.type === TokenType.FungibleESDT) { - token.roles = await this.getTokenRoles(identifier); + token.roles = await this.getTokenRoles(uppercaseIdentifier); } else if (token.type === TokenType.MetaESDT) { - const elasticCollection = await this.indexerService.getCollection(identifier); + const elasticCollection = await this.indexerService.getCollection(uppercaseIdentifier); if (elasticCollection) { await this.collectionService.applyCollectionRoles(token, elasticCollection); } @@ -101,6 +103,19 @@ export class TokenService { return token; } + uppercaseToken(identifier: string): string { + if (!identifier.includes("-")) { + return identifier.toUpperCase(); + } + + const tokenParts = identifier.split("-"); + if (tokenParts.length !== 2) { + return identifier.toUpperCase(); + } + + return tokenParts[0].toUpperCase() + "-" + tokenParts[1]; + } + 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..be9bce75f 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 even if the input string is not uppercase', 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); From 1b8fd45f1f474566e8084aa75eab39b594fc33c1 Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Fri, 8 Nov 2024 13:45:43 +0200 Subject: [PATCH 2/4] update case --- src/endpoints/tokens/token.service.ts | 16 ++++++++-------- src/test/unit/services/tokens.spec.ts | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/endpoints/tokens/token.service.ts b/src/endpoints/tokens/token.service.ts index 169710a40..235b4fb75 100644 --- a/src/endpoints/tokens/token.service.ts +++ b/src/endpoints/tokens/token.service.ts @@ -72,12 +72,12 @@ export class TokenService { 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 uppercaseIdentifier = this.uppercaseToken(identifier); - let token = tokens.find(x => x.identifier === uppercaseIdentifier); + const identifier = this.adaptIdentifierCase(rawIdentifier); + let token = tokens.find(x => x.identifier === identifier); - if (!TokenUtils.isToken(uppercaseIdentifier)) { + if (!TokenUtils.isToken(identifier)) { return undefined; } @@ -92,9 +92,9 @@ export class TokenService { await this.applySupply(token, supplyOptions); if (token.type === TokenType.FungibleESDT) { - token.roles = await this.getTokenRoles(uppercaseIdentifier); + token.roles = await this.getTokenRoles(identifier); } else if (token.type === TokenType.MetaESDT) { - const elasticCollection = await this.indexerService.getCollection(uppercaseIdentifier); + const elasticCollection = await this.indexerService.getCollection(identifier); if (elasticCollection) { await this.collectionService.applyCollectionRoles(token, elasticCollection); } @@ -103,7 +103,7 @@ export class TokenService { return token; } - uppercaseToken(identifier: string): string { + adaptIdentifierCase(identifier: string): string { if (!identifier.includes("-")) { return identifier.toUpperCase(); } @@ -113,7 +113,7 @@ export class TokenService { return identifier.toUpperCase(); } - return tokenParts[0].toUpperCase() + "-" + tokenParts[1]; + return tokenParts[0].toUpperCase() + "-" + tokenParts[1].toLowerCase(); } async getTokens(queryPagination: QueryPagination, filter: TokenFilter): Promise { diff --git a/src/test/unit/services/tokens.spec.ts b/src/test/unit/services/tokens.spec.ts index be9bce75f..faa0f32bb 100644 --- a/src/test/unit/services/tokens.spec.ts +++ b/src/test/unit/services/tokens.spec.ts @@ -239,7 +239,7 @@ describe('Token Service', () => { })); }); - it('should return token even if the input string is not uppercase', async () => { + it('should return token case insensitive', async () => { const data = require('../../mocks/tokens.mock.json'); tokenService.getAllTokens = jest.fn().mockResolvedValue(data); @@ -248,7 +248,7 @@ describe('Token Service', () => { tokenService.applySupply = jest.fn().mockResolvedValue(undefined); tokenService.getTokenRoles = jest.fn().mockResolvedValue([]); - const result = await tokenService.getToken('wEglD-bd4d79'); + const result = await tokenService.getToken('wEglD-bd4D79'); expect(tokenService.getAllTokens).toHaveBeenCalledTimes(1); expect(tokenService.applyTickerFromAssets).toHaveBeenCalledTimes(1); expect(tokenService.applySupply).toHaveBeenCalledTimes(1); From a4cfd9aa45d1926e7078558f1936c48599a9cc9b Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Fri, 8 Nov 2024 13:48:41 +0200 Subject: [PATCH 3/4] renaming --- src/endpoints/tokens/token.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoints/tokens/token.service.ts b/src/endpoints/tokens/token.service.ts index 235b4fb75..163b17023 100644 --- a/src/endpoints/tokens/token.service.ts +++ b/src/endpoints/tokens/token.service.ts @@ -74,7 +74,7 @@ export class TokenService { async getToken(rawIdentifier: string, supplyOptions?: TokenSupplyOptions): Promise { const tokens = await this.getAllTokens(); - const identifier = this.adaptIdentifierCase(rawIdentifier); + const identifier = this.normalizeIdentifierCase(rawIdentifier); let token = tokens.find(x => x.identifier === identifier); if (!TokenUtils.isToken(identifier)) { @@ -103,7 +103,7 @@ export class TokenService { return token; } - adaptIdentifierCase(identifier: string): string { + normalizeIdentifierCase(identifier: string): string { if (!identifier.includes("-")) { return identifier.toUpperCase(); } From 1fa51aa910399ee4b361f0ebdd7224bafa0a3466 Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Fri, 8 Nov 2024 14:01:59 +0200 Subject: [PATCH 4/4] refactoring --- src/endpoints/tokens/token.service.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/endpoints/tokens/token.service.ts b/src/endpoints/tokens/token.service.ts index 163b17023..1aa0eda8a 100644 --- a/src/endpoints/tokens/token.service.ts +++ b/src/endpoints/tokens/token.service.ts @@ -104,16 +104,12 @@ export class TokenService { } normalizeIdentifierCase(identifier: string): string { - if (!identifier.includes("-")) { + const [ticker, randomSequence] = identifier.split("-"); + if (!ticker || !randomSequence) { return identifier.toUpperCase(); } - const tokenParts = identifier.split("-"); - if (tokenParts.length !== 2) { - return identifier.toUpperCase(); - } - - return tokenParts[0].toUpperCase() + "-" + tokenParts[1].toLowerCase(); + return `${ticker.toUpperCase()}-${randomSequence.toLowerCase()}`; } async getTokens(queryPagination: QueryPagination, filter: TokenFilter): Promise {