Skip to content

Commit

Permalink
chore: enable console.log ESlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Verisana committed Dec 26, 2022
1 parent 7680ff7 commit 88fb8a7
Show file tree
Hide file tree
Showing 9 changed files with 893 additions and 53 deletions.
57 changes: 57 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports = {
env: {
node: true,
},
extends: [
// Enable other rules gradually when you really to rewrite everything

// 'eslint:recommended',
// 'airbnb-base',
// 'plugin:import/typescript',
// 'plugin:@typescript-eslint/eslint-recommended',
// 'plugin:@typescript-eslint/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'ES2019',
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
'import/no-unresolved': 0,
'import/prefer-default-export': 0,
'import/extensions': 0,
'no-underscore-dangle': 0,
'no-shadow': 'off',
// '@typescript-eslint/no-shadow': ['error'],
'no-plusplus': 'off',
'no-continue': 'off',
'no-param-reassign': 'off',
'no-console': 'error',
'max-classes-per-file': 'off',
'no-await-in-loop': 'off',
'@typescript-eslint/no-unused-vars': ['off'],
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/no-non-null-assertion': 0,
'class-methods-use-this': 0,
'no-restricted-syntax': [
'error',
{
selector: 'ForInStatement',
message:
'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
},
{
selector: 'LabeledStatement',
message:
'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
},
{
selector: 'WithStatement',
message:
'`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
},
],
},
};
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx pretty-quick --staged && yarn build
yarn checks && yarn build
22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
"@types/jest": "26.0.24",
"@types/lodash": "4.14.178",
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"axios": "0.26.0",
"change-case": "^4.1.2",
"dotenv": "16.0.0",
"eslint": "^8.30.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"express": "^4.18.2",
"husky": "7.0.1",
"husky": "^8.0.2",
"jest": "^29.0.3",
"prettier": "2.3.2",
"pretty-quick": "3.1.1",
"ts-jest": "^29.0.1",
"prettier": "^2.8.1",
"pretty-quick": "^3.1.3",
"ts-jest": "^29.0.3",
"ts-node": "^10.6.0",
"typescript": "4.6.4",
"yargs": "^17.0.1"
Expand All @@ -36,10 +42,15 @@
"test-integration": "ts-node scripts/dex-integration.ts test",
"build": "tsc",
"test": "jest",
"prepare": "husky install && yarn build"
"prepare": "husky install && yarn build",
"check:pq": "pretty-quick --staged",
"check:tsc": "tsc",
"check:es": "eslint \"src/**/*.ts\" --ignore-pattern=\"*.test.ts\"",
"checks": "yarn check:pq && yarn check:tsc && yarn check:es"
},
"dependencies": {
"@0x/utils": "^4.5.2",
"@paraswap/core": "1.0.3",
"async": "^3.2.4",
"bignumber.js": "9.1.0",
"cross-fetch": "^3.1.5",
Expand All @@ -50,7 +61,6 @@
"lodash": "4.17.21",
"log4js": "6.6.1",
"node-cache": "^5.1.2",
"@paraswap/core": "1.0.3",
"ts-essentials": "9.1.2",
"web3": "1.6.0",
"web3-core": "1.6.0",
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export enum Network {
ARBITRUM = 42161,
OPTIMISM = 10,
}

export const SUBGRAPH_TIMEOUT = 20 * 1000;

export enum LIMIT_ORDER_PROVIDERS {
Expand Down
5 changes: 4 additions & 1 deletion src/dex-helper/dummy-dex-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Address, LoggerConstructor, Token } from '../types';
import { StaticJsonRpcProvider, Provider } from '@ethersproject/providers';
import multiABIV2 from '../abi/multi-v2.json';
import log4js from 'log4js';
import { getLogger } from '../lib/log4js';
import Web3 from 'web3';
import { Contract } from 'web3-eth-contract';
import { generateConfig, ConfigHelper } from '../config';
Expand All @@ -19,6 +20,8 @@ import { Response, RequestConfig } from './irequest-wrapper';
import { BlockHeader } from 'web3-eth';
import { PromiseScheduler } from '../lib/promise-scheduler';

const logger = getLogger('DummyDexHelper');

// This is a dummy cache for testing purposes
class DummyCache implements ICache {
private storage: Record<string, string> = {};
Expand Down Expand Up @@ -170,7 +173,7 @@ class DummyBlockManager implements IBlockManager {
contractAddress: Address | Address[],
afterBlockNumber: number,
): void {
console.log(
logger.info(
`Subscribed to logs ${subscriber.name} ${contractAddress} ${afterBlockNumber}`,
);
subscriber.isTracking = () => true;
Expand Down
2 changes: 1 addition & 1 deletion src/dex/swaap-v1/swaap-v1-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class SwaapV1Pool extends StatefulEventSubscriber<SwaapV1PoolState> {
const nameOrSignatureOrTopic: string = log.topics[0];
if (isHexString(nameOrSignatureOrTopic)) {
const topichash = nameOrSignatureOrTopic.toLowerCase();
for (const name in SwaapV1Pool.poolInterface.events) {
for (const name of Object.keys(SwaapV1Pool.poolInterface.events)) {
if (topichash === SwaapV1Pool.poolInterface.getEventTopic(name)) {
isAnonymousEvent = false;
break;
Expand Down
17 changes: 10 additions & 7 deletions src/dex/uniswap-v3/scripts/measure-calc-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ request worst case scenario.
*/
import * as dotenv from 'dotenv';
import { getLogger } from '../../../lib/log4js';
import { DeepReadonly } from 'ts-essentials';
dotenv.config();
import { Network, SwapSide } from '../../../constants';
Expand All @@ -13,6 +14,8 @@ import { uniswapV3Math } from '../contract-math/uniswap-v3-math';
import { PoolState } from '../types';
import { UniswapV3 } from '../uniswap-v3';

const logger = getLogger('UniswapV3MeasureScript');

const runsNumber = 1000;
const printFrequency = 100;
const network = Network.MAINNET;
Expand Down Expand Up @@ -116,7 +119,7 @@ const executeOnlySyncOperations = async (states: DeepReadonly<PoolState>[]) => {

const aggregateAndPrintMeasures = (measures: number[]) => {
const sum = measures.reduce((a, b) => a + b);
console.log(
logger.info(
`Measured ${measures.length}. Average = ${(sum / measures.length).toFixed(
2,
)} ms. Max = ${Math.max(...measures)} ms. Min = ${Math.min(
Expand All @@ -141,29 +144,29 @@ const runOneSuite = async (func: Function) => {
}
counter++;
}
console.log('\n');
logger.info('\n');
aggregateAndPrintMeasures(measures);
};

(async function main() {
console.log(`Started measurement script for ${runsNumber} runs...\n`);
logger.info(`Started measurement script for ${runsNumber} runs...\n`);

const blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();

// Fetch all states and calculation variables before measurement
await executeGetPricesVolume(blockNumber);

console.log('\n');
logger.info('\n');

const states = Object.values(uniV3.eventPools).map(
ep => ep!.getState(blockNumber)!,
);

console.log(`\nRun for full calculation cycles\n`);
logger.info(`\nRun for full calculation cycles\n`);
await runOneSuite(executeGetPricesVolume.bind(undefined, blockNumber));

console.log(`\nRun for only sync cycles\n`);
logger.info(`\nRun for only sync cycles\n`);
await runOneSuite(executeOnlySyncOperations.bind(undefined, states));

console.log(`Tests ended`);
logger.info(`Tests ended`);
})();
19 changes: 11 additions & 8 deletions src/stateful-event-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ export abstract class StatefulEventSubscriber<State>
): AsyncOrSync<DeepReadonly<State>>;

restart(blockNumber: number): void {
for (const bn in this.stateHistory) {
if (+bn >= blockNumber) break;
for (const _bn of Object.keys(this.stateHistory)) {
const bn = +_bn;
if (bn >= blockNumber) break;
delete this.stateHistory[bn];
}
if (this.state && this.stateBlockNumber < blockNumber) {
Expand Down Expand Up @@ -255,8 +256,9 @@ export abstract class StatefulEventSubscriber<State>
}
//Find the last state before the blockNumber of the logs
let stateBeforeLog: DeepReadonly<State> | undefined;
for (const bn in this.stateHistory) {
if (+bn >= blockNumber) break;
for (const _bn of Object.keys(this.stateHistory)) {
const bn = +_bn;
if (bn >= blockNumber) break;
stateBeforeLog = this.stateHistory[bn];
}
//Ignoring logs if there's no older state to play them onto
Expand Down Expand Up @@ -311,10 +313,10 @@ export abstract class StatefulEventSubscriber<State>
if (this.invalid) {
let lastBn = undefined;
//loop in the ascending order of the blockNumber. V8 property when object keys are number.
for (const bn in this.stateHistory) {
for (const bn of Object.keys(this.stateHistory)) {
const bnAsNumber = +bn;
if (bnAsNumber > blockNumber) {
delete this.stateHistory[bn];
delete this.stateHistory[+bn];
} else {
lastBn = bnAsNumber;
}
Expand All @@ -327,7 +329,8 @@ export abstract class StatefulEventSubscriber<State>
}
} else {
//Keep the current state in this.state and in the history
for (const bn in this.stateHistory) {
for (const _bn of Object.keys(this.stateHistory)) {
const bn = +_bn;
if (+bn > blockNumber && +bn !== this.stateBlockNumber) {
delete this.stateHistory[bn];
}
Expand Down Expand Up @@ -455,7 +458,7 @@ export abstract class StatefulEventSubscriber<State>
}
const minBlockNumberToKeep = this.stateBlockNumber - MAX_BLOCKS_HISTORY;
let lastBlockNumber: number | undefined;
for (const bn in this.stateHistory) {
for (const bn of Object.keys(this.stateHistory)) {
if (+bn <= minBlockNumberToKeep) {
if (lastBlockNumber) delete this.stateHistory[lastBlockNumber];
}
Expand Down
Loading

0 comments on commit 88fb8a7

Please sign in to comment.