Skip to content

Commit

Permalink
Resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenvoronov committed Aug 25, 2023
2 parents 35625de + 57818b6 commit 3630bd1
Show file tree
Hide file tree
Showing 56 changed files with 1,383 additions and 841 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cd-subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
paths:
- packages/sdk/typescript/subgraph/**
- .github/workflows/cd-subgraph.yaml
workflow_dispatch:

jobs:
subgraph:
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/job-launcher/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ HOST=localhost
PORT=3000
FE_URL=http://localhost:3001
SESSION_SECRET=test
PASSWORD_SECRET=test

# Database
POSTGRES_HOST=0.0.0.0
POSTGRES_USER=operator
POSTGRES_PASSWORD=qwerty
POSTGRES_DB=job-launcher
POSTGRES_DATABASE=job-launcher
POSTGRES_SYNC=false
POSTGRES_PORT=5432
POSTGRES_SSL=false

#Web3
WEB3_ENV=testnet
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/job-launcher/server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- POSTGRES_HOST=${POSTGRES_HOST}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_DB=${POSTGRES_DATABASE}
- POSTGRES_PORT=${POSTGRES_PORT}
- POSTGRES_SYNC=${POSTGRES_SYNC}
logging:
Expand Down
1 change: 1 addition & 0 deletions packages/apps/job-launcher/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"cookie-parser": "^1.4.6",
"decimal.js": "^10.4.3",
"ethers": "^5.7.2",
"express-session": "^1.17.3",
"joi": "^17.9.2",
Expand Down
17 changes: 12 additions & 5 deletions packages/apps/job-launcher/server/src/common/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ export const ConfigNames = {
PORT: 'PORT',
FE_URL: 'FE_URL',
SESSION_SECRET: 'SESSION_SECRET',
PASSWORD_SECRET: 'PASSWORD_SECRET',
JWT_SECRET: 'JWT_SECRET',
JWT_ACCESS_TOKEN_EXPIRES_IN: 'JWT_ACCESS_TOKEN_EXPIRES_IN',
JWT_REFRESH_TOKEN_EXPIRES_IN: 'JWT_REFRESH_TOKEN_EXPIRES_IN',
POSTGRES_HOST: 'POSTGRES_HOST',
POSTGRES_USER: 'POSTGRES_USER',
POSTGRES_PASSWORD: 'POSTGRES_PASSWORD',
POSTGRES_DB: 'POSTGRES_DB',
POSTGRES_DATABASE: 'POSTGRES_DATABASE',
POSTGRES_PORT: 'POSTGRES_PORT',
POSTGRES_SYNC: 'POSTGRES_SYNC',
POSTGRES_SSL: 'POSTGRES_SSL',
WEB3_ENV: 'WEB3_ENV',
WEB3_PRIVATE_KEY: 'WEB3_PRIVATE_KEY',
JOB_LAUNCHER_FEE: 'JOB_LAUNCHER_FEE',
Expand All @@ -41,6 +41,9 @@ export const ConfigNames = {
SENDGRID_API_KEY: 'SENDGRID_API_KEY',
SENDGRID_FROM_EMAIL: 'SENDGRID_FROM_EMAIL',
SENDGRID_FROM_NAME: 'SENDGRID_FROM_NAME',
CVAT_JOB_SIZE: 'CVAT_JOB_SIZE',
CVAT_MAX_TIME: 'CVAT_MAX_TIME',
CVAT_VAL_SIZE: 'CVAT_VAL_SIZE',
};

export const envValidator = Joi.object({
Expand All @@ -50,7 +53,6 @@ export const envValidator = Joi.object({
PORT: Joi.string().default(5000),
FE_URL: Joi.string().default('http://localhost:3005'),
SESSION_SECRET: Joi.string().default('session_key'),
PASSWORD_SECRET: Joi.string().default('$2b$10$EICgM2wYixoJisgqckU9gu'),
// Auth
JWT_SECRET: Joi.string().default('secrete'),
JWT_ACCESS_TOKEN_EXPIRES_IN: Joi.string().default(1000000000),
Expand All @@ -60,9 +62,10 @@ export const envValidator = Joi.object({
POSTGRES_HOST: Joi.string().default('127.0.0.1'),
POSTGRES_USER: Joi.string().default('operator'),
POSTGRES_PASSWORD: Joi.string().default('qwerty'),
POSTGRES_DB: Joi.string().default('job-launcher'),
POSTGRES_DATABASE: Joi.string().default('job-launcher'),
POSTGRES_PORT: Joi.string().default('5432'),
POSTGRES_SYNC: Joi.string().default(false),
POSTGRES_SSL: Joi.string().default(false),
// Web3
WEB3_ENV: Joi.string().default('testnet'),
WEB3_PRIVATE_KEY: Joi.string().required(),
Expand Down Expand Up @@ -92,4 +95,8 @@ export const envValidator = Joi.object({
SENDGRID_API_KEY: Joi.string().required(),
SENDGRID_FROM_EMAIL: Joi.string().default('[email protected]'),
SENDGRID_FROM_NAME: Joi.string().default('Human Protocol Job Launcher'),
});
// CVAT
CVAT_JOB_SIZE: Joi.string().default('10'),
CVAT_MAX_TIME: Joi.string().default('300'),
CVAT_VAL_SIZE: Joi.string().default('2'),
});
39 changes: 33 additions & 6 deletions packages/apps/job-launcher/server/src/common/config/networks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ChainId, NETWORKS } from '@human-protocol/sdk';

export interface TokensList {
[key: string]: string | undefined;
}
export interface NetworkDto {
chainId: number;
rpcUrl: string;
tokens: TokensList;
}

interface NetworkMapDto {
Expand All @@ -9,30 +15,51 @@ interface NetworkMapDto {

export const networkMap: NetworkMapDto = {
polygon: {
chainId: 137,
chainId: ChainId.POLYGON,
rpcUrl:
'https://polygon-mainnet.g.alchemy.com/v2/0Lorh5KRkGl5FsRwy2epTg8fEFFoqUfY',
tokens: {
hmt: NETWORKS[ChainId.POLYGON]?.hmtAddress,
usdt: '0x170a18b9190669cda08965562745a323c907e5ec',
},
},
bsc: {
chainId: 56,
chainId: ChainId.BSC_MAINNET,
rpcUrl: 'https://bsc-dataseed1.binance.org/',
tokens: {
hmt: NETWORKS[ChainId.BSC_MAINNET]?.hmtAddress,
usdt: '0x55d398326f99059fF775485246999027B3197955',
},
},
mumbai: {
chainId: 80001,
chainId: ChainId.POLYGON_MUMBAI,
rpcUrl:
'https://polygon-mumbai.g.alchemy.com/v2/vKNSJzJf6SW2sdW-05bgFwoyFxUrMzii',
tokens: {
hmt: NETWORKS[ChainId.POLYGON_MUMBAI]?.hmtAddress,
usdt: '0x5b20e68f501590C130d77C87C2A2f2B43Fc09701',
},
},
goerli: {
chainId: 5,
chainId: ChainId.GOERLI,
rpcUrl: 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
tokens: {
hmt: NETWORKS[ChainId.GOERLI]?.hmtAddress,
},
},
moonbeam: {
chainId: 1284,
chainId: ChainId.MOONBEAM,
rpcUrl: 'https://rpc.api.moonbeam.network',
tokens: {
hmt: NETWORKS[ChainId.MOONBEAM]?.hmtAddress,
},
},
bsctest: {
chainId: 97,
chainId: ChainId.BSC_TESTNET,
rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
tokens: {
hmt: NETWORKS[ChainId.BSC_TESTNET]?.hmtAddress,
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,11 @@ export enum ErrorWeb3 {
export enum ErrorSendGrid {
EmailNotSent = 'Email was not sent',
InvalidApiKey = 'Invalid SendGrid API key',
}
}

/**
* Represents error messages related to postgres.
*/
export enum ErrorPostgres {
NumericFieldOverflow = 'Numeric field overflow',
}
17 changes: 13 additions & 4 deletions packages/apps/job-launcher/server/src/common/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId } from "@human-protocol/sdk";
import { ChainId } from '@human-protocol/sdk';

