-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathNonceKeeperWallet.ts
More file actions
160 lines (139 loc) · 3.99 KB
/
NonceKeeperWallet.ts
File metadata and controls
160 lines (139 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { BigNumber } from "@ethersproject/bignumber";
import { defaultPath, HDNode } from "@ethersproject/hdnode";
import type { Deferrable } from "@ethersproject/properties";
import type {
Provider,
TransactionRequest,
TransactionResponse,
} from "@ethersproject/providers";
import { Wallet } from "@ethersproject/wallet";
import type { Wordlist } from "@ethersproject/wordlists";
import { Logger } from "@ethersproject/logger";
const ethersLogger = new Logger("NonceKeeperWallet");
import { log } from "./logger.js";
const nonces: Record<number, Promise<number>> = {};
export class NonceKeeperWallet extends Wallet {
connect(provider: Provider): NonceKeeperWallet {
return new NonceKeeperWallet(this, provider);
}
async getNextNonce(): Promise<number> {
const chainId = await this.getChainId();
nonces[chainId] ||= super.getTransactionCount();
const nonce = nonces[chainId];
nonces[chainId] = nonces[chainId].then((nonce) => nonce + 1);
return nonce;
}
async sendTransaction(
transaction: Deferrable<TransactionRequest>,
): Promise<TransactionResponse> {
try {
// this check is necessary in order to not generate new nonces when a tx is going to fail
await super.estimateGas(transaction);
} catch (error) {
checkError(error, { transaction });
}
if (transaction.nonce == null) {
transaction.nonce = await this.getNextNonce();
}
log.debug({ msg: "transaction", transaction });
return super.sendTransaction(transaction);
}
async estimateGas(
transaction: Deferrable<TransactionRequest>,
): Promise<BigNumber> {
return super
.estimateGas(transaction)
.catch((error) => checkError(error, { transaction }));
}
static override fromMnemonic(
mnemonic: string,
path?: string,
wordlist?: Wordlist,
) {
if (!path) {
path = defaultPath;
}
return new NonceKeeperWallet(
HDNode.fromMnemonic(mnemonic, undefined, wordlist).derivePath(path),
);
}
}
function checkError(error: any, params: any): any {
const transaction = params.transaction || params.signedTransaction;
let message = error.message;
if (
error.code === Logger.errors.SERVER_ERROR &&
error.error &&
typeof error.error.message === "string"
) {
message = error.error.message;
} else if (typeof error.body === "string") {
message = error.body;
} else if (typeof error.responseText === "string") {
message = error.responseText;
}
message = (message || "").toLowerCase();
// "insufficient funds for gas * price + value + cost(data)"
if (message.match(/insufficient funds|base fee exceeds gas limit/i)) {
ethersLogger.throwError(
"insufficient funds for intrinsic transaction cost",
Logger.errors.INSUFFICIENT_FUNDS,
{
error,
transaction,
},
);
}
// "nonce too low"
if (message.match(/nonce (is )?too low/i)) {
ethersLogger.throwError(
"nonce has already been used",
Logger.errors.NONCE_EXPIRED,
{
error,
transaction,
},
);
}
// "replacement transaction underpriced"
if (
message.match(
/replacement transaction underpriced|transaction gas price.*too low/i,
)
) {
ethersLogger.throwError(
"replacement fee too low",
Logger.errors.REPLACEMENT_UNDERPRICED,
{
error,
transaction,
},
);
}
// "replacement transaction underpriced"
if (message.match(/only replay-protected/i)) {
ethersLogger.throwError(
"legacy pre-eip-155 transactions not supported",
Logger.errors.UNSUPPORTED_OPERATION,
{
error,
transaction,
},
);
}
if (
message.match(
/gas required exceeds allowance|always failing transaction|execution reverted/,
)
) {
ethersLogger.throwError(
"cannot estimate gas; transaction may fail or may require manual gas limit",
Logger.errors.UNPREDICTABLE_GAS_LIMIT,
{
error,
transaction,
},
);
}
throw error;
}