-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feat/back 574 #57
Open
Louis-Amas
wants to merge
3
commits into
master
Choose a base branch
from
feat/BACK-574
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/back 574 #57
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* eslint-disable */ | ||
import axios from 'axios'; | ||
import { SERVICE_CONFIGURATION_SERVICE_HTTP } from '../env'; | ||
import { sleep } from './utils'; | ||
import { ApplicationError } from '../errors'; | ||
import { CHAIN_ID_MAINNET } from '../lib/constants'; | ||
|
||
const serviceName = 'volumetracker'; | ||
|
||
export const CORS_ALLOWED_HEADERS = 'Accept, Content-Type, Origin'; | ||
export const REQUEST_BODY_SIZE_LIMIT_BYTES = 256 * 1024; // 256KB | ||
export const CACHE_CONTROL_PREFLIGHT_REQUESTS_MAX_AGE_SECS = 24 * 60 * 60; // 24 hours | ||
|
||
const CONFIG_SERVICE_TIMEOUT = 5000; | ||
const CONFIG_SERVICE_RETRY_INTERVAL = 3000; | ||
|
||
export type NetworkMap<T> = { [network: number]: T }; | ||
|
||
export type Config = { | ||
network: number; | ||
augustusAddress: string; | ||
augustusV4Address: string; | ||
pspAddress: string; | ||
isStaking: boolean; | ||
rewardDistributionAddress: string; | ||
safetyModuleAddress: string; | ||
privateHttpArchiveProvider: string; | ||
coinGeckoPlatform: string; | ||
multicallV2Address: string; | ||
volumeTrackerInitTime: number; | ||
}; | ||
|
||
type GlobalConfig = { | ||
apiKeyCoingecko: string; | ||
covalentV1ApiKey: string; | ||
covalentV1HttpUrl: string; | ||
apiPrefineryHttp: string; | ||
apiKeyPrefinery: string; | ||
apiAplcapiHttp: string; | ||
apiKeyAplcapi: string; | ||
databaseUrl: string; | ||
apiKeyCaptcha: string; | ||
apiKeySubmitAccount: string; | ||
}; | ||
|
||
type ConfigResponse = { | ||
networks: NetworkMap<Config>; | ||
global: GlobalConfig; | ||
}; | ||
|
||
class ConfigLoader { | ||
public byNetwork: NetworkMap<Config> = {}; | ||
|
||
public enabledNetworks: number[] = []; | ||
|
||
public global?: GlobalConfig; | ||
|
||
public isLoaded: Promise<void>; | ||
|
||
public hasStartedNotifier?: (value: void | PromiseLike<void>) => void; | ||
|
||
constructor() { | ||
this.isLoaded = new Promise(resolve => { | ||
this.hasStartedNotifier = resolve; | ||
}); | ||
} | ||
|
||
async load() { | ||
console.log(`Try to get config from ${SERVICE_CONFIGURATION_SERVICE_HTTP}`); | ||
while (true) { | ||
try { | ||
const configs = ( | ||
await axios.get<ConfigResponse>( | ||
`${SERVICE_CONFIGURATION_SERVICE_HTTP}/configuration?service=${serviceName}`, | ||
{ timeout: CONFIG_SERVICE_TIMEOUT }, | ||
) | ||
).data; | ||
this.global = configs.global; | ||
for (const network in configs.networks) { | ||
const config = configs.networks[network]; | ||
this.byNetwork[network] = config; | ||
} | ||
this.enabledNetworks.push(CHAIN_ID_MAINNET); | ||
break; | ||
} catch (e) { | ||
console.error('Error downloading configuration:', e); | ||
} | ||
await sleep(CONFIG_SERVICE_RETRY_INTERVAL); | ||
} | ||
console.log(`received config`); | ||
} | ||
|
||
getConfig(network: number): Config { | ||
const config = this.byNetwork[network]; | ||
if (!config) { | ||
throw new ApplicationError(`Missing config for network ${network}`); | ||
} | ||
return config; | ||
} | ||
|
||
getGlobalConfig(): GlobalConfig { | ||
if (!this.global) { | ||
throw new ApplicationError(`Missing global config`); | ||
} | ||
return this.global; | ||
} | ||
} | ||
export const configLoader = new ConfigLoader(); | ||
|
||
export const init = async () => { | ||
await configLoader.load(); | ||
configLoader.hasStartedNotifier!(); | ||
}; |
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,3 @@ | ||
/* eslint-disable */ | ||
export const sleep = (ms: number) => | ||
new Promise(resolve => setTimeout(resolve, ms)); |
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,16 +1,14 @@ | ||
import { Client } from 'pg'; | ||
import { Sequelize } from 'sequelize-typescript'; | ||
import * as cls from 'cls-hooked'; | ||
import { IS_DEV } from './env'; | ||
import { configLoader } from './config'; | ||
|
||
const logger = global.LOGGER(); | ||
|
||
const IS_DEV = process.env.NODE_ENV === 'development'; | ||
const globalConfig = configLoader.getGlobalConfig(); | ||
|
||
const DATABASE_URL = | ||
process.env.DATABASE_URL || | ||
'postgres://paraswap:[email protected]:32780/volume_tracker'; | ||
const logger = global.LOGGER(); | ||
|
||
const DATABASE_NAME = process.env.DATABASE_NAME || 'volume_tracker'; | ||
const DATABASE_NAME = 'volume_tracker'; | ||
|
||
export class Database { | ||
sequelize: Sequelize; | ||
|
@@ -22,14 +20,15 @@ export class Database { | |
} | ||
|
||
// create a volume-tracker DB if it doesn't exist already | ||
const connectionStringParts = DATABASE_URL.split('/'); | ||
const connectionStringParts = globalConfig.databaseUrl.split('/'); | ||
const connectionStringDBName = | ||
connectionStringParts[connectionStringParts.length - 1]; | ||
if (connectionStringDBName !== DATABASE_NAME) { | ||
logger.info( | ||
'Database name in connection string is different than expected', | ||
); | ||
const client = new Client({ connectionString: DATABASE_URL }); | ||
|
||
const client = new Client({ connectionString: globalConfig.databaseUrl }); | ||
await client.connect(); | ||
try { | ||
await client.query(`CREATE DATABASE ${DATABASE_NAME};`); | ||
|
@@ -62,7 +61,7 @@ export class Database { | |
}); | ||
|
||
try { | ||
logger.info('Connecting to database...'); | ||
logger.info('Connecting to database...', connectionStringDBName); | ||
await this.sequelize.authenticate(); | ||
logger.info('Connected to database'); | ||
} catch (e) { | ||
|
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,18 @@ | ||
import { EnvironmentError } from './errors/environment-error'; | ||
|
||
function getEnvOrThrow( | ||
name: string, | ||
defaultValue: string | undefined = undefined, | ||
) { | ||
const value = process.env[name] || defaultValue; | ||
if (value === undefined) { | ||
throw new EnvironmentError(name); | ||
} | ||
return value; | ||
} | ||
export const SERVICE_CONFIGURATION_SERVICE_HTTP = getEnvOrThrow( | ||
'SERVICE_CONFIGURATION_SERVICE_HTTP', | ||
); | ||
export const NODE_ENV = getEnvOrThrow('NODE_ENV', 'development'); | ||
export const IS_DEV = NODE_ENV === 'development'; | ||
export const PORT = parseInt(getEnvOrThrow('PORT', '3236'), 10); |
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,6 @@ | ||
export class ApplicationError extends Error { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message: string, public isLogged: boolean = false) { | ||
super(message); | ||
} | ||
} |
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,8 @@ | ||
import { ApplicationError } from './application-error'; | ||
|
||
export class ConfigError extends ApplicationError { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
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,8 @@ | ||
import { ApplicationError } from './application-error'; | ||
|
||
export class DatabaseError extends ApplicationError { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
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,10 @@ | ||
import { DatabaseError } from './database-error'; | ||
|
||
// This error indicates only that we want to rollback, but not send the user InternalServerError | ||
// So this error should be caught after transaction and later returned the value you want | ||
export class DatabaseRollbackError extends DatabaseError { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message?: string) { | ||
super(message || 'Database Rollback'); | ||
} | ||
} |
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,7 @@ | ||
import { ApplicationError } from './application-error'; | ||
|
||
export class EnvironmentError extends ApplicationError { | ||
constructor(key: string) { | ||
super(`Required environment variable '${key}' expected!`); | ||
} | ||
} |
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,13 @@ | ||
export { ApplicationError } from './application-error'; | ||
|
||
export { EnvironmentError } from './environment-error'; | ||
export { DatabaseError } from './database-error'; | ||
export { ConfigError } from './config-error'; | ||
export { LimitOrderError } from './limit-order-error'; | ||
|
||
export { RestError } from './rest-errors/rest-error'; | ||
export { BadRequestError } from './rest-errors/bad-request-error'; | ||
export { InternalServerError } from './rest-errors/internal-server-error'; | ||
export { NotFoundError } from './rest-errors/not-found-error'; | ||
|
||
export { ValidationError } from './rest-errors/validation-error'; |
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,8 @@ | ||
import { ApplicationError } from './application-error'; | ||
|
||
export class LimitOrderError extends ApplicationError { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
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,8 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { ClientSideError } from './client-side-error'; | ||
|
||
export class BadRequestError extends ClientSideError { | ||
constructor(message: string) { | ||
super(message, StatusCodes.BAD_REQUEST); | ||
} | ||
} |
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,7 @@ | ||
import { ApplicationError } from '../application-error'; | ||
|
||
export class BlackListError extends ApplicationError { | ||
constructor(message: string, readonly statusCode: number) { | ||
super(message); | ||
} | ||
} |
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,8 @@ | ||
import { RestError } from './rest-error'; | ||
|
||
export class ClientSideError extends RestError { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message: string, statusCode: number) { | ||
super(message, statusCode); | ||
} | ||
} |
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,8 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { RestError } from './rest-error'; | ||
|
||
export class InternalServerError extends RestError { | ||
constructor(message: string) { | ||
super(message, StatusCodes.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this project is open and meant for anyone to run it and challenge our computations I think it's important to make this an easy road.
Perhaps we could allow a local config only mode ?
Could be done by constructing a default config object where keys would be picked from env var.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for sure this one of my next task.