Skip to content

Commit

Permalink
fix: adding env to configuration service
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-Amas committed Jul 4, 2022
1 parent 1a3ff49 commit 2e5e7c3
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { configLoader } from '../../../src/config';
import { covalentClient } from '../../../src/lib/utils/data-providers-clients';
import {
CovalentAPI,
CovalentTransaction,
GasRefundTransaction,
} from '../types';

const globalConfig = configLoader.getGlobalConfig();

interface GetContractTXsByNetworkInput {
chainId: number;
chainId: number
contract: string;
startTimestamp: number;
endTimestamp: number;
Expand All @@ -31,7 +34,6 @@ export const covalentGetTXsForContract = async ({
contract,
});

const { COVALENT_API_KEY } = process.env;
const path = (page: number) => {
/* Covalent API only has time relative pagination for tx scanning (give me tx within last X seconds).
* We take a safety margin to counter possible edge case of relative - not absolute - range bounds
Expand All @@ -58,7 +60,7 @@ export const covalentGetTXsForContract = async ({
throw new Error('only query historic data');
}

return `/${chainId}/address/${contract}/transactions_v2/?key=${COVALENT_API_KEY}&no-logs=true&page-number=${page}&page-size=1000&block-signed-at-limit=${startSecondsAgo}&block-signed-at-span=${duration}&match={"to_address": "${contract}"}`;
return `/${chainId}/address/${contract}/transactions_v2/?key=${globalConfig.covalentV1ApiKey}&no-logs=true&page-number=${page}&page-size=1000&block-signed-at-limit=${startSecondsAgo}&block-signed-at-span=${duration}&match={"to_address": "${contract}"}`;
};

// todo: better would be to first call the end point with page-size=0 just to get the total number of items, and then construct many request promises and run concurrently - currently this isn't possible (as `total_count` is null) in the covalent api but scheduled
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type GlobalConfig = {
apiAplcapiHttp: string;
apiKeyAplcapi: string;
databaseUrl: string;
apiKeyCaptcha: string;
apiKeySubmitAccount: string;
};

Expand Down
20 changes: 10 additions & 10 deletions src/database.ts
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;
Expand All @@ -22,14 +20,16 @@ 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 });

console.log(globalConfig.databaseUrl);
const client = new Client({ connectionString: globalConfig.databaseUrl });
await client.connect();
try {
await client.query(`CREATE DATABASE ${DATABASE_NAME};`);
Expand Down Expand Up @@ -62,7 +62,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) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export const Balancer_80PSP_20WETH_poolId =
'0xcb0e14e96f2cefa8550ad8e4aea344f211e5061d00020000000000000000011a';
export const Balancer_80PSP_20WETH_address =
Balancer_80PSP_20WETH_poolId.substring(0, 42); // or 0xcb0e14e96f2cefa8550ad8e4aea344f211e5061d

export const CONFIG_VOLUME_TRACKER_INIT_TIME_1 =16198272
13 changes: 7 additions & 6 deletions src/lib/onboarding/beta-tester.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import axios from 'axios';
import * as jwt from 'jsonwebtoken';
import { assert } from 'ts-essentials';
import { configLoader } from '../../config';
import { AuthToken } from './types';

const config = configLoader.getGlobalConfig();

const logger = global.LOGGER('BetaTest');

const _pk = process.env.APLCAPI_KEY;
const baseUrl = process.env.APLCAPI_BASE_URL;
const keyid = 'SQ994H8QHA';
const issuerId = '2e01237d-0848-46cc-a3ac-b0f6245eb42b';

export function generateAuthToken(): AuthToken {
assert(_pk, 'set APLCAPI_KEY env var');
assert(config.apiKeyAplcapi, 'set APLCAPI_KEY env var');

const pk = _pk.replace(/\\n/g, '\n');
const pk = config.apiKeyAplcapi.replace(/\\n/g, '\n');

const iat = Math.floor(Date.now() / 1000);
const exp = iat + 15 * 60;
Expand Down Expand Up @@ -46,11 +47,11 @@ export async function createTester({
email: string;
authToken: string;
}) {
assert(baseUrl, 'set APLCAPI_BASE_URL env var');
assert(config.apiAplcapiHttp, 'set APLCAPI_BASE_URL env var');

try {
await axios.post(
baseUrl + '/v1/betaTesters',
config.apiAplcapiHttp + '/v1/betaTesters',
{
data: {
attributes: {
Expand Down
21 changes: 11 additions & 10 deletions src/lib/onboarding/mail-service-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as _ from 'lodash';
import { assert } from 'ts-essentials';
import { configLoader } from '../../config';
import { constructHttpClient } from '../utils/http-client';
import {
AccountCreationError,
Expand All @@ -16,7 +17,7 @@ import {

const logger = global.LOGGER('MailService');

const { MAIL_SERVICE_BASE_URL, MAIL_SERVICE_API_KEY } = process.env;
const globalConfig = configLoader.getGlobalConfig();

const mailServiceClient = constructHttpClient({
cacheOptions: {
Expand Down Expand Up @@ -54,10 +55,10 @@ export async function createNewAccount(
account: AccountToCreate,
isVerified: boolean,
): Promise<RegisteredAccount> {
assert(MAIL_SERVICE_BASE_URL, 'set MAIL_SERVICE_BASE_URL env var');
assert(MAIL_SERVICE_API_KEY, 'set MAIL_SERVICE_API_KEY env var');
assert(globalConfig.apiPrefineryHttp, 'set MAIL_SERVICE_BASE_URL env var');
assert(globalConfig.apiKeyPrefinery, 'set MAIL_SERVICE_API_KEY env var');

const apiUrl = `${MAIL_SERVICE_BASE_URL}/betas/17942/testers?api_key=${MAIL_SERVICE_API_KEY}`;
const apiUrl = `${globalConfig.apiPrefineryHttp}/betas/17942/testers?api_key=${globalConfig.apiKeyPrefinery}`;

const createdAccount = {
...account,
Expand Down Expand Up @@ -91,10 +92,10 @@ export async function createNewAccount(
export async function removeUserFromWaitlist({
uuid,
}: Pick<RegisteredAccount, 'uuid'>): Promise<void> {
assert(MAIL_SERVICE_BASE_URL, 'set MAIL_SERVICE_BASE_URL env var');
assert(MAIL_SERVICE_API_KEY, 'set MAIL_SERVICE_API_KEY env var');
assert(globalConfig.apiPrefineryHttp, 'set MAIL_SERVICE_BASE_URL env var');
assert(globalConfig.apiKeyPrefinery, 'set MAIL_SERVICE_API_KEY env var');

const apiUrl = `${MAIL_SERVICE_BASE_URL}/betas/17942/testers/${uuid}?api_key=${MAIL_SERVICE_API_KEY}`;
const apiUrl = `${globalConfig.apiPrefineryHttp}/betas/17942/testers/${uuid}?api_key=${globalConfig.apiKeyPrefinery}`;

try {
await mailServiceClient.delete(apiUrl);
Expand All @@ -106,10 +107,10 @@ export async function removeUserFromWaitlist({
export async function fetchAccountByUUID({
uuid,
}: Pick<RegisteredAccount, 'uuid'>): Promise<RegisteredAccount> {
assert(MAIL_SERVICE_BASE_URL, 'set MAIL_SERVICE_BASE_URL env var');
assert(MAIL_SERVICE_API_KEY, 'set MAIL_SERVICE_API_KEY env var');
assert(globalConfig.apiPrefineryHttp, 'set MAIL_SERVICE_BASE_URL env var');
assert(globalConfig.apiKeyPrefinery, 'set MAIL_SERVICE_API_KEY env var');

const apiUrl = `${MAIL_SERVICE_BASE_URL}/betas/17942/testers/${uuid}?api_key=${MAIL_SERVICE_API_KEY}`;
const apiUrl = `${globalConfig.apiPrefineryHttp}/betas/17942/testers/${uuid}?api_key=${globalConfig.apiKeyPrefinery}`;

try {
const { data: registeredAccount } =
Expand Down
7 changes: 5 additions & 2 deletions src/lib/onboarding/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
} from './errors';
import { Utils } from '../utils';
import { isAddress } from '@ethersproject/address';
import { configLoader } from '../../config';

const globalConfig = configLoader.getGlobalConfig();

const logger = global.LOGGER('OnboardingRouter');

Expand Down Expand Up @@ -77,11 +80,11 @@ router.get('/check-eligibility/:address/:blockNumber', async (req, res) => {
router.post('/submit-verified', async (req, res) => {
try {
assert(
process.env.SUBMIT_ACCOUNT_API_KEY,
globalConfig.apiKeySubmitAccount,
'set SUBMIT_ACCOUNT_API_KEY env var',
);

if (req.headers['x-auth-token'] !== process.env.SUBMIT_ACCOUNT_API_KEY)
if (req.headers['x-auth-token'] !== globalConfig.apiKeySubmitAccount)
throw new AuthorizationError();

const account = req.body;
Expand Down
7 changes: 5 additions & 2 deletions src/lib/onboarding/verification-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { assert } from 'ts-essentials';
import { URLSearchParams } from 'url';
import { configLoader } from '../../config';
import { constructHttpClient } from '../utils/http-client';
import { VerificationError } from './errors';

const config = configLoader.getGlobalConfig();

const logger = global.LOGGER('verificationService');

const responseVerificationClient = constructHttpClient({
Expand All @@ -15,10 +18,10 @@ const responseVerificationClient = constructHttpClient({
});

export const verifyKey = async (key: string) => {
assert(process.env.CAPTCHA_SECRET_KEY, 'CAPTCHA_SECRET_KEY should be set');
assert(config.apiKeyCaptcha, 'CAPTCHA_SECRET_KEY should be set');

const params = new URLSearchParams();
params.append('secret', process.env.CAPTCHA_SECRET_KEY);
params.append('secret', config.apiKeyCaptcha);
params.append('response', key);

try {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/utils/covalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { assert } from 'ts-essentials';
import { URLSearchParams } from 'url';
import { queryPaginatedData, QueryPaginatedDataParams } from './helpers';
import { covalentClient } from './data-providers-clients';
import { configLoader } from '../../config';

const COVALENT_API_KEY = process.env.COVALENT_API_KEY || 'ckey_docs'; // public, is rate-limited and unreliable
const config = configLoader.getGlobalConfig();

const COVALENT_API_KEY = config.covalentV1ApiKey || 'ckey_docs'; // public, is rate-limited and unreliable

interface TokenHoldersOptions {
token: string;
Expand Down
5 changes: 4 additions & 1 deletion src/lib/utils/data-providers-clients.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { configLoader } from '../../config';
import { constructHttpClient } from './http-client';

const global = configLoader.getGlobalConfig();

export const coingeckoClient = constructHttpClient({
axiosConfig: {
baseURL: 'https://api.coingecko.com/api/v3',
Expand All @@ -14,7 +17,7 @@ export const coingeckoClient = constructHttpClient({

export const covalentClient = constructHttpClient({
axiosConfig: {
baseURL: 'https://api.covalenthq.com/v1',
baseURL: global.covalentV1HttpUrl,
timeout: 30_000,
},
rateLimitOptions: {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/volume-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const DAY = 1000 * 60 * 60 * 24;
export const MAX_PERIOD = 120 * DAY // 60 days

const CACHE_MAX_SIZE = 60 // days
const GC_INTERVAL = process.env.VOLUME_AGGREGATION_GC_INTERVAL || DAY / 2 // 12 hours
const GC_INTERVAL = DAY / 2 // 12 hours

export class VolumesCache {
private volumes: VolumeCache = {}
Expand Down

0 comments on commit 2e5e7c3

Please sign in to comment.