-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
1,383 additions
and
841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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({ | ||
|
@@ -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), | ||
|
@@ -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(), | ||
|
@@ -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'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/apps/job-launcher/server/src/common/utils/decimal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
packages/apps/job-launcher/server/src/common/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.