Optional provider arg. Update eslint conf

This commit is contained in:
smart_ex 2022-06-24 18:24:00 +10:00
parent e37e9c3c8d
commit 5f259e8e35
5 changed files with 39 additions and 13 deletions

View File

@ -11,7 +11,7 @@
"SharedArrayBuffer": "readonly" "SharedArrayBuffer": "readonly"
}, },
"parserOptions": { "parserOptions": {
"ecmaVersion": 2018 "ecmaVersion": 2020
}, },
"rules": { "rules": {
"indent": [ "indent": [
@ -21,12 +21,30 @@
"SwitchCase": 1 "SwitchCase": 1
} }
], ],
"linebreak-style": ["error", "unix"], "linebreak-style": [
"quotes": ["error", "single", { "avoidEscape": true }], "error",
"semi": ["error", "never"], "unix"
"object-curly-spacing": ["error", "always"], ],
"quotes": [
"error",
"single",
{
"avoidEscape": true
}
],
"semi": [
"error",
"never"
],
"object-curly-spacing": [
"error",
"always"
],
"require-await": "error", "require-await": "error",
"comma-dangle": ["error", "only-multiline"], "comma-dangle": [
"error",
"only-multiline"
],
"space-before-function-paren": [ "space-before-function-paren": [
"error", "error",
{ {

4
index.d.ts vendored
View File

@ -5,6 +5,7 @@ import PromiEvent from 'web3-core-promievent'
import { GasPriceOracle } from 'gas-price-oracle' import { GasPriceOracle } from 'gas-price-oracle'
import { Mutex } from 'async-mutex' import { Mutex } from 'async-mutex'
import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types' import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types'
import { JsonRpcProvider } from '@ethersproject/providers'
export interface TransactionData { export interface TransactionData {
to: string to: string
@ -45,6 +46,7 @@ export interface TxManagerParams {
broadcastNodes?: string[] broadcastNodes?: string[]
config?: TxManagerConfig config?: TxManagerConfig
gasPriceOracleConfig?: GasOracleOptions gasPriceOracleConfig?: GasOracleOptions
provider: JsonRpcProvider
} }
export class TxManager { export class TxManager {
@ -81,7 +83,9 @@ type TEventEmitter = typeof EventEmitter
declare interface TxManagerEventEmmiter extends TEventEmitter { declare interface TxManagerEventEmmiter extends TEventEmitter {
on<U extends TxManagerEvents>(event: U, listener: MessageEvents[U]): this on<U extends TxManagerEvents>(event: U, listener: MessageEvents[U]): this
on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise<TransactionReceipt> on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise<TransactionReceipt>
emit<U extends TxManagerEvents>(event: U, ...args: Parameters<MessageEvents[U]>): boolean emit<U extends TxManagerEvents>(event: U, ...args: Parameters<MessageEvents[U]>): boolean
} }

View File

@ -4,6 +4,9 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"engines": {
"node": ">=14.0.0"
},
"scripts": { "scripts": {
"eslint": "eslint --ext .js --ignore-path .gitignore .", "eslint": "eslint --ext .js --ignore-path .gitignore .",
"prettier:check": "prettier --check . --config .prettierrc", "prettier:check": "prettier --check . --config .prettierrc",
@ -19,7 +22,8 @@
"url": "git://github.com/tornadocash/tx-manager.git" "url": "git://github.com/tornadocash/tx-manager.git"
}, },
"files": [ "files": [
"src/*" "src/*",
"index.d.ts"
], ],
"dependencies": { "dependencies": {
"async-mutex": "^0.2.4", "async-mutex": "^0.2.4",

View File

@ -26,14 +26,13 @@ const sameTxErrors = [
'Transaction with the same hash was already imported.', 'Transaction with the same hash was already imported.',
'already known', 'already known',
'AlreadyKnown', 'AlreadyKnown',
'Known transaction' 'Known transaction',
] ]
class Transaction { class Transaction {
constructor(tx, manager) { constructor(tx, manager) {
this.manager = manager this.manager = manager
this.tx = { ...tx } this.tx = { ...tx }
this.nonce = manager._nonce
this._promise = PromiEvent() this._promise = PromiEvent()
this._emitter = this._promise.eventEmitter this._emitter = this._promise.eventEmitter
this.executed = false this.executed = false
@ -73,7 +72,7 @@ class Transaction {
if (!tx.gasLimit) { if (!tx.gasLimit) {
const estimatedGasLimit = await this._estimateGas(tx) const estimatedGasLimit = await this._estimateGas(tx)
tx.gasLimit = min( 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, this.manager.config.BLOCK_GAS_LIMIT,
) )
} }
@ -301,7 +300,8 @@ class Transaction {
} }
_handleRpcError(e, method) { _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 // Sometimes ethers wraps known errors, unwrap it in this case
e = e.error e = e.error
} }

View File

@ -22,10 +22,10 @@ const defaultConfig = {
} }
class TxManager { class TxManager {
constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {}, gasPriceOracleConfig = {} }) { constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {}, gasPriceOracleConfig = {}, provider }) {
this.config = Object.assign({ ...defaultConfig }, config) this.config = Object.assign({ ...defaultConfig }, config)
this._privateKey = privateKey.startsWith('0x') ? privateKey : '0x' + privateKey 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._wallet = new ethers.Wallet(this._privateKey, this._provider)
this.address = this._wallet.address this.address = this._wallet.address
this._broadcastNodes = broadcastNodes this._broadcastNodes = broadcastNodes