From 5f259e8e3591926e9ee6b33f03f0ec2f06731264 Mon Sep 17 00:00:00 2001 From: smart_ex Date: Fri, 24 Jun 2022 18:24:00 +1000 Subject: [PATCH] Optional provider arg. Update eslint conf --- .eslintrc.json | 30 ++++++++++++++++++++++++------ index.d.ts | 4 ++++ package.json | 6 +++++- src/Transaction.js | 8 ++++---- src/TxManager.js | 4 ++-- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 41bfff8..6e95220 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,7 +11,7 @@ "SharedArrayBuffer": "readonly" }, "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": 2020 }, "rules": { "indent": [ @@ -21,12 +21,30 @@ "SwitchCase": 1 } ], - "linebreak-style": ["error", "unix"], - "quotes": ["error", "single", { "avoidEscape": true }], - "semi": ["error", "never"], - "object-curly-spacing": ["error", "always"], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single", + { + "avoidEscape": true + } + ], + "semi": [ + "error", + "never" + ], + "object-curly-spacing": [ + "error", + "always" + ], "require-await": "error", - "comma-dangle": ["error", "only-multiline"], + "comma-dangle": [ + "error", + "only-multiline" + ], "space-before-function-paren": [ "error", { diff --git a/index.d.ts b/index.d.ts index f739513..c5b1291 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,7 @@ import PromiEvent from 'web3-core-promievent' import { GasPriceOracle } from 'gas-price-oracle' import { Mutex } from 'async-mutex' import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types' +import { JsonRpcProvider } from '@ethersproject/providers' export interface TransactionData { to: string @@ -45,6 +46,7 @@ export interface TxManagerParams { broadcastNodes?: string[] config?: TxManagerConfig gasPriceOracleConfig?: GasOracleOptions + provider: JsonRpcProvider } export class TxManager { @@ -81,7 +83,9 @@ type TEventEmitter = typeof EventEmitter declare interface TxManagerEventEmmiter extends TEventEmitter { on(event: U, listener: MessageEvents[U]): this + on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise + emit(event: U, ...args: Parameters): boolean } diff --git a/package.json b/package.json index 0a3b1bb..9790650 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "", "main": "index.js", "types": "index.d.ts", + "engines": { + "node": ">=14.0.0" + }, "scripts": { "eslint": "eslint --ext .js --ignore-path .gitignore .", "prettier:check": "prettier --check . --config .prettierrc", @@ -19,7 +22,8 @@ "url": "git://github.com/tornadocash/tx-manager.git" }, "files": [ - "src/*" + "src/*", + "index.d.ts" ], "dependencies": { "async-mutex": "^0.2.4", diff --git a/src/Transaction.js b/src/Transaction.js index 7bc791f..8589c0a 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -26,14 +26,13 @@ const sameTxErrors = [ 'Transaction with the same hash was already imported.', 'already known', 'AlreadyKnown', - 'Known transaction' + 'Known transaction', ] class Transaction { constructor(tx, manager) { this.manager = manager this.tx = { ...tx } - this.nonce = manager._nonce this._promise = PromiEvent() this._emitter = this._promise.eventEmitter this.executed = false @@ -73,7 +72,7 @@ class Transaction { if (!tx.gasLimit) { const estimatedGasLimit = await this._estimateGas(tx) tx.gasLimit = min( - estimatedGasLimit.mul(this.manager.config.GAS_LIMIT_MULTIPLIER * 100).div(100), + estimatedGasLimit.mul(Math.floor(this.manager.config.GAS_LIMIT_MULTIPLIER * 100)).div(100), this.manager.config.BLOCK_GAS_LIMIT, ) } @@ -301,7 +300,8 @@ class Transaction { } _handleRpcError(e, method) { - if (e.error.error) { + console.log('_handleRpcError', e, method) + if (e.error?.error) { // Sometimes ethers wraps known errors, unwrap it in this case e = e.error } diff --git a/src/TxManager.js b/src/TxManager.js index 85a774e..6c7806b 100644 --- a/src/TxManager.js +++ b/src/TxManager.js @@ -22,10 +22,10 @@ const defaultConfig = { } class TxManager { - constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {}, gasPriceOracleConfig = {} }) { + constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {}, gasPriceOracleConfig = {}, provider }) { this.config = Object.assign({ ...defaultConfig }, config) this._privateKey = privateKey.startsWith('0x') ? privateKey : '0x' + privateKey - this._provider = new ethers.providers.JsonRpcProvider(rpcUrl) + this._provider = provider || new ethers.providers.JsonRpcProvider(rpcUrl) this._wallet = new ethers.Wallet(this._privateKey, this._provider) this.address = this._wallet.address this._broadcastNodes = broadcastNodes