Skip to content

Commit

Permalink
fix(xcm-api): Rename some node query endpoints 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Nov 20, 2024
1 parent 6d02218 commit 00a1654
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 184 deletions.
36 changes: 1 addition & 35 deletions apps/xcm-api/src/assets/assets.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,11 @@ describe('AssetsController', () => {
.spyOn(assetsService, 'getAssetsObject')
.mockReturnValue(mockResult);

const result = controller.getAssetsObject(
node,
'polkadot',
mockRequestObject,
);
const result = controller.getAssetsObject(node, mockRequestObject);

expect(result).toBe(mockResult);
expect(spy).toHaveBeenCalledWith(node);
});

it('should return assets object for a valid parachain id', () => {
const paraId = '2009';
const spy = vi
.spyOn(assetsService, 'getNodeByParaId')
.mockReturnValue(paraId);

const result = controller.getAssetsObject(
paraId,
'polkadot',
mockRequestObject,
);

expect(result).toBe(paraId);
expect(spy).toHaveBeenCalledWith(Number(paraId), 'polkadot');
});
});

describe('getAssetId', () => {
Expand Down Expand Up @@ -182,20 +162,6 @@ describe('AssetsController', () => {
});
});

describe('getParaId', () => {
it('should return parachain id for a valid node', () => {
const mockResult = 2009;
const spy = vi
.spyOn(assetsService, 'getParaId')
.mockReturnValue(mockResult);

const result = controller.getParaId(node, mockRequestObject);

expect(result).toBe(mockResult);
expect(spy).toHaveBeenCalledWith(node);
});
});

