Skip to content

Commit

Permalink
Add an endpoint to retrieve all valid networks (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
flopez7 authored Aug 28, 2023
1 parent 0d36bba commit a1b21ab
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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<ConfigService>;
Expand Down Expand Up @@ -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);
});
});
});
16 changes: 12 additions & 4 deletions packages/apps/job-launcher/server/src/modules/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 =
Expand All @@ -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;
}
}

1 comment on commit a1b21ab

@vercel
Copy link

@vercel vercel bot commented on a1b21ab Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

job-launcher-server – ./packages/apps/job-launcher/server

job-launcher-server-nine.vercel.app
job-launcher-server-humanprotocol.vercel.app
job-launcher-server-git-develop-humanprotocol.vercel.app

Please sign in to comment.