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

[Job Launcher] Added testnet and mainnet setup #763

Merged
merged 13 commits into from
Aug 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe('JobService', () => {
paymentRepository: PaymentRepository,
paymentService: PaymentService,
createPaymentMock: any,
routingProtocolService: RoutingProtocolService;
routingProtocolService: RoutingProtocolService,
web3Service: Web3Service;

const signerMock = {
address: MOCK_ADDRESS,
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('JobService', () => {
provide: Web3Service,
useValue: {
getSigner: jest.fn().mockReturnValue(signerMock),
validateChainId: jest.fn(),
},
},
{ provide: JobRepository, useValue: createMock<JobRepository>() },
Expand All @@ -136,6 +138,7 @@ describe('JobService', () => {
paymentService = moduleRef.get(PaymentService);
routingProtocolService = moduleRef.get(RoutingProtocolService);
createPaymentMock = jest.spyOn(paymentRepository, 'create');
web3Service = moduleRef.get<Web3Service>(Web3Service);
});

describe('createFortuneJob', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export class JobService {
fundAmount,
} = dto;

if (chainId) {
this.web3Service.validateChainId(chainId);
}

const userBalance = await this.paymentService.getUserBalance(userId);

const fundAmountInWei = ethers.utils.parseUnits(
Expand Down Expand Up @@ -189,6 +193,10 @@ export class JobService {
fundAmount,
} = dto;

if (chainId) {
this.web3Service.validateChainId(chainId);
}

const userBalance = await this.paymentService.getUserBalance(userId);

const fundAmountInWei = ethers.utils.parseUnits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('PaymentService', () => {
provide: Web3Service,
useValue: {
getSigner: jest.fn().mockReturnValue(signerMock),
validateChainId: jest.fn(),
},
},
{ provide: ConfigService, useValue: mockConfigService },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export class PaymentService {
userId: number,
dto: PaymentCryptoCreateDto,
): Promise<boolean> {
this.web3Service.validateChainId(dto.chainId);

const provider = new providers.JsonRpcProvider(
Object.values(networkMap).find(
(item) => item.chainId === dto.chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ConfigModule } from '@nestjs/config';

@Module({
imports: [ConfigModule],
providers: [Web3Service, Logger],
providers: [Web3Service],
exports: [Web3Service],
})
export class Web3Module {}
22 changes: 13 additions & 9 deletions packages/apps/job-launcher/server/src/modules/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export class Web3Service {
}
}

getSigner(chainId: number): Wallet {
if (Web3Env.MAINNET === this.configService.get(ConfigNames.WEB3_ENV) && !MAINNET_CHAIN_IDS.includes(chainId)) {
this.logger.log(ErrorWeb3.InvalidMainnetChainId, Web3Service.name);
throw new BadRequestException(ErrorWeb3.InvalidMainnetChainId);
} else if (Web3Env.TESTNET === this.configService.get(ConfigNames.WEB3_ENV) && !TESTNET_CHAIN_IDS.includes(chainId)) {
this.logger.log(ErrorWeb3.InvalidTestnetChainId, Web3Service.name);
throw new BadRequestException(ErrorWeb3.InvalidTestnetChainId);
}

public getSigner(chainId: number): Wallet {
this.validateChainId(chainId);
return this.signers[chainId];
}

public validateChainId(chainId: number): void {
const currentWeb3Env = this.configService.get(ConfigNames.WEB3_ENV);
const validChainIds = currentWeb3Env === Web3Env.MAINNET ? MAINNET_CHAIN_IDS : TESTNET_CHAIN_IDS;

if (!validChainIds.includes(chainId)) {
const errorType = currentWeb3Env === Web3Env.MAINNET ? ErrorWeb3.InvalidMainnetChainId : ErrorWeb3.InvalidTestnetChainId;
this.logger.log(errorType, Web3Service.name);
throw new BadRequestException(errorType);
}
}
}