Skip to content

Commit

Permalink
Merge pull request #48 from bancorprotocol/added-linter
Browse files Browse the repository at this point in the history
  • Loading branch information
zavelevsky authored Aug 6, 2023
2 parents 3ad61cc + 0373680 commit 65e0801
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 68 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
],
"prefer-rest-params": "off"
},
"root": true
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
"clean": "rm -rf dist && rm -rf src/abis/types",
"compile-abis": "typechain --target ethers-v5 --out-dir 'src/abis/types' 'src/abis/**/*.json'",
"prebuild": "yarn clean && yarn compile-abis",
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "mocha"
"build": "yarn lint && rollup -c",
"dev": "yarn lint && rollup -c -w",
"test": "yarn lint && mocha",
"lint": "eslint src --ext .ts"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -76,14 +77,17 @@
"@types/mocha": "^10.0.0",
"@types/node": "^18.16.3",
"@types/sinon": "^10.0.15",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"chai": "^4.3.7",
"eslint": "^8.46.0",
"mocha": "^10.1.0",
"rollup": "^3.27.0",
"rollup-plugin-typescript2": "^0.34.1",
"sinon": "^15.0.4",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
"typescript": "^5.0.3"
"typescript": "^5.1.6"
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
Expand Down
10 changes: 8 additions & 2 deletions src/chain-cache/ChainCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,13 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
strategies: EncodedStrategy[],
noPairAddedEvent: boolean = false
): void {
logger.debug('Adding pair with', strategies.length, ' strategies to cache', token0, token1);
logger.debug(
'Adding pair with',
strategies.length,
' strategies to cache',
token0,
token1
);
const key = toPairKey(token0, token1);
if (this._strategiesByPair[key]) {
throw new Error(`Pair ${key} already cached`);
Expand Down Expand Up @@ -428,7 +434,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
);
}
const key = toPairKey(strategy.token0, strategy.token1);
if (!!this._strategiesById[strategy.id.toString()]) {
if (this._strategiesById[strategy.id.toString()]) {
logger.debug(
`Strategy ${strategy.id} already cached, under the pair ${key} - skipping`
);
Expand Down
2 changes: 1 addition & 1 deletion src/chain-cache/ChainSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class ChainSync {
logger.debug('_detectReorg called');
const blocksMetadata: BlockMetadata[] = this._chainCache.blocksMetadata;
const numberToBlockMetadata: { [key: number]: BlockMetadata } = {};
for (let blockMetadata of blocksMetadata) {
for (const blockMetadata of blocksMetadata) {
const { number, hash } = blockMetadata;
if (number > currentBlock) {
logger.log(
Expand Down
1 change: 1 addition & 0 deletions src/chain-cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TokenPair } from '../common/types';

export type EventMap = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: (...args: any[]) => void;
};

Expand Down
6 changes: 6 additions & 0 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const globalObject = (() => {

function getVerbosityLevel(): number {
if (globalObject !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Number((globalObject as any).CARBON_DEFI_SDK_VERBOSITY) || 0;
}

Expand All @@ -32,6 +33,7 @@ function shouldConvertBigNumbersToStrings(): boolean {

const originalLog = console.log;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function convertBigNumbersToStrings(obj: any): any {
if (obj instanceof BigNumber) {
return obj.toString();
Expand All @@ -51,6 +53,7 @@ function convertBigNumbersToStrings(obj: any): any {
}

if (shouldConvertBigNumbersToStrings()) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
console.debug = (...args: any[]) => {
const convertedArgs = args.map(convertBigNumbersToStrings);
originalLog.apply(console, convertedArgs);
Expand All @@ -62,14 +65,17 @@ export class Logger {
this._prefix = `[SDK][${file}]:`;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public error(...args: any[]) {
console.error(this._prefix, ...args);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public log(...args: any[]) {
console.log(this._prefix, ...args);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public debug(...args: any[]) {
isVerbose() && console.debug(this._prefix, ...args);
}
Expand Down
5 changes: 4 additions & 1 deletion src/contracts-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface MultiCall {
contractAddress: string;
interface: Interface;
methodName: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
methodParameters: any[];
}

Expand Down Expand Up @@ -38,7 +39,9 @@ export const multicall = async (
call.returnData
);
});
} catch (error) {}
} catch (error) {
/* empty */
}
};

export const isETHAddress = (address: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/strategy-management/Toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export class Toolkit {
tradeByTargetAmount
);

let actionsWei: MatchActionBNStr[] = Toolkit.getMatchActions(
const actionsWei: MatchActionBNStr[] = Toolkit.getMatchActions(
amountWei,
tradeByTargetAmount,
orders,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/numerics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const mulDiv = (x: BigNumber, y: BigNumber, z: BigNumber) =>
y.eq(z) ? x : x.mul(y).div(z);

function trimDecimal(input: string, precision: number): string {
let decimalIdx = input.indexOf('.');
const decimalIdx = input.indexOf('.');
if (decimalIdx !== -1) {
return input.slice(0, decimalIdx + precision + 1);
}
Expand Down
45 changes: 2 additions & 43 deletions src/utils/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,17 @@ import {
TradeActionBNStr,
} from '../common/types';

/**
* Helper utility to deserialize a value that was contains BigNumber instances
* @returns
*/
export const deserialize = (value: any): any => {
if (
typeof value === 'object' &&
value !== null &&
value.type === 'BigNumber' &&
value.hex !== null
) {
return BigNumber.from(value.hex);
}

if (Array.isArray(value)) {
return value.map(deserialize);
}

if (typeof value === 'object' && value !== null) {
const deserializedObj: { [key: string]: any } = {};
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
deserializedObj[key] = deserialize(value[key]);
}
}
return deserializedObj;
}

return value;
};

export const replaceBigNumbersWithStrings = <T>(
obj: T
): RetypeBigNumberToString<T> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function replace(obj: any): any {
if (BigNumber.isBigNumber(obj)) {
return obj.toString();
}

if (typeof obj === 'object' && obj !== null) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const newObj: any = Array.isArray(obj) ? [] : {};
for (const key in obj) {
newObj[key] = replace(obj[key]);
Expand Down Expand Up @@ -113,18 +84,6 @@ export const matchActionBNToStr = (action: MatchAction): MatchActionBNStr => {
return replaceBigNumbersWithStrings(action);
};

export const matchActionStrToBN = (action: MatchActionBNStr): MatchAction => {
return {
id: BigNumber.from(action.id),
input: BigNumber.from(action.input),
output: BigNumber.from(action.output),
};
};

export const tradeActionBNToStr = (action: TradeAction): TradeActionBNStr => {
return replaceBigNumbersWithStrings(action);
};

export const tradeActionStrToBN = (action: TradeActionBNStr): TradeAction => {
return {
strategyId: BigNumber.from(action.strategyId),
Expand Down
Loading

0 comments on commit 65e0801

Please sign in to comment.