Skip to content

Commit

Permalink
API query: normalize token identifier case (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-rosianu authored Nov 15, 2024
1 parent 34f5ad1 commit 29d9459
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/endpoints/tokens/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ export class TokenService {

async isToken(identifier: string): Promise<boolean> {
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<TokenDetailed | undefined> {
async getToken(rawIdentifier: string, supplyOptions?: TokenSupplyOptions): Promise<TokenDetailed | undefined> {
const tokens = await this.getAllTokens();
const identifier = this.normalizeIdentifierCase(rawIdentifier);
let token = tokens.find(x => x.identifier === identifier);

if (!TokenUtils.isToken(identifier)) {
Expand Down Expand Up @@ -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<TokenDetailed[]> {
const { from, size } = queryPagination;

Expand Down
21 changes: 21 additions & 0 deletions src/test/unit/services/tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 29d9459

Please sign in to comment.