@argonprotocol/mainchain 1.4.9-dev.8f82736f → 1.4.9-dev.c17ec6c5

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.
@@ -13808,6 +13808,12 @@ type IBigIntLike = bigint | {
13808
13808
  };
13809
13809
 
13810
13810
  type ITxProgressCallback = (progressToInBlock: number, result?: TxResult) => void;
13811
+ type IBlockInclusion = {
13812
+ blockHash: Uint8Array;
13813
+ blockNumber?: number;
13814
+ extrinsicIndex: number;
13815
+ events: GenericEvent[];
13816
+ };
13811
13817
  declare class TxResult {
13812
13818
  #private;
13813
13819
  protected readonly client: ArgonClient;
@@ -13857,14 +13863,10 @@ declare class TxResult {
13857
13863
  accountAddress: string;
13858
13864
  nonce: number;
13859
13865
  });
13860
- setSeenInBlock(block: {
13861
- blockHash: Uint8Array;
13862
- blockNumber?: number;
13863
- extrinsicIndex: number;
13864
- events: GenericEvent[];
13865
- }): Promise<void>;
13866
- setFinalized(): void;
13866
+ setSeenInBlock(block: IBlockInclusion): Promise<void>;
13867
+ setFinalized(): Promise<void>;
13867
13868
  onSubscriptionResult(result: ISubmittableResult): void;
13869
+ private publishSeenInBlock;
13868
13870
  private updateProgress;
13869
13871
  private parseEvents;
13870
13872
  }
package/browser/index.js CHANGED
@@ -6,7 +6,7 @@ export { decodeAddress, encodeAddress, isAddress, mnemonicGenerate } from '@polk
6
6
  import { TypeRegistry, Metadata } from '@polkadot/types';
7
7
  import BigNumber4 from 'bignumber.js';
8
8
  import bs58check from 'bs58check';
9
- import { hexToU8a, u8aToHex } from '@polkadot/util';
9
+ import { u8aEq, hexToU8a, u8aToHex } from '@polkadot/util';
10
10
  export { hexToU8a, u8aEq, u8aToHex } from '@polkadot/util';
11
11
  import { keccak256, stringToHex, encodeAbiParameters, toRlp, toHex, hexToBytes, bytesToHex, getAddress, decodeEventLog, createPublicClient, http } from 'viem';
12
12
  import { createBlockHeaderFromRPC } from '@ethereumjs/block';
@@ -215,13 +215,14 @@ function getBigIntFallback(value, source, fallbackFields, fallback = 0n) {
215
215
  }
216
216
 
217
217
  // src/TxResult.ts
