diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..2a7d4513 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +node_modules +dist +coverage +*.test.ts + diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..8aeba5b2 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,73 @@ +{ + "root": true, + "parser": "@typescript-eslint/parser", + "plugins": ["@typescript-eslint", "jest"], + "overrides": [ + { + "files": ["*.ts"], + "extends": ["@metamask/eslint-config-typescript"] + }, + { + "files": ["*.d.ts"], + "parserOptions": { + "sourceType": "script" + } + }, + { + "files": ["*.js"], + "parserOptions": { + "sourceType": "script" + }, + "extends": ["@metamask/eslint-config-nodejs"] + }, + { + "files": ["*.test.ts", "*.test.js"], + "extends": ["@metamask/eslint-config-jest"] + }, + { + "files": ["src/SwapsController.ts"], + "rules": { + "accessor-pairs": "off", + "consistent-return": "off", + "no-async-promise-executor": "off", + "@typescript-eslint/prefer-optional-chain": "off", + "no-restricted-syntax": "off", + "import/no-named-as-default": "off", + } + }, + { + "files": ["src/SwapsController.test.ts"], + "rules": { + "no-new": "off" + } + }, + { + "files": ["src/swapsInterfaces.ts"], + "rules": { + "@typescript-eslint/naming-convention": "off" + } + }, + { + "files": ["src/swapsUtil.ts"], + "rules": { + "consistent-return": "off", + "no-negated-condition": "off", + "@typescript-eslint/naming-convention": "off" + } + }, + { + "files": ["src/swapsUtil.test.ts"], + "rules": { + "jest/require-to-throw-message": "off", + "@typescript-eslint/naming-convention": "off", + "no-restricted-globals": "off" + } + } + ], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "plugin:jest/recommended", + "@metamask/eslint-config" + ] +} diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index b71e8558..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,78 +0,0 @@ -module.exports = { - root: true, - - extends: ['@metamask/eslint-config'], - - overrides: [ - { - files: ['*.ts'], - extends: ['@metamask/eslint-config-typescript'], - }, - - { - files: ['*.d.ts'], - parserOptions: { - sourceType: 'script', - }, - }, - - { - files: ['*.js'], - parserOptions: { - sourceType: 'script', - }, - extends: ['@metamask/eslint-config-nodejs'], - }, - - { - files: ['*.test.ts', '*.test.js'], - extends: ['@metamask/eslint-config-jest'], - }, - - // TODO: Enable these - { - files: ['src/SwapsController.ts'], - rules: { - 'accessor-pairs': 'off', - 'consistent-return': 'off', - 'no-async-promise-executor': 'off', - '@typescript-eslint/prefer-optional-chain': 'off', - // ignoring this to avoid changing the controller interface - 'no-restricted-syntax': 'off', - }, - }, - { - files: ['src/SwapsController.test.ts'], - rules: { - 'no-new': 'off', - }, - }, - { - files: ['src/swapsInterfaces.ts'], - rules: { - // disabling this since enums affected are used in other controllers/clients - '@typescript-eslint/naming-convention': 'off', - }, - }, - { - files: ['src/swapsUtil.ts'], - rules: { - 'consistent-return': 'off', - 'no-negated-condition': 'off', - // disabling this since enums affected are used in other controllers/clients - '@typescript-eslint/naming-convention': 'off', - }, - }, - { - files: ['src/swapsUtil.test.ts'], - rules: { - 'jest/require-to-throw-message': 'off', - // disabling this since enums affected are used in other controllers/clients - '@typescript-eslint/naming-convention': 'off', - 'no-restricted-globals': 'off', - }, - }, - ], - - ignorePatterns: ['!.eslintrc.js', '!.prettierrc.js', 'dist/'], -}; diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..fa83705a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "quoteProps": "as-needed", + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "all" +} diff --git a/.prettierrc.js b/.prettierrc.js deleted file mode 100644 index b2d98d2e..00000000 --- a/.prettierrc.js +++ /dev/null @@ -1,8 +0,0 @@ -// All of these are defaults except singleQuote, but we specify them -// for explicitness -module.exports = { - quoteProps: 'as-needed', - singleQuote: true, - tabWidth: 2, - trailingComma: 'all', -}; diff --git a/package.json b/package.json index cae6b954..8a976eb3 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "abort-controller": "^3.0.0", "async-mutex": "^0.4.1", "bignumber.js": "^9.0.1", + "bn.js": "^5.2.1", "human-standard-token-abi": "^2.0.0", "web3": "^4.2.2" }, diff --git a/src/SwapsController.ts b/src/SwapsController.ts index d132a94b..00a0b866 100644 --- a/src/SwapsController.ts +++ b/src/SwapsController.ts @@ -21,7 +21,7 @@ import type { Hex } from '@metamask/utils'; import { Mutex } from 'async-mutex'; import { BigNumber } from 'bignumber.js'; import abiERC20 from 'human-standard-token-abi'; -import { Web3 } from 'web3'; +import * as web3 from 'web3'; import type { APIAggregatorMetadata, @@ -58,6 +58,9 @@ import { shouldEnableDirectWrapping, } from './swapsUtil'; +// hack to fix web3 import issue after transpiling +const Web3 = web3.Web3 === undefined ? web3.default : web3.Web3; + // Functions to determine type of the return value from GasFeeController /** diff --git a/src/swapsUtil.ts b/src/swapsUtil.ts index 1c639720..951feaed 100644 --- a/src/swapsUtil.ts +++ b/src/swapsUtil.ts @@ -9,6 +9,7 @@ import type { Transaction } from '@metamask/transaction-controller'; import type { Hex } from '@metamask/utils'; import { add0x } from '@metamask/utils'; import { BigNumber } from 'bignumber.js'; +import { BN } from 'bn.js'; import { ALLOWED_CONTRACT_ADDRESSES, @@ -283,8 +284,8 @@ export async function fetchTradesInfo( to: quote.trade.to, from: quote.trade.from, data: quote.trade.data, - amount: BNToHex(new BigNumber(quote.trade.value)), - gas: BNToHex(quote.maxGas || new BigNumber(MAX_GAS_LIMIT)), + amount: BNToHex(new BN(quote.trade.value)), + gas: BNToHex(new BN(quote.maxGas) || new BN(MAX_GAS_LIMIT)), }); return { diff --git a/tsconfig.json b/tsconfig.json index 1d9aa090..9c85930e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,20 @@ { "compilerOptions": { + "allowJs": true, "declaration": true, "declarationDir": "dist", "inlineSources": true, "esModuleInterop": true, - "lib": ["DOM", "ES2020"], + "forceConsistentCasingInFileNames": true, "module": "CommonJS", "moduleResolution": "Node", "noUnusedLocals": true, "noUnusedParameters": true, "outDir": "dist", - "rootDir": "./src", "sourceMap": true, "strict": true, - "target": "ES2017" + "target": "ES2020" }, - "include": ["src/**/*.ts", "src/**/*.test.ts"] + "include": ["src/**/*.ts"], + "exclude": ["src/**/*.test.ts", "node_modules"] } diff --git a/yarn.lock b/yarn.lock index 7fa1a4bd..a793c3ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2330,9 +2330,9 @@ ecc-jsbn@~0.1.1: safer-buffer "^2.1.0" electron-to-chromium@^1.4.668: - version "1.4.737" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.737.tgz#3a774a58e04980741f65d440f5fdf57af18b6dde" - integrity sha512-QvLTxaLHKdy5YxvixAw/FfHq2eWLUL9KvsPjp0aHK1gI5d3EDuDgITkvj0nFO2c6zUY3ZqVAJQiBYyQP9tQpfw== + version "1.4.738" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.738.tgz#9a7fca98abaee61e20c9c25013d5ce60bb533436" + integrity sha512-lwKft2CLFztD+vEIpesrOtCrko/TFnEJlHFdRhazU7Y/jx5qc4cqsocfVrBg4So4gGe9lvxnbLIoev47WMpg+A== elliptic@^6.5.2, elliptic@^6.5.4: version "6.5.5" @@ -5402,9 +5402,9 @@ proc-log@^3.0.0: integrity sha512-++Vn7NS4Xf9NacaU9Xq3URUuqZETPsf8L4j5/ckhaRYsfPeRyzGw+iDjFhV/Jr3uNmTvvddEJFWh5R1gRgUH8A== proc-log@^4.0.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/proc-log/-/proc-log-4.1.0.tgz#ab6c1552b454e9e467cb58119179e4557d109b34" - integrity sha512-dmQ2iPw2nJMi9/4dpaG1wd0m1GE+K5kW7RGbjy5hoEEGnhPIzsm+klBO5RGGdcoYbWsNtU2KSNAdEldts+icLg== + version "4.2.0" + resolved "https://registry.yarnpkg.com/proc-log/-/proc-log-4.2.0.tgz#b6f461e4026e75fdfe228b265e9f7a00779d7034" + integrity sha512-g8+OnU/L2v+wyiVK+D5fA34J7EH8jZ8DDlvwhRCMxmMj7UCBvxiO1mGeN+36JXIKF4zevU4kRBd8lVgG9vLelA== process-nextick-args@~2.0.0: version "2.0.1"