Skip to content

Commit

Permalink
[Job Launcher] Added testnet and mainnet setup (#763)
Browse files Browse the repository at this point in the history
* Added testnet and mainnet setup

* Removed unused tests

* Added additional checks for chain id

* Updated unit tests

* Removed user status from web3 enums

* Updated create job method

* Removed console log

* Removed unnecessary import

* Removed duplicated dto from job service

* Updated job controller

* Removed unnecessary type

* Resolved comments
  • Loading branch information
eugenvoronov authored Aug 15, 2023
1 parent fd0d9a0 commit a490016
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 197 deletions.
6 changes: 5 additions & 1 deletion packages/apps/job-launcher/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ POSTGRES_SYNC=false
POSTGRES_PORT=5432

#Web3
WEB3_ENV=testnet
WEB3_PRIVATE_KEY=
JOB_LAUNCHER_FEE=1
RECORDING_ORACLE_FEE=1
Expand Down Expand Up @@ -42,4 +43,7 @@ STRIPE_SECRET_KEY=
STRIPE_API_VERSION=
STRIPE_APP_NAME=
STRIPE_APP_VERSION=
STRIPE_APP_INFO_URL=
STRIPE_APP_INFO_URL=

# Sendgrid
SENDGRID_API_KEY=
2 changes: 2 additions & 0 deletions packages/apps/job-launcher/server/src/common/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ConfigNames = {
POSTGRES_DB: 'POSTGRES_DB',
POSTGRES_PORT: 'POSTGRES_PORT',
POSTGRES_SYNC: 'POSTGRES_SYNC',
WEB3_ENV: 'WEB3_ENV',
WEB3_PRIVATE_KEY: 'WEB3_PRIVATE_KEY',
JOB_LAUNCHER_FEE: 'JOB_LAUNCHER_FEE',
RECORDING_ORACLE_FEE: 'RECORDING_ORACLE_FEE',
Expand Down Expand Up @@ -61,6 +62,7 @@ export const envValidator = Joi.object({
POSTGRES_PORT: Joi.string().default('5432'),
POSTGRES_SYNC: Joi.string().default(false),
// Web3
WEB3_ENV: Joi.string().default('testnet'),
WEB3_PRIVATE_KEY: Joi.string().required(),
JOB_LAUNCHER_FEE: Joi.string().default(10),
RECORDING_ORACLE_FEE: Joi.string().default(10),
Expand Down
10 changes: 10 additions & 0 deletions packages/apps/job-launcher/server/src/common/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export enum ErrorJob {
WebhookWasNotSent = 'Webhook was not sent',
ResultNotFound = 'Result not found',
ResultValidationFailed = 'Result validation failed',
InvalidRequestType = 'Invalid job type',
JobParamsValidationFailed = 'Job parameters validation failed',
}

/**
Expand Down Expand Up @@ -82,3 +84,11 @@ export enum ErrorBucket {
NotPublic = 'Bucket is not public',
UnableSaveFile = 'Unable to save file',
}

/**
* Represents error messages related to web3.
*/
export enum ErrorWeb3 {
InvalidTestnetChainId = 'Invalid chain id provided for the testnet environment',
InvalidMainnetChainId = 'Invalid chain id provided for the mainnet environment',
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ChainId } from "@human-protocol/sdk";

export const NS = 'hmt';
export const COINGECKO_API_URL =
'https://api.coingecko.com/api/v3/simple/price';
export const JOB_RETRIES_COUNT_THRESHOLD = 3;
export const TX_CONFIRMATION_TRESHOLD = 1;

export const JWT_PREFIX = 'bearer ';
export const TESTNET_CHAIN_IDS = [ChainId.BSC_TESTNET, ChainId.POLYGON_MUMBAI, ChainId.GOERLI];
export const MAINNET_CHAIN_IDS = [ChainId.BSC_MAINNET, ChainId.POLYGON, ChainId.MOONBEAM];
4 changes: 4 additions & 0 deletions packages/apps/job-launcher/server/src/common/enums/web3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Web3Env {
TESTNET = 'testnet',
MAINNET = 'mainnet',
}
12 changes: 7 additions & 5 deletions packages/apps/job-launcher/server/src/common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TokenId } from "../enums/payment";
import { COINGECKO_API_URL } from "../constants";
import { NotFoundException } from "@nestjs/common";
import { ErrorCurrency } from "../constants/errors";
import { HttpService } from "@nestjs/axios";

export async function getRate(from: string, to: string): Promise<number> {
let reversed = false;
Expand All @@ -14,11 +15,12 @@ export async function getRate(from: string, to: string): Promise<number> {
reversed = true;
}

const { data } = await firstValueFrom(
this.httpService.get(
`${COINGECKO_API_URL}?ids=${from}&vs_currencies=${to}`,
),
) as any;
const httpService = new HttpService()
const { data } = await firstValueFrom(
httpService.get(
`${COINGECKO_API_URL}?ids=${from}&vs_currencies=${to}`,
),
) as any;

if (!data[from] || !data[from][to]) {
throw new NotFoundException(ErrorCurrency.PairNotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/common/guards';
import { RequestWithUser } from 'src/common/types';
import { JobCvatDto, JobFortuneDto } from './job.dto';
import { JobFortuneDto, JobImageLabelBinaryDto } from './job.dto';
import { JobService } from './job.service';
import { JobRequestType } from 'src/common/enums/job';

@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
Expand All @@ -24,15 +25,15 @@ export class JobController {
@Request() req: RequestWithUser,
@Body() data: JobFortuneDto,
): Promise<number> {
return this.jobService.createFortuneJob(req.user.id, data);
return this.jobService.createJob(req.user.id, JobRequestType.FORTUNE, data);
}

@Post('/cvat')
public async createCvatJob(
@Request() req: RequestWithUser,
@Body() data: JobCvatDto,
@Body() data: JobImageLabelBinaryDto,
): Promise<number> {
return this.jobService.createCvatJob(req.user.id, data);
return this.jobService.createJob(req.user.id, JobRequestType.IMAGE_LABEL_BINARY, data);
}

@Get('/result')
Expand Down
67 changes: 49 additions & 18 deletions packages/apps/job-launcher/server/src/modules/job/job.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ export class JobCreateDto {
public waitUntil: Date;
}

export class JobFortuneDto {
export class CreateJobDto {
@ApiProperty({
enum: ChainId,
})
@IsEnum(ChainId)
@IsOptional()
public chainId?: ChainId;

@ApiProperty()
@IsNumber()
public fortunesRequired: number;
@ApiProperty({
enum: JobRequestType,
})
@IsEnum(JobRequestType)
public requestType: JobRequestType;

@ApiProperty()
@IsString()
public requesterTitle: string;
@IsNumber()
public submissionsRequired: number;

@ApiProperty()
@IsString()
Expand All @@ -47,27 +49,40 @@ export class JobFortuneDto {
@IsNumber()
@IsPositive()
public fundAmount: number;

@ApiPropertyOptional()
@IsString()
@IsOptional()
public requesterTitle?: string;

@ApiPropertyOptional()
@IsUrl()
@IsOptional()
public dataUrl?: string;

@ApiPropertyOptional()
@IsArray()
@IsOptional()
public labels?: string[];

@ApiPropertyOptional()
@IsNumber()
@IsPositive()
@IsOptional()
public requesterAccuracyTarget?: number;
}

export class JobCvatDto {
export class JobDto {
@ApiProperty({
enum: ChainId,
})
@IsEnum(ChainId)
@IsOptional()
public chainId?: ChainId;

@ApiProperty()
@IsUrl()
public dataUrl: string;

@ApiProperty()
@IsNumber()
public annotationsPerImage: number;

@ApiProperty()
@IsArray()
public labels: string[];
public submissionsRequired: number;

@ApiProperty()
@IsString()
Expand All @@ -76,12 +91,28 @@ export class JobCvatDto {
@ApiProperty()
@IsNumber()
@IsPositive()
public requesterAccuracyTarget: number;
public fundAmount: number;
}

export class JobFortuneDto extends JobDto {
@ApiProperty()
@IsString()
public requesterTitle: string;
}

export class JobImageLabelBinaryDto extends JobDto {
@ApiProperty()
@IsUrl()
public dataUrl: string;

@ApiProperty()
@IsArray()
public labels: string[];

@ApiProperty()
@IsNumber()
@IsPositive()
public fundAmount: number;
public requesterAccuracyTarget: number;
}

export class JobUpdateDto {
Expand Down
Loading

0 comments on commit a490016

Please sign in to comment.