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

[Recording Oracle] - Refactored #732

Merged
merged 12 commits into from
Aug 21, 2023
13 changes: 0 additions & 13 deletions packages/apps/fortune/recording-oracle/.env.sample

This file was deleted.

9 changes: 6 additions & 3 deletions packages/apps/fortune/recording-oracle/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { APP_PIPE } from "@nestjs/core";

import { serverConfig } from "@/common/config";
import { HttpValidationPipe } from "@/common/pipes";
import { JobModule } from "@/modules/job/job.module";

import { AppController } from "./app.controller";
import { envValidator, s3Config, serverConfig, web3Config } from "./common/config";

@Module({
providers: [
Expand All @@ -17,7 +16,11 @@ import { AppController } from "./app.controller";
],
imports: [
ConfigModule.forRoot({
load: [serverConfig],
envFilePath: process.env.NODE_ENV
? `.env.${process.env.NODE_ENV as string}`
: '.env',
validationSchema: envValidator,
load: [serverConfig, s3Config, web3Config],
}),
JobModule,
],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./ethereum.config";
export * from "./server.config";
export * from "./storage.config";
export * from "./web3";
export * from "./server";
export * from "./s3";
export * from "./validation";
13 changes: 13 additions & 0 deletions packages/apps/fortune/recording-oracle/src/common/config/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConfigType, registerAs } from "@nestjs/config";

export const s3Config = registerAs("s3", () => ({
endPoint: process.env.S3_ENDPOINT!,
port: +(process.env.S3_PORT!),
accessKey: process.env.S3_ACCESS_KEY!,
secretKey: process.env.S3_SECRET_KEY!,
bucket: process.env.S3_BACKET!,
useSSL: process.env.S3_USE_SSL === "true",
}));

export const s3ConfigKey = s3Config.KEY;
export type S3ConfigType = ConfigType<typeof s3Config>;

This file was deleted.

11 changes: 11 additions & 0 deletions packages/apps/fortune/recording-oracle/src/common/config/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ConfigType, registerAs } from "@nestjs/config";

export const serverConfig = registerAs("server", () => ({
host: process.env.HOST!,
port: +(process.env.PORT!),
sessionSecret: process.env.SESSION_SECRET!,
feUrl: process.env.FE_URL!,
reputationOracleWebhookUrl: process.env.REPUTATION_ORACLE_WEBHOOK_URL!,
}));
export const serverConfigKey = serverConfig.KEY;
export type ServerConfigType = ConfigType<typeof serverConfig>;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as Joi from 'joi';

export const ConfigNames = {
NODE_ENV: 'NODE_ENV',
HOST: 'HOST',
PORT: 'PORT',
FE_URL: 'FE_URL',
SESSION_SECRET: 'SESSION_SECRET',
WEB3_PRIVATE_KEY: 'WEB3_PRIVATE_KEY',
REPUTATION_ORACLE_WEBHOOK_URL: 'REPUTATION_ORACLE_WEBHOOK_URL',
S3_ENDPOINT: 'S3_ENDPOINT',
S3_PORT: 'S3_PORT',
S3_ACCESS_KEY: 'S3_ACCESS_KEY',
S3_SECRET_KEY: 'S3_SECRET_KEY',
S3_BACKET: 'S3_BACKET',
S3_USE_SSL: 'S3_USE_SSL',
};

