From a1b21ab4c7136c715692a5ccc7e1a48c43efc006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20L=C3=B3pez?= <50665615+flopez7@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:50:51 +0200 Subject: [PATCH] Add an endpoint to retrieve all valid networks (#839) --- .../src/modules/web3/web3.controller.ts | 17 ++++++++++++++++ .../server/src/modules/web3/web3.module.ts | 2 ++ .../src/modules/web3/web3.service.spec.ts | 20 ++++++++++++++++--- .../server/src/modules/web3/web3.service.ts | 16 +++++++++++---- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 packages/apps/job-launcher/server/src/modules/web3/web3.controller.ts diff --git a/packages/apps/job-launcher/server/src/modules/web3/web3.controller.ts b/packages/apps/job-launcher/server/src/modules/web3/web3.controller.ts new file mode 100644 index 0000000000..a03027c364 --- /dev/null +++ b/packages/apps/job-launcher/server/src/modules/web3/web3.controller.ts @@ -0,0 +1,17 @@ +import { Controller, Get } from '@nestjs/common'; +import { Public } from '../../common/decorators'; +import { ApiTags } from '@nestjs/swagger'; +import { Web3Service } from './web3.service'; +import { ChainId } from '@human-protocol/sdk'; + +@ApiTags('Web3') +@Controller('/web3') +export class Web3Controller { + constructor(private readonly web3Service: Web3Service) {} + + @Public() + @Get('/networks') + getValidChains(): ChainId[] { + return this.web3Service.getValidChains(); + } +} diff --git a/packages/apps/job-launcher/server/src/modules/web3/web3.module.ts b/packages/apps/job-launcher/server/src/modules/web3/web3.module.ts index e09eefc454..acd61d7ac6 100644 --- a/packages/apps/job-launcher/server/src/modules/web3/web3.module.ts +++ b/packages/apps/job-launcher/server/src/modules/web3/web3.module.ts @@ -1,10 +1,12 @@ import { Logger, Module } from '@nestjs/common'; import { Web3Service } from './web3.service'; import { ConfigModule } from '@nestjs/config'; +import { Web3Controller } from './web3.controller'; @Module({ imports: [ConfigModule], providers: [Web3Service], exports: [Web3Service], + controllers: [Web3Controller], }) export class Web3Module {} diff --git a/packages/apps/job-launcher/server/src/modules/web3/web3.service.spec.ts b/packages/apps/job-launcher/server/src/modules/web3/web3.service.spec.ts index 38629a3167..8a64e8cedf 100644 --- a/packages/apps/job-launcher/server/src/modules/web3/web3.service.spec.ts +++ b/packages/apps/job-launcher/server/src/modules/web3/web3.service.spec.ts @@ -1,10 +1,10 @@ +import { ChainId } from '@human-protocol/sdk'; import { ConfigService } from '@nestjs/config'; import { Test } from '@nestjs/testing'; -import { networkMap } from '../../common/config'; -import { Web3Service } from './web3.service'; -import { ChainId } from '@human-protocol/sdk'; +import { MAINNET_CHAIN_IDS, TESTNET_CHAIN_IDS } from '../../common/constants'; import { ErrorWeb3 } from '../../common/constants/errors'; import { Web3Env } from '../../common/enums/web3'; +import { Web3Service } from './web3.service'; describe('Web3Service', () => { let mockConfigService: Partial; @@ -76,4 +76,18 @@ describe('Web3Service', () => { ); }); }); + + describe('getValidChains', () => { + it('should get all valid chainIds on MAINNET', () => { + mockConfigService.get = jest.fn().mockReturnValue(Web3Env.MAINNET); + const validChainIds = web3Service.getValidChains(); + expect(validChainIds).toBe(MAINNET_CHAIN_IDS); + }); + + it('should get all valid chainIds on TESTNET', () => { + mockConfigService.get = jest.fn().mockReturnValue(Web3Env.TESTNET); + const validChainIds = web3Service.getValidChains(); + expect(validChainIds).toBe(TESTNET_CHAIN_IDS); + }); + }); }); diff --git a/packages/apps/job-launcher/server/src/modules/web3/web3.service.ts b/packages/apps/job-launcher/server/src/modules/web3/web3.service.ts index 963967a47b..948029016b 100644 --- a/packages/apps/job-launcher/server/src/modules/web3/web3.service.ts +++ b/packages/apps/job-launcher/server/src/modules/web3/web3.service.ts @@ -5,6 +5,7 @@ import { ConfigNames, networkMap } from '../../common/config'; import { Web3Env } from '../../common/enums/web3'; import { MAINNET_CHAIN_IDS, TESTNET_CHAIN_IDS } from '../../common/constants'; import { ErrorWeb3 } from '../../common/constants/errors'; +import { ChainId } from '@human-protocol/sdk'; @Injectable() export class Web3Service { @@ -27,10 +28,7 @@ export class Web3Service { public validateChainId(chainId: number): void { const currentWeb3Env = this.configService.get(ConfigNames.WEB3_ENV); - const validChainIds = - currentWeb3Env === Web3Env.MAINNET - ? MAINNET_CHAIN_IDS - : TESTNET_CHAIN_IDS; + const validChainIds = this.getValidChains(); if (!validChainIds.includes(chainId)) { const errorType = @@ -41,4 +39,14 @@ export class Web3Service { throw new BadRequestException(errorType); } } + + public getValidChains(): ChainId[] { + const currentWeb3Env = this.configService.get(ConfigNames.WEB3_ENV); + const validChainIds = + currentWeb3Env === Web3Env.MAINNET + ? MAINNET_CHAIN_IDS + : TESTNET_CHAIN_IDS; + + return validChainIds; + } }