218
- var _isBroadcast, _submissionError;
218
+ var _isBroadcast, _submissionError, _pendingInBlock;
219
219
  var TxResult = class {
220
220
  constructor(client, extrinsic) {
221
221
  this.client = client;
222
222
  this.extrinsic = extrinsic;
223
223
  __privateAdd(this, _isBroadcast, false);
224
224
  __privateAdd(this, _submissionError);
225
+ __privateAdd(this, _pendingInBlock);
225
226
  __publicField(this, "waitForFinalizedBlock");
226
227
  __publicField(this, "waitForInFirstBlock");
227
228
  __publicField(this, "events", []);
@@ -278,21 +279,44 @@ var TxResult = class {
278
279
  return __privateGet(this, _submissionError);
279
280
  }
280
281
  async setSeenInBlock(block) {
281
- const { blockHash, blockNumber, events } = block;
282
- if (blockHash !== this.blockHash) {
283
- this.parseEvents(events);
284
- this.blockHash = blockHash;
285
- this.blockNumber = blockNumber ?? await this.client.rpc.chain.getHeader(blockHash).then((h) => h.number.toNumber());
286
- this.extrinsicIndex = block.extrinsicIndex;
287
- this.updateProgress();
288
- if (this.extrinsicError) {
289
- this.inBlockReject(this.extrinsicError);
290
- } else {
291
- this.inBlockResolve(blockHash);
292
- }
282
+ if (block.blockNumber === void 0) {
283
+ __privateSet(this, _pendingInBlock, {
284
+ blockHash: block.blockHash,
285
+ extrinsicIndex: block.extrinsicIndex,
286
+ events: block.events
287
+ });
288
+ return;
289
+ }
290
+ if (this.blockHash && this.blockNumber === block.blockNumber && this.extrinsicIndex === block.extrinsicIndex && u8aEq(this.blockHash, block.blockHash)) {
291
+ __privateSet(this, _pendingInBlock, void 0);
292
+ return;
293
+ }
294
+ __privateSet(this, _pendingInBlock, void 0);
295
+ this.parseEvents(block.events);
296
+ this.blockHash = block.blockHash;
297
+ this.blockNumber = block.blockNumber;
298
+ this.extrinsicIndex = block.extrinsicIndex;
299
+ this.updateProgress();
300
+ if (this.extrinsicError) {
301
+ this.inBlockReject(this.extrinsicError);
302
+ } else {
303
+ this.inBlockResolve(block.blockHash);
293
304
  }
294
305
  }
295
- setFinalized() {
306
+ async setFinalized() {
307
+ const pendingInBlock = __privateGet(this, _pendingInBlock);
308
+ if (pendingInBlock) {
309
+ await this.publishSeenInBlock(pendingInBlock);
310
+ } else if (this.blockHash && this.blockNumber === void 0) {
311
+ if (this.extrinsicIndex === void 0) {
312
+ throw new Error("Cannot finalize transaction before extrinsic index is known");
313
+ }
314
+ await this.publishSeenInBlock({
315
+ blockHash: this.blockHash,
316
+ extrinsicIndex: this.extrinsicIndex,
317
+ events: this.events
318
+ });
319
+ }
296
320
  this.isFinalized = true;
297
321
  this.updateProgress();
298
322
  let error = this.extrinsicError ?? this.submissionError;
@@ -315,33 +339,73 @@ var TxResult = class {
315
339
  if (result2.internalError) this.submissionError = result2.internalError;
316
340
  }
317
341
  if (status.isInBlock) {
318
- void this.setSeenInBlock({
319
- blockHash: Uint8Array.from(status.asInBlock),
320
- events: extrinsicEvents,
321
- extrinsicIndex: txIndex
322
- });
342
+ try {
343
+ const pendingInBlock = createPendingInBlock(
344
+ Uint8Array.from(status.asInBlock),
345
+ txIndex,
346
+ extrinsicEvents
347
+ );
348
+ __privateSet(this, _pendingInBlock, pendingInBlock);
349
+ void this.publishSeenInBlock(pendingInBlock).catch((error) => {
350
+ if (!isMissingBlockHeaderError(error)) {
351
+ this.submissionError = error;
352
+ }
353
+ });
354
+ } catch (error) {
355
+ this.submissionError = error;
356
+ }
323
357
  }
324
358
  if (status.isUsurped) {
359
+ __privateSet(this, _pendingInBlock, void 0);
325
360
  this.submissionError = new TxSubmissionError(
326
361
  TxSubmissionErrorCode.Usurped,
327
362
  `Transaction was usurped by ${status.asUsurped.toHex()}.`
328
363
  );
329
364
  }
330
365
  if (status.isDropped) {
366
+ __privateSet(this, _pendingInBlock, void 0);
331
367
  this.submissionError = new TxSubmissionError(
332
368
  TxSubmissionErrorCode.Dropped,
333
369
  "Transaction was dropped before it was included in a block."
334
370
  );
335
371
  }
336
372
  if (status.isInvalid) {
373
+ __privateSet(this, _pendingInBlock, void 0);
337
374
  this.submissionError = new TxSubmissionError(
338
375
  TxSubmissionErrorCode.Invalid,
339
376
  "Transaction was rejected as invalid by the node."
340
377
  );
341
378
  }
342
379
  if (isFinalized) {
343
- this.setFinalized();
380
+ try {
381
+ __privateSet(this, _pendingInBlock, createPendingInBlock(
382
+ Uint8Array.from(status.asFinalized),
383
+ txIndex ?? __privateGet(this, _pendingInBlock)?.extrinsicIndex ?? this.extrinsicIndex,
384
+ extrinsicEvents.length ? extrinsicEvents : this.events
385
+ ));
386
+ } catch (error) {
387
+ this.submissionError = error;
388
+ return;
389
+ }
390
+ void this.setFinalized().catch((error) => {
391
+ this.submissionError = error;
392
+ });
393
+ }
394
+ }
395
+ async publishSeenInBlock(block) {
396
+ const pendingInBlock = __privateGet(this, _pendingInBlock);
397
+ if (!pendingInBlock || pendingInBlock.extrinsicIndex !== block.extrinsicIndex || !u8aEq(pendingInBlock.blockHash, block.blockHash)) {
398
+ return;
344
399
  }
400
+ const blockNumber = await this.client.rpc.chain.getHeader(block.blockHash).then((h) => h.number.toNumber());
401
+ const currentPendingInBlock = __privateGet(this, _pendingInBlock);
402
+ if (!currentPendingInBlock || currentPendingInBlock.extrinsicIndex !== block.extrinsicIndex || !u8aEq(currentPendingInBlock.blockHash, block.blockHash)) {
403
+ return;
404
+ }
405
+ await this.setSeenInBlock({
406
+ ...block,
407
+ blockNumber
408
+ });
345
409
  }
346
410
  updateProgress() {
347
411
  if (this.isFinalized || this.submissionError) {
@@ -390,6 +454,20 @@ var TxResult = class {
390
454
  };
391
455
  _isBroadcast = new WeakMap();
392
456
  _submissionError = new WeakMap();
457
+ _pendingInBlock = new WeakMap();
458
+ function createPendingInBlock(blockHash, extrinsicIndex, events) {
459
+ if (extrinsicIndex === void 0) {
460
+ throw new Error("Cannot publish transaction block state before extrinsic index is known");
461
+ }
462
+ return {
463
+ blockHash,
464
+ extrinsicIndex,
465
+ events
466
+ };
467
+ }
468
+ function isMissingBlockHeaderError(error) {
469
+ return String(error).includes("Unable to retrieve header and parent from supplied hash");
470
+ }
393
471
 
394
472
  // src/TxSubmitter.ts
395
473
  var TxSubmitter = class {