export const envValidator = Joi.object({
// General
NODE_ENV: Joi.string().default('development'),
HOST: Joi.string().default('localhost'),
PORT: Joi.string().default(5000),
FE_URL: Joi.string().default('http://localhost:3001'),
SESSION_SECRET: Joi.string().default('session_key'),
// Web3
WEB3_PRIVATE_KEY: Joi.string().required(),
REPUTATION_ORACLE_WEBHOOK_URL: Joi.string().default('http://localhost:4005'),
// S3
S3_ENDPOINT: Joi.string().default('127.0.0.1'),
S3_PORT: Joi.string().default(9000),
S3_ACCESS_KEY: Joi.string().required(),
S3_SECRET_KEY: Joi.string().required(),
S3_BACKET: Joi.string().default('solution'),
S3_USE_SSL: Joi.string().default(false),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ConfigType, registerAs } from "@nestjs/config";

export const web3Config = registerAs("web3", () => ({
web3PrivateKey: process.env.WEB3_PRIVATE_KEY!,
}));

export const web3ConfigKey = web3Config.KEY;
export type Web3ConfigType = ConfigType<typeof web3Config>;
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ export enum ErrorJob {
NotFoundIntermediateResultsUrl = "Error while getting intermediate results url from escrow contract",
SolutionAlreadyExists = "Solution already exists",
AllSolutionsHaveAlreadyBeenSent = "All solutions have already been sent",
WebhookWasNotSent = 'Webhook was not sent',
ManifestNotFound = 'Manifest not found'
}

/**
* Represents error messages related to bucket.
*/
export enum ErrorBucket {
NotPublic = 'Bucket is not public',
UnableSaveFile = 'Unable to save file',
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NetworkMapDto } from "../interfaces/network";

export const networkMap: NetworkMapDto = {
polygon: {
chainId: 137,
rpcUrl:
'https://polygon-mainnet.g.alchemy.com/v2/0Lorh5KRkGl5FsRwy2epTg8fEFFoqUfY',
},
bsc: {
chainId: 56,
rpcUrl: 'https://bsc-dataseed1.binance.org/',
},
mumbai: {
chainId: 80001,
rpcUrl:
'https://polygon-mumbai.g.alchemy.com/v2/vKNSJzJf6SW2sdW-05bgFwoyFxUrMzii',
},
goerli: {
chainId: 5,
rpcUrl: 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
},
moonbeam: {
chainId: 1284,
rpcUrl: 'https://rpc.api.moonbeam.network',
},
bsctest: {
chainId: 97,
rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
},
};

export const networks = Object.values(networkMap).map((network) => network);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum JobRequestType {
FORTUNE = 'FORTUNE'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ChainId } from "@human-protocol/sdk";
import { JobRequestType } from "../enums/job";

export interface IManifest {
submissionsRequired: number;
requesterTitle: string;
requesterDescription: string;
fundAmount: string;
requestType: JobRequestType;
}

export interface ISolution {
exchangeAddress: string;
workerAddress: string;
solution: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface NetworkMapDto {
[key: string]: {
chainId: number;
rpcUrl: string;
};
}
69 changes: 40 additions & 29 deletions packages/apps/fortune/recording-oracle/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { json, urlencoded } from "body-parser";
import { useContainer } from "class-validator";
import cookieParser from "cookie-parser";
import session from "express-session";
import helmet from "helmet";
import session from 'express-session';
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'body-parser';
import { useContainer } from 'class-validator';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';

import { GlobalExceptionsFilter } from "@/common/filter";

import { AppModule } from "./app.module";
import { ServerConfigType, serverConfigKey } from "./common/config";
import { AppModule } from './app.module';
import { ConfigNames, ServerConfigType, serverConfigKey } from './common/config';
import { GlobalExceptionsFilter } from './common/filter';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { cors: true });
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
});

const { feUrl, sessionSecret, host, port }: ServerConfigType = app.get(serverConfigKey);

// Modules
useContainer(app.select(AppModule), { fallbackOnErrors: true });
console.log(feUrl, sessionSecret, host, port)

// Filters
useContainer(app.select(AppModule), { fallbackOnErrors: true });
app.useGlobalFilters(new GlobalExceptionsFilter());

// Middlewares
app.enableCors({
origin:
process.env.NODE_ENV === "development" || process.env.NODE_ENV === "staging"
? [`http://localhost:3005`, `http://127.0.0.1:3005`, `http://0.0.0.0:3005`, feUrl]
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'staging'
? [
`http://localhost:3001`,
`http://127.0.0.1:3001`,
`http://0.0.0.0:3001`,
feUrl,
]
: [feUrl],
credentials: true,
exposedHeaders: ["Content-Disposition"],
exposedHeaders: ['Content-Disposition'],
});

useContainer(app.select(AppModule), { fallbackOnErrors: true });

app.use(cookieParser());

app.use(
session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
}),
);
app.use(json({ limit: "5mb" }));
app.use(urlencoded({ limit: "5mb", extended: true }));
app.use(helmet());
app.use(json({ limit: '5mb' }));
app.use(urlencoded({ limit: '5mb', extended: true }));

// Swagger
const config = new DocumentBuilder()
.addBearerAuth()
.setTitle("Fortune Recording Oracle API")
.setDescription("Swagger Fortune Recording Oracle API")
.setVersion("1.0")
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("swagger", app, document);
SwaggerModule.setup('swagger', app, document);

app.use(helmet());

await app.listen(port, host, async () => {
// eslint-disable-next-line no-console
console.info(`Recording oracle server is running on http://${host}:${port}`);
console.info(`API server is running on http://${host}:${port}`);
});
}

bootstrap();
void bootstrap();

Loading