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"
},
"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",
{

4
index.d.ts vendored
View File

@ -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<U extends TxManagerEvents>(event: U, listener: MessageEvents[U]): this
on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise<TransactionReceipt>
emit<U extends TxManagerEvents>(event: U, ...args: Parameters<MessageEvents[U]>): boolean
}

View File

@ -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",

View File

@ -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
}

View File

@ -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