@atomiqlabs/chain-evm 1.0.0-dev.104 → 1.0.0-dev.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,8 @@ class EVMEvents extends EVMModule_1.EVMModule {
22
22
  });
23
23
  }
24
24
  catch (e) {
25
- if (e.error?.code === -32008 || //Response is too big
25
+ if ((e.error?.code === -32602 && e.error?.message?.startsWith("query exceeds max results")) || //Query exceeds max results
26
+ e.error?.code === -32008 || //Response is too big
26
27
  e.error?.code === -32005 //Limit exceeded
27
28
  ) {
28
29
  if (startBlock === endBlock)
@@ -22,6 +22,7 @@ export declare class EVMPersistentSigner extends EVMSigner {
22
22
  private save;
23
23
  private checkPastTransactions;
24
24
  private startFeeBumper;
25
+ private syncNonceFromChain;
25
26
  init(): Promise<void>;
26
27
  stop(): Promise<void>;
27
28
  private readonly sendTransactionQueue;
@@ -75,11 +75,11 @@ class EVMPersistentSigner extends EVMSigner_1.EVMSigner {
75
75
  for (let [nonce, data] of this.pendingTxs) {
76
76
  if (!data.sending && data.lastBumped < Date.now() - this.waitBeforeBump) {
77
77
  _safeBlockTxCount = await this.chainInterface.provider.getTransactionCount(this.address, this.safeBlockTag);
78
- this.confirmedNonce = _safeBlockTxCount;
79
- if (_safeBlockTxCount > nonce) {
78
+ this.confirmedNonce = _safeBlockTxCount - 1;
79
+ if (this.confirmedNonce >= nonce) {
80
80
  this.pendingTxs.delete(nonce);
81
81
  data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.hash));
82
- this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
82
+ this.logger.info(`checkPastTransactions(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
83
83
  this.save();
84
84
  continue;
85
85
  }
@@ -159,6 +159,21 @@ class EVMPersistentSigner extends EVMSigner_1.EVMSigner {
159
159
  };
160
160
  func();
161
161
  }
162
+ async syncNonceFromChain() {
163
+ const txCount = await this.chainInterface.provider.getTransactionCount(this.address, this.safeBlockTag);
164
+ this.confirmedNonce = txCount - 1;
165
+ if (this.pendingNonce < this.confirmedNonce) {
166
+ this.pendingNonce = this.confirmedNonce;
167
+ for (let [nonce, data] of this.pendingTxs) {
168
+ if (nonce <= this.pendingNonce) {
169
+ this.pendingTxs.delete(nonce);
170
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.hash));
171
+ }
172
+ this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
173
+ }
174
+ this.save();
175
+ }
176
+ }
162
177
  async init() {
163
178
  try {
164
179
  await fs.mkdir(this.directory);
@@ -213,11 +228,17 @@ class EVMPersistentSigner extends EVMSigner_1.EVMSigner {
213
228
  this.save();
214
229
  this.chainInterface.Transactions._knownTxSet.add(signedTx.hash);
215
230
  try {
231
+ //TODO: This can fail due to not receiving a response from the server, however the transaction
232
+ // might already be broadcasted!
216
233
  const result = await this.chainInterface.provider.broadcastTransaction(signedRawTx);
217
234
  pendingTxObject.sending = false;
218
235
  return result;
219
236
  }
220
237
  catch (e) {
238
+ if (e.code === "NONCE_EXPIRED") {
239
+ //Re-check nonce from on-chain
240
+ await this.syncNonceFromChain();
241
+ }
221
242
  this.chainInterface.Transactions._knownTxSet.delete(signedTx.hash);
222
243
  this.pendingTxs.delete(transaction.nonce);
223
244
  this.pendingNonce--;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-evm",
3
- "version": "1.0.0-dev.104",
3
+ "version": "1.0.0-dev.105",
4
4
  "description": "EVM specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -24,6 +24,7 @@ export class EVMEvents extends EVMModule<any> {
24
24
  });
25
25
  } catch (e) {
26
26
  if(
27
+ (e.error?.code===-32602 && e.error?.message?.startsWith("query exceeds max results")) || //Query exceeds max results
27
28
  e.error?.code===-32008 || //Response is too big
28
29
  e.error?.code===-32005 //Limit exceeded
29
30
  ) {
@@ -131,11 +131,11 @@ export class EVMPersistentSigner extends EVMSigner {
131
131
  for(let [nonce, data] of this.pendingTxs) {
132
132
  if(!data.sending && data.lastBumped<Date.now()-this.waitBeforeBump) {
133
133
  _safeBlockTxCount = await this.chainInterface.provider.getTransactionCount(this.address, this.safeBlockTag);
134
- this.confirmedNonce = _safeBlockTxCount;
135
- if(_safeBlockTxCount > nonce) {
134
+ this.confirmedNonce = _safeBlockTxCount - 1;
135
+ if(this.confirmedNonce >= nonce) {
136
136
  this.pendingTxs.delete(nonce);
137
137
  data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.hash));
138
- this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
138
+ this.logger.info(`checkPastTransactions(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
139
139
  this.save();
140
140
  continue;
141
141
  }
@@ -228,6 +228,22 @@ export class EVMPersistentSigner extends EVMSigner {
228
228
  func();
229
229
  }
230
230
 
231
+ private async syncNonceFromChain() {
232
+ const txCount = await this.chainInterface.provider.getTransactionCount(this.address, this.safeBlockTag);
233
+ this.confirmedNonce = txCount-1;
234
+ if(this.pendingNonce < this.confirmedNonce) {
235
+ this.pendingNonce = this.confirmedNonce;
236
+ for(let [nonce, data] of this.pendingTxs) {
237
+ if(nonce <= this.pendingNonce) {
238
+ this.pendingTxs.delete(nonce);
239
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.hash));
240
+ }
241
+ this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
242
+ }
243
+ this.save();
244
+ }
245
+ }
246
+
231
247
  async init(): Promise<void> {
232
248
  try {
233
249
  await fs.mkdir(this.directory)
@@ -291,10 +307,16 @@ export class EVMPersistentSigner extends EVMSigner {
291
307
  this.chainInterface.Transactions._knownTxSet.add(signedTx.hash);
292
308
 
293
309
  try {
310
+ //TODO: This can fail due to not receiving a response from the server, however the transaction
311
+ // might already be broadcasted!
294
312
  const result = await this.chainInterface.provider.broadcastTransaction(signedRawTx);
295
313
  pendingTxObject.sending = false;
296
314
  return result;
297
315
  } catch (e) {
316
+ if(e.code==="NONCE_EXPIRED") {
317
+ //Re-check nonce from on-chain
318
+ await this.syncNonceFromChain();
319
+ }
298
320
  this.chainInterface.Transactions._knownTxSet.delete(signedTx.hash);
299
321
  this.pendingTxs.delete(transaction.nonce);
300
322
  this.pendingNonce--;