@atomiqlabs/chain-starknet 8.4.0 → 8.4.1

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.
@@ -138,6 +138,10 @@ export declare class StarknetChainInterface implements ChainInterface<StarknetTx
138
138
  * @inheritDoc
139
139
  */
140
140
  deserializeSignedTx(txData: string): Promise<SignedStarknetTx>;
141
+ /**
142
+ * @inheritDoc
143
+ */
144
+ getTxId(signedTX: SignedStarknetTx): Promise<string>;
141
145
  /**
142
146
  * @inheritDoc
143
147
  */
@@ -156,6 +156,12 @@ class StarknetChainInterface {
156
156
  deserializeSignedTx(txData) {
157
157
  return Promise.resolve(StarknetTransactions_1.StarknetTransactions.deserializeTx(txData));
158
158
  }
159
+ /**
160
+ * @inheritDoc
161
+ */
162
+ getTxId(signedTX) {
163
+ return Promise.resolve(signedTX.txId ?? (0, Utils_1.calculateHash)(signedTX));
164
+ }
159
165
  /**
160
166
  * @inheritDoc
161
167
  */
@@ -299,15 +299,30 @@ class StarknetSwapContract extends StarknetContractBase_1.StarknetContractBase {
299
299
  const escrowHash = data.getEscrowHash();
300
300
  const stateData = await this.contract.get_hash_state("0x" + escrowHash);
301
301
  const state = Number(stateData.state);
302
+ const initBlockHeight = Number(stateData.init_blockheight);
302
303
  const blockHeight = Number(stateData.finish_blockheight);
304
+ const getInitTxId = async () => {
305
+ const events = await this._Events.getContractBlockEvents(["escrow_manager::events::Initialize"], [null, null, null, "0x" + escrowHash], initBlockHeight, initBlockHeight);
306
+ if (events.length === 0)
307
+ throw new Error("Initialize event not found!");
308
+ return events[0].txHash;
309
+ };
303
310
  switch (state) {
304
311
  case ESCROW_STATE_COMMITTED:
305
- if (data.isOfferer(signer) && await this.isExpired(signer, data))
306
- return { type: base_1.SwapCommitStateType.REFUNDABLE };
307
- return { type: base_1.SwapCommitStateType.COMMITED };
312
+ if (data.isOfferer(signer) && await this.isExpired(signer, data)) {
313
+ return {
314
+ type: base_1.SwapCommitStateType.REFUNDABLE,
315
+ getInitTxId
316
+ };
317
+ }
318
+ return {
319
+ type: base_1.SwapCommitStateType.COMMITED,
320
+ getInitTxId
321
+ };
308
322
  case ESCROW_STATE_CLAIMED:
309
323
  return {
310
324
  type: base_1.SwapCommitStateType.PAID,
325
+ getInitTxId,
311
326
  getTxBlock: async () => {
312
327
  return {
313
328
  blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
@@ -336,6 +351,7 @@ class StarknetSwapContract extends StarknetContractBase_1.StarknetContractBase {
336
351
  case ESCROW_STATE_REFUNDED:
337
352
  return {
338
353
  type: await this.isExpired(signer, data) ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
354
+ getInitTxId,
339
355
  getTxBlock: async () => {
340
356
  return {
341
357
  blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
@@ -435,6 +451,7 @@ class StarknetSwapContract extends StarknetContractBase_1.StarknetContractBase {
435
451
  },
436
452
  state: {
437
453
  type: base_1.SwapCommitStateType.PAID,
454
+ getInitTxId: foundSwapData?.getInitTxId,
438
455
  getClaimTxId: () => Promise.resolve(event.txHash),
439
456
  getClaimResult: () => Promise.resolve(claimHandler.parseWitnessResult(event.params.witness_result)),
440
457
  getTxBlock: async () => {
@@ -462,6 +479,7 @@ class StarknetSwapContract extends StarknetContractBase_1.StarknetContractBase {
462
479
  },
463
480
  state: {
464
481
  type: isExpired ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
482
+ getInitTxId: foundSwapData?.getInitTxId,
465
483
  getRefundTxId: () => Promise.resolve(event.txHash),
466
484
  getTxBlock: async () => {
467
485
  return {
@@ -489,8 +507,8 @@ class StarknetSwapContract extends StarknetContractBase_1.StarknetContractBase {
489
507
  getTxBlock: foundSwapData.getTxBlock
490
508
  },
491
509
  state: data.isOfferer(signer) && await this.isExpired(signer, data)
492
- ? { type: base_1.SwapCommitStateType.REFUNDABLE }
493
- : { type: base_1.SwapCommitStateType.COMMITED }
510
+ ? { type: base_1.SwapCommitStateType.REFUNDABLE, getInitTxId: foundSwapData.getInitTxId }
511
+ : { type: base_1.SwapCommitStateType.COMMITED, getInitTxId: foundSwapData.getInitTxId }
494
512
  };
495
513
  }
496
514
  await Promise.all(promises);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-starknet",
3
- "version": "8.4.0",
3
+ "version": "8.4.1",
4
4
  "description": "Starknet specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "url": "git+https://github.com/atomiqlabs/atomiq-chain-starknet.git"
32
32
  },
33
33
  "dependencies": {
34
- "@atomiqlabs/base": "^13.4.0",
34
+ "@atomiqlabs/base": "^13.4.2",
35
35
  "@noble/hashes": "^1.7.1",
36
36
  "@scure/btc-signer": "^1.6.0",
37
37
  "abi-wan-kanabi": "2.2.4",
@@ -1,5 +1,5 @@
1
1
  import {Provider, constants, stark, ec, Account, provider, wallet, WebSocketChannel, logger} from "starknet";
2
- import {getLogger, toHex} from "../../utils/Utils";
2
+ import {calculateHash, getLogger, toHex} from "../../utils/Utils";
3
3
  import {SignedStarknetTx, StarknetTransactions, StarknetTx} from "./modules/StarknetTransactions";
4
4
  import {StarknetFees} from "./modules/StarknetFees";
5
5
  import {StarknetAddresses} from "./modules/StarknetAddresses";
@@ -252,6 +252,13 @@ export class StarknetChainInterface implements ChainInterface<StarknetTx, Signed
252
252
  return Promise.resolve(StarknetTransactions.deserializeTx(txData));
253
253
  }
254
254
 
255
+ /**
256
+ * @inheritDoc
257
+ */
258
+ getTxId(signedTX: SignedStarknetTx): Promise<string> {
259
+ return Promise.resolve(signedTX.txId ?? calculateHash(signedTX));
260
+ }
261
+
255
262
  /**
256
263
  * @inheritDoc
257
264
  */
@@ -375,14 +375,35 @@ export class StarknetSwapContract
375
375
  const escrowHash = data.getEscrowHash();
376
376
  const stateData = await this.contract.get_hash_state("0x"+escrowHash);
377
377
  const state = Number(stateData.state);
378
+ const initBlockHeight = Number(stateData.init_blockheight);
378
379
  const blockHeight = Number(stateData.finish_blockheight);
380
+
381
+ const getInitTxId = async () => {
382
+ const events = await this._Events.getContractBlockEvents(
383
+ ["escrow_manager::events::Initialize"],
384
+ [null, null, null, "0x"+escrowHash],
385
+ initBlockHeight, initBlockHeight
386
+ );
387
+ if(events.length===0) throw new Error("Initialize event not found!");
388
+ return events[0].txHash;
389
+ }
390
+
379
391
  switch(state) {
380
392
  case ESCROW_STATE_COMMITTED:
381
- if(data.isOfferer(signer) && await this.isExpired(signer,data)) return {type: SwapCommitStateType.REFUNDABLE};
382
- return {type: SwapCommitStateType.COMMITED};
393
+ if(data.isOfferer(signer) && await this.isExpired(signer,data)) {
394
+ return {
395
+ type: SwapCommitStateType.REFUNDABLE,
396
+ getInitTxId
397
+ };
398
+ }
399
+ return {
400
+ type: SwapCommitStateType.COMMITED,
401
+ getInitTxId
402
+ };
383
403
  case ESCROW_STATE_CLAIMED:
384
404
  return {
385
405
  type: SwapCommitStateType.PAID,
406
+ getInitTxId,
386
407
  getTxBlock: async () => {
387
408
  return {
388
409
  blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
@@ -417,6 +438,7 @@ export class StarknetSwapContract
417
438
  case ESCROW_STATE_REFUNDED:
418
439
  return {
419
440
  type: await this.isExpired(signer, data) ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
441
+ getInitTxId,
420
442
  getTxBlock: async () => {
421
443
  return {
422
444
  blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
@@ -563,6 +585,7 @@ export class StarknetSwapContract
563
585
  },
564
586
  state: {
565
587
  type: SwapCommitStateType.PAID,
588
+ getInitTxId: foundSwapData?.getInitTxId,
566
589
  getClaimTxId: () => Promise.resolve(event.txHash),
567
590
  getClaimResult: () => Promise.resolve(claimHandler.parseWitnessResult(event.params.witness_result)),
568
591
  getTxBlock: async () => {
@@ -590,6 +613,7 @@ export class StarknetSwapContract
590
613
  },
591
614
  state: {
592
615
  type: isExpired ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
616
+ getInitTxId: foundSwapData?.getInitTxId,
593
617
  getRefundTxId: () => Promise.resolve(event.txHash),
594
618
  getTxBlock: async () => {
595
619
  return {
@@ -628,8 +652,8 @@ export class StarknetSwapContract
628
652
  getTxBlock: foundSwapData.getTxBlock
629
653
  },
630
654
  state: data.isOfferer(signer) && await this.isExpired(signer, data)
631
- ? {type: SwapCommitStateType.REFUNDABLE}
632
- : {type: SwapCommitStateType.COMMITED}
655
+ ? {type: SwapCommitStateType.REFUNDABLE, getInitTxId: foundSwapData.getInitTxId}
656
+ : {type: SwapCommitStateType.COMMITED, getInitTxId: foundSwapData.getInitTxId}
633
657
  }
634
658
  }
635
659