export const NS = 'hmt';
export const COINGECKO_API_URL =
Expand All @@ -7,7 +7,16 @@ 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];
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,
];

export const SENDGRID_API_KEY_REGEX = /^SG\.[A-Za-z0-9-_]{22}\.[A-Za-z0-9-_]{43}$/;
export const SENDGRID_API_KEY_REGEX =
/^SG\.[A-Za-z0-9-_]{22}\.[A-Za-z0-9-_]{43}$/;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { ICoingeckoTokenId } from '../interfaces';

export const CoingeckoTokenId: ICoingeckoTokenId = {
hmt: 'human-protocol',
usdt: 'tether',
};
2 changes: 2 additions & 0 deletions packages/apps/job-launcher/server/src/common/enums/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export enum JobStatus {

export enum JobRequestType {
IMAGE_LABEL_BINARY = 'IMAGE_LABEL_BINARY',
IMAGE_POINTS = 'IMAGE_POINTS',
IMAGE_BOXES = 'IMAGE_BOXES',
FORTUNE = 'FORTUNE',
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export enum Currency {

export enum TokenId {
HMT = 'hmt',
USDT = 'usdt',
}

export enum PaymentSource {
Expand Down Expand Up @@ -74,5 +75,3 @@ export enum StripePaymentStatus {
REQUIRES_PAYMENT_METHOD = 'requires_payment_method',
SUCCEEDED = 'succeeded',
}


Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { IBase } from './base';
export interface IJob extends IBase {
userId: number;
chainId: number;
fee: string;
fundAmount: string;
fee: number;
fundAmount: number;
escrowAddress: string;
manifestUrl: string;
manifestHash: string;
Expand Down
62 changes: 62 additions & 0 deletions packages/apps/job-launcher/server/src/common/utils/decimal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Decimal from 'decimal.js';

export function mul(a: number, b: number): number {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

const result = decimalA.times(decimalB);

return result.toNumber();
}

export function div(a: number, b: number): number {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

if (decimalB.isZero()) {
throw new Error('Division by zero is not allowed.');
}

const result = decimalA.dividedBy(decimalB);

return result.toNumber();
}

export function add(a: number, b: number): number {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

const result = decimalA.plus(decimalB);

return result.toNumber();
}

export function sub(a: number, b: number): number {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

const result = decimalA.minus(decimalB);

return result.toNumber();
}

export function eq(a: number, b: number): boolean {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

return decimalA.eq(decimalB);
}

export function lt(a: number, b: number): boolean {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

return decimalA.lt(decimalB);
}

export function gt(a: number, b: number): boolean {
const decimalA = new Decimal(a);
const decimalB = new Decimal(b);

return decimalA.gt(decimalB);
}
52 changes: 27 additions & 25 deletions packages/apps/job-launcher/server/src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { firstValueFrom } from "rxjs";
import { CoingeckoTokenId } from "../constants/payment";
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";
import { firstValueFrom } from 'rxjs';
import { CoingeckoTokenId } from '../constants/payment';
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;
if (from === to) {
return 1;
}
let reversed = false;

if (Object.values(TokenId).includes(to as TokenId)) {
[from, to] = [CoingeckoTokenId[to], from];
} else {
reversed = true;
}
if (Object.values(TokenId).includes(to as TokenId)) {
[from, to] = [CoingeckoTokenId[to], from];
reversed = true;
} else {
[from, to] = [CoingeckoTokenId[from], to];
}

const httpService = new HttpService()
const { data } = await firstValueFrom(
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);
}
if (!data[from] || !data[from][to]) {
throw new NotFoundException(ErrorCurrency.PairNotFound);
}

const rate = data[from][to];
const rate = data[from][to];

return reversed ? 1 / rate : rate;
}
return reversed ? 1 / rate : rate;
}
Loading

0 comments on commit 3630bd1

Please sign in to comment.