@atomiqlabs/chain-starknet 4.0.0-dev.39 → 4.0.0-dev.40

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.
@@ -26,6 +26,7 @@ export declare class StarknetPersistentSigner extends StarknetSigner {
26
26
  private save;
27
27
  private checkPastTransactions;
28
28
  private startFeeBumper;
29
+ private syncNonceFromChain;
29
30
  init(): Promise<void>;
30
31
  stop(): Promise<void>;
31
32
  private readonly sendTransactionQueue;
@@ -81,9 +81,11 @@ class StarknetPersistentSigner extends StarknetSigner_1.StarknetSigner {
81
81
  let _safeBlockNonce = null;
82
82
  for (let [nonce, data] of this.pendingTxs) {
83
83
  if (!data.sending && data.lastBumped < Date.now() - this.config.waitBeforeBump) {
84
- _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
85
- this.confirmedNonce = _safeBlockNonce;
86
- if (_safeBlockNonce > nonce) {
84
+ if (_safeBlockNonce == null) {
85
+ _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
86
+ this.confirmedNonce = _safeBlockNonce - 1n;
87
+ }
88
+ if (this.confirmedNonce >= nonce) {
87
89
  this.pendingTxs.delete(nonce);
88
90
  data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
89
91
  this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
@@ -183,6 +185,22 @@ class StarknetPersistentSigner extends StarknetSigner_1.StarknetSigner {
183
185
  };
184
186
  func();
185
187
  }
188
+ async syncNonceFromChain() {
189
+ const txCount = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
190
+ this.confirmedNonce = txCount - 1n;
191
+ if (this.pendingNonce < this.confirmedNonce) {
192
+ this.logger.info(`syncNonceFromChain(): Re-synced latest nonce from chain, adjusting local pending nonce ${this.pendingNonce} -> ${this.confirmedNonce}`);
193
+ this.pendingNonce = this.confirmedNonce;
194
+ for (let [nonce, data] of this.pendingTxs) {
195
+ if (nonce <= this.pendingNonce) {
196
+ this.pendingTxs.delete(nonce);
197
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
198
+ this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
199
+ }
200
+ }
201
+ this.save();
202
+ }
203
+ }
186
204
  async init() {
187
205
  try {
188
206
  await (0, promises_1.mkdir)(this.directory);
@@ -232,6 +250,11 @@ class StarknetPersistentSigner extends StarknetSigner_1.StarknetSigner {
232
250
  return result;
233
251
  }
234
252
  catch (e) {
253
+ if (e.baseError?.code === 52) { //Invalid transaction nonce
254
+ //Re-check nonce from on-chain
255
+ this.logger.info("sendTransaction(): Got INVALID_TRANSACTION_NONCE (52) back from backend, re-checking latest nonce from chain!");
256
+ await this.syncNonceFromChain();
257
+ }
235
258
  this.chainInterface.Transactions._knownTxSet.delete(signedTx.txId);
236
259
  this.pendingTxs.delete(transaction.details.nonce);
237
260
  this.pendingNonce--;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-starknet",
3
- "version": "4.0.0-dev.39",
3
+ "version": "4.0.0-dev.40",
4
4
  "description": "Starknet specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -129,9 +129,11 @@ export class StarknetPersistentSigner extends StarknetSigner {
129
129
 
130
130
  for(let [nonce, data] of this.pendingTxs) {
131
131
  if(!data.sending && data.lastBumped<Date.now()-this.config.waitBeforeBump) {
132
- _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
133
- this.confirmedNonce = _safeBlockNonce;
134
- if(_safeBlockNonce > nonce) {
132
+ if(_safeBlockNonce==null) {
133
+ _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
134
+ this.confirmedNonce = _safeBlockNonce - 1n;
135
+ }
136
+ if(this.confirmedNonce >= nonce) {
135
137
  this.pendingTxs.delete(nonce);
136
138
  data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
137
139
  this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
@@ -242,6 +244,23 @@ export class StarknetPersistentSigner extends StarknetSigner {
242
244
  func();
243
245
  }
244
246
 
247
+ private async syncNonceFromChain() {
248
+ const txCount = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
249
+ this.confirmedNonce = txCount-1n;
250
+ if(this.pendingNonce < this.confirmedNonce) {
251
+ this.logger.info(`syncNonceFromChain(): Re-synced latest nonce from chain, adjusting local pending nonce ${this.pendingNonce} -> ${this.confirmedNonce}`);
252
+ this.pendingNonce = this.confirmedNonce;
253
+ for(let [nonce, data] of this.pendingTxs) {
254
+ if(nonce <= this.pendingNonce) {
255
+ this.pendingTxs.delete(nonce);
256
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
257
+ this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
258
+ }
259
+ }
260
+ this.save();
261
+ }
262
+ }
263
+
245
264
  async init(): Promise<void> {
246
265
  try {
247
266
  await mkdir(this.directory)
@@ -299,6 +318,11 @@ export class StarknetPersistentSigner extends StarknetSigner {
299
318
  pendingTxObject.sending = false;
300
319
  return result;
301
320
  } catch (e) {
321
+ if(e.baseError?.code === 52) { //Invalid transaction nonce
322
+ //Re-check nonce from on-chain
323
+ this.logger.info("sendTransaction(): Got INVALID_TRANSACTION_NONCE (52) back from backend, re-checking latest nonce from chain!");
324
+ await this.syncNonceFromChain();
325
+ }
302
326
  this.chainInterface.Transactions._knownTxSet.delete(signedTx.txId);
303
327
  this.pendingTxs.delete(transaction.details.nonce);
304
328
  this.pendingNonce--;