describe('getSupportedAssets', () => {
it('should return supported assets for a valid node origin and destination', () => {
const nodeOrigin = 'Acala';
Expand Down
29 changes: 3 additions & 26 deletions apps/xcm-api/src/assets/assets.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Controller, Get, Param, Query, Req, Request } from '@nestjs/common';
import { AssetsService } from './assets.service.js';
import { isNumeric } from '../utils.js';
import { SymbolDto } from './dto/SymbolDto.js';
import { AnalyticsService } from '../analytics/analytics.service.js';
import { EventName } from '../analytics/EventName.js';
Expand All @@ -14,25 +13,11 @@ export class AssetsController {
) {}

@Get('assets/:node')
getAssetsObject(
@Param('node') nodeOrParaId: string,
@Query('ecosystem') ecosystem: string | undefined,
@Req() req: Request,
) {
const isParaId = isNumeric(nodeOrParaId);
if (isParaId) {
this.analyticsService.track(EventName.GET_NODE_BY_PARA_ID, req, {
paraId: nodeOrParaId,
});
return this.assetsService.getNodeByParaId(
Number(nodeOrParaId),
ecosystem,
);
}
getAssetsObject(@Param('node') node: string, @Req() req: Request) {
this.analyticsService.track(EventName.GET_ASSETS_OBJECT, req, {
node: nodeOrParaId,
node,
});
return this.assetsService.getAssetsObject(nodeOrParaId);
return this.assetsService.getAssetsObject(node);
}

@Get('assets/:node/id')
Expand Down Expand Up @@ -106,14 +91,6 @@ export class AssetsController {
return this.assetsService.hasSupportForAsset(node, symbol);
}

@Get('assets/:node/para-id')
getParaId(@Param('node') node: string, @Req() req: Request) {
this.analyticsService.track(EventName.GET_PARA_ID, req, {
node,
});
return this.assetsService.getParaId(node);
}

@Get('supported-assets')
getSupportedAssets(
@Query() { origin, destination }: SupportedAssetsDto,
Expand Down
72 changes: 0 additions & 72 deletions apps/xcm-api/src/assets/assets.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('AssetsService', () => {
const symbol = 'DOT';
const unknownSymbol = 'UNKNOWN';
const assetId = '1';
const paraId = 2000;
const decimals = 12;

beforeEach(async () => {
Expand Down Expand Up @@ -373,77 +372,6 @@ describe('AssetsService', () => {
});
});

describe('AssetsService', () => {
let getParaIdSpy: MockInstance;

beforeEach(() => {
getParaIdSpy = vi.spyOn(paraspellSdk, 'getParaId');
});

afterEach(() => {
vi.clearAllMocks();
});

describe('getParaId', () => {
it('should return parachain ID for a valid node', () => {
const result = service.getParaId(node);

expect(result).toEqual(paraId);
expect(getParaIdSpy).toHaveBeenCalledWith(node);
});

it('should throw BadRequestException for invalid node', () => {
const validateNodeSpy = vi
.spyOn(utils, 'validateNode')
.mockImplementation(() => {
throw new BadRequestException();
});
expect(() => service.getParaId(invalidNode)).toThrow(
BadRequestException,
);

expect(validateNodeSpy).toHaveBeenCalledWith(invalidNode, {
excludeEthereum: true,
});
expect(getParaIdSpy).not.toHaveBeenCalled();
});
});
});

describe('getNodeByParaId', () => {
let getTNodeSpy: MockInstance;

beforeEach(() => {
getTNodeSpy = vi.spyOn(paraspellSdk, 'getTNode');
});

it('should return node by parachain ID', () => {
getTNodeSpy.mockReturnValue(node);
const result = service.getNodeByParaId(paraId, 'polkadot');

expect(result).toEqual(JSON.stringify(node));
expect(getTNodeSpy).toHaveBeenCalledWith(paraId, 'polkadot');
});

it('should throw NotFoundException for unknown parachain ID', () => {
const unknownParaId = 999;

expect(() => service.getNodeByParaId(unknownParaId, 'polkadot')).toThrow(
NotFoundException,
);
expect(getTNodeSpy).toHaveBeenCalledWith(unknownParaId, 'polkadot');
});

it('should throw BadRequestException for invalid ecosystem', () => {
const invalidEcosystem = 'invalid';

expect(() => service.getNodeByParaId(paraId, invalidEcosystem)).toThrow(
BadRequestException,
);
expect(getTNodeSpy).not.toHaveBeenCalled();
});
});

describe('getSupportedAssets', () => {
let getSupportedAssetsSpy: MockInstance;

Expand Down
29 changes: 1 addition & 28 deletions apps/xcm-api/src/assets/assets.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import {
TNode,
TNodePolkadotKusama,
getAllAssetsSymbols,
getAssetDecimals,
getAssetId,
getAssetsObject,
getNativeAssets,
getOtherAssets,
getParaId,
getRelayChainSymbol,
getSupportedAssets,
getTNode,
hasSupportForAsset,
} from '@paraspell/sdk';
import { validateNode } from '../utils.js';
Expand Down Expand Up @@ -70,26 +63,6 @@ export class AssetsService {
return hasSupportForAsset(node as TNode, symbol);
}

getParaId(node: string) {
validateNode(node, { excludeEthereum: true });
return getParaId(node as TNodePolkadotKusama);
}

getNodeByParaId(paraId: number, ecosystem: string | undefined) {
if (ecosystem !== 'polkadot' && ecosystem !== 'kusama') {
throw new BadRequestException(
"Invalid ecosystem provided. Available options are 'polkadot' and 'kusama'.",
);
}
const node = getTNode(paraId, ecosystem);
if (!node) {
throw new NotFoundException(
`Node with parachain id ${paraId} not found.`,
);
}
return JSON.stringify(node);
}

getSupportedAssets(nodeOrigin: string, nodeDestination: string) {
validateNode(nodeOrigin, { withRelayChains: true });
validateNode(nodeDestination, { withRelayChains: true });
Expand Down
28 changes: 28 additions & 0 deletions apps/xcm-api/src/node-configs/node-configs.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ describe('AssetsController', () => {
});
});

describe('getParaId', () => {
it('should return parachain id for a valid node', () => {
const mockResult = 2009;
const spy = vi.spyOn(service, 'getParaId').mockReturnValue(mockResult);

const result = controller.getParaId(node, mockRequestObject);

expect(result).toBe(mockResult);
expect(spy).toHaveBeenCalledWith(node);
});
});

describe('getNodeByParaId', () => {
it('should return assets object for a valid parachain id', () => {
const paraId = '2009';
const spy = vi.spyOn(service, 'getNodeByParaId').mockReturnValue(paraId);

const result = controller.getAssetsObject(
paraId,
'polkadot',
mockRequestObject,
);

expect(result).toBe(paraId);
expect(spy).toHaveBeenCalledWith(Number(paraId), 'polkadot');
});
});

describe('getSupportedAssets', () => {
it('should return supported assets for a valid node origin and destination', () => {
const mockResult = ['wss://acala.com', 'wss://acala2.com'];
Expand Down
28 changes: 24 additions & 4 deletions apps/xcm-api/src/node-configs/node-configs.controller.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import { Controller, Get, Param, Req, Request } from '@nestjs/common';
import { Controller, Get, Param, Query, Req, Request } from '@nestjs/common';
import { NodeConfigsService } from './node-configs.service.js';
import { AnalyticsService } from '../analytics/analytics.service.js';
import { EventName } from '../analytics/EventName.js';

@Controller()
@Controller('nodes')
export class NodeConfigsController {
constructor(
private service: NodeConfigsService,
private analyticsService: AnalyticsService,
) {}

@Get('nodes')
@Get()
getNodeNames(@Req() req: Request) {
this.analyticsService.track(EventName.GET_NODE_NAMES, req);
return this.service.getNodeNames();
}

@Get('ws-endpoints/:node')
@Get(':node/ws-endpoints')
getWsEndpoints(@Param('node') node: string, @Req() req: Request) {
this.analyticsService.track(EventName.GET_NODE_NAMES, req);
return this.service.getWsEndpoints(node);
}

@Get(':node/para-id')
getParaId(@Param('node') node: string, @Req() req: Request) {
this.analyticsService.track(EventName.GET_PARA_ID, req, {
node,
});
return this.service.getParaId(node);
}

@Get(':paraId')
getAssetsObject(
@Param('paraId') paraId: string,
@Query('ecosystem') ecosystem: string | undefined,
@Req() req: Request,
) {
this.analyticsService.track(EventName.GET_NODE_BY_PARA_ID, req, {
paraId,
});
return this.service.getNodeByParaId(Number(paraId), ecosystem);
}
}
Loading

0 comments on commit 00a1654

Please sign in to comment.