Skip to content

Commit 442f98b

Browse files
authored
Merge pull request #21 from NodeFactoryIo/mpetrun5/pass-logger-instance-to-tx-monitor
Pass logger instance to tx monitor
2 parents a0ec3d5 + 57efc9b commit 442f98b

File tree

5 files changed

+393
-338
lines changed

5 files changed

+393
-338
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@
5252
"bsert": "0.0.10",
5353
"chai": "^4.2.0",
5454
"eslint": "^6.6.0",
55+
"ethers": "^5.0.X",
5556
"mocha": "^6.2.3",
5657
"sinon": "^9.0.2",
5758
"ts-loader": "^6.2.1",
5859
"typescript": "^3.6.4"
5960
},
6061
"dependencies": {
6162
"axios": ">=0.19.2",
62-
"debug": "^4.1.1",
63-
"ethers": "^5.0.X",
63+
"debug": "^4.2.0",
6464
"it-pushable": "^1.4.0"
65+
},
66+
"peerDependencies": {
67+
"ethers": ">=5"
6568
}
6669
}

src/logger.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
import debug from "debug";
22

3-
export const logger = debug("web3-server-wallet");
3+
export interface ILogger<TReturn = void> {
4+
info(msg: string, ...params: unknown[]): TReturn;
5+
debug(msg: string, ...params: unknown[]): TReturn;
6+
error(msg: string, ...params: unknown[]): TReturn;
7+
}
8+
9+
export class DefaultLogger implements ILogger {
10+
private infoLogger = debug("web3-server-wallet:INFO")
11+
private debugLogger = debug("web3-server-wallet:DEBUG")
12+
private errorLogger = debug("web3-server-wallet:ERROR")
13+
14+
public info(message: string): void {
15+
this.infoLogger(message);
16+
}
17+
18+
public debug(message: string): void {
19+
this.debugLogger(message);
20+
}
21+
22+
public error(message: string): void {
23+
this.errorLogger(message);
24+
}
25+
}
26+
27+
export const defaultLogger = new DefaultLogger();

src/monitorService.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
recalculateGasPrice,
77
transactionNotInBlock
88
} from "./utils";
9-
import {logger} from "./logger";
9+
import {defaultLogger, ILogger} from "./logger";
1010

1111
interface ITxMonitorOptions {
1212
neededConfirmations: number;
@@ -17,6 +17,7 @@ interface ITxMonitorOptions {
1717

1818
export class TxMonitorService {
1919
private wallet: ServerWeb3Wallet;
20+
private logger: ILogger;
2021
private intervalId?: NodeJS.Timeout;
2122
private options: ITxMonitorOptions;
2223
private defaultOptions = {
@@ -25,8 +26,9 @@ export class TxMonitorService {
2526
transactionTimeout: 180000
2627
};
2728

28-
constructor(wallet: ServerWeb3Wallet, options?: Partial<ITxMonitorOptions>) {
29+
constructor(wallet: ServerWeb3Wallet, logger=defaultLogger, options?: Partial<ITxMonitorOptions>) {
2930
this.wallet = wallet;
31+
this.logger = logger;
3032
this.options = Object.assign({}, this.defaultOptions, options);
3133
};
3234

@@ -74,6 +76,11 @@ export class TxMonitorService {
7476
this.options.gasPriceIncrease
7577
);
7678
try {
79+
this.logger.debug(
80+
`Resending transaction ${transaction.hash}.
81+
Old gas: ${transaction.gasPrice}.
82+
New gas price: ${transaction.gasPrice}`
83+
);
7784
await this.wallet.sendTransaction({
7885
to: transaction.to,
7986
nonce: transaction.nonce,
@@ -84,8 +91,10 @@ export class TxMonitorService {
8491
gasPrice: newGasPrice
8592
});
8693
} catch(error) {
87-
logger(`Resending transaction with hash ${transaction.hash} failed, ${error.message}`);
94+
this.logger.error(`Resending transaction with hash ${transaction.hash} failed, ${error.message}`);
8895
}
96+
97+
this.logger.debug(`Deleting transaction ${transaction.hash} from storage`);
8998
await this.wallet.walletStorage.deleteTransaction(transaction.hash);
9099
}
91100

src/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import axios from "axios";
22
import {BigNumber, utils, providers} from "ethers";
33
import {SavedTransactionResponse} from "./@types/wallet";
4-
import {logger} from "./logger";
54

65
const GAS_PRICE_API = "https://ethgasstation.info/api/ethgasAPI.json"
76

@@ -27,7 +26,6 @@ export async function estimateGasPrice(
2726
return utils.parseUnits((gasPrice / 10).toString(), "gwei");
2827
}
2928
} catch(error) {
30-
logger("Gas station api not available.");
3129
return;
3230
}
3331
}

0 commit comments

Comments
 (0)