Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API query: normalize token identifier case #1375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading