@atomiqlabs/chain-starknet 6.0.0-beta.0 → 6.0.0-beta.2

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.
@@ -61,7 +61,7 @@ export declare class StarknetBtcRelay<B extends BtcBlock> extends StarknetContra
61
61
  }, requiredBlockheight?: number): Promise<{
62
62
  header: StarknetBtcStoredHeader;
63
63
  height: number;
64
- } | null>;
64
+ }>;
65
65
  /**
66
66
  * Retrieves blockheader data by blockheader's commit hash,
67
67
  *
@@ -49,7 +49,18 @@ class StarknetChainEventsBrowser {
49
49
  */
50
50
  getSwapDataGetter(event, claimHandler) {
51
51
  return async () => {
52
- const trace = await this.provider.getTransactionTrace(event.txHash);
52
+ let trace;
53
+ try {
54
+ trace = await this.provider.getTransactionTrace(event.txHash);
55
+ }
56
+ catch (e) {
57
+ this.logger.warn("getSwapDataGetter(): getter: starknet_traceTransaction not supported by the RPC: ", e);
58
+ const blockTraces = await this.provider.getBlockTransactionsTraces(event.blockHash);
59
+ const foundTrace = blockTraces.find(val => (0, Utils_1.toHex)(val.transaction_hash) === (0, Utils_1.toHex)(event.txHash));
60
+ if (foundTrace == null)
61
+ throw new Error(`Cannot find ${event.txHash} in the block traces, block: ${event.blockHash}`);
62
+ trace = foundTrace.trace_root;
63
+ }
53
64
  if (trace == null)
54
65
  return null;
55
66
  if (trace.execute_invocation.revert_reason != null)
@@ -1,5 +1,18 @@
1
- import { RpcChannel, RpcProvider, RpcProviderOptions } from "starknet";
2
- export declare class RpcChannelWithRetries extends RpcChannel {
1
+ import { RPC08, RPC09, RpcProvider, RpcProviderOptions } from "starknet";
2
+ export declare class Rpc08ChannelWithRetries extends RPC08.RpcChannel {
3
+ readonly retryPolicy?: {
4
+ maxRetries?: number;
5
+ delay?: number;
6
+ exponential?: boolean;
7
+ };
8
+ constructor(options?: RpcProviderOptions, retryPolicy?: {
9
+ maxRetries?: number;
10
+ delay?: number;
11
+ exponential?: boolean;
12
+ });
13
+ protected fetchEndpoint(method: any, params?: any): Promise<any>;
14
+ }
15
+ export declare class Rpc09ChannelWithRetries extends RPC09.RpcChannel {
3
16
  readonly retryPolicy?: {
4
17
  maxRetries?: number;
5
18
  delay?: number;
@@ -13,6 +26,13 @@ export declare class RpcChannelWithRetries extends RpcChannel {
13
26
  protected fetchEndpoint(method: any, params?: any): Promise<any>;
14
27
  }
15
28
  export declare class RpcProviderWithRetries extends RpcProvider {
29
+ /**
30
+ * Tries to do naive detection of the spec version based on the suffix of nodeUrl, better pass the `specVersion`
31
+ * in the options!
32
+ *
33
+ * @param options
34
+ * @param retryPolicy
35
+ */
16
36
  constructor(options?: RpcProviderOptions, retryPolicy?: {
17
37
  maxRetries?: number;
18
38
  delay?: number;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RpcProviderWithRetries = exports.RpcChannelWithRetries = void 0;
3
+ exports.RpcProviderWithRetries = exports.Rpc09ChannelWithRetries = exports.Rpc08ChannelWithRetries = void 0;
4
4
  const starknet_1 = require("starknet");
5
5
  const Utils_1 = require("../../utils/Utils");
6
- class RpcChannelWithRetries extends starknet_1.RpcChannel {
6
+ class Rpc08ChannelWithRetries extends starknet_1.RPC08.RpcChannel {
7
7
  constructor(options, retryPolicy) {
8
8
  super(options);
9
9
  this.retryPolicy = retryPolicy;
@@ -22,11 +22,45 @@ class RpcChannelWithRetries extends starknet_1.RpcChannel {
22
22
  });
23
23
  }
24
24
  }
25
- exports.RpcChannelWithRetries = RpcChannelWithRetries;
25
+ exports.Rpc08ChannelWithRetries = Rpc08ChannelWithRetries;
26
+ class Rpc09ChannelWithRetries extends starknet_1.RPC09.RpcChannel {
27
+ constructor(options, retryPolicy) {
28
+ super(options);
29
+ this.retryPolicy = retryPolicy;
30
+ }
31
+ fetchEndpoint(method, params) {
32
+ return (0, Utils_1.tryWithRetries)(() => super.fetchEndpoint(method, params), this.retryPolicy, e => {
33
+ if (!e.message.startsWith("RPC: "))
34
+ return false;
35
+ const arr = e.message.split("\n");
36
+ const errorCode = parseInt(arr[arr.length - 1]);
37
+ if (isNaN(errorCode))
38
+ return false;
39
+ if (errorCode < 0)
40
+ return false; //Not defined error, e.g. Rate limit (-32097)
41
+ return true;
42
+ });
43
+ }
44
+ }
45
+ exports.Rpc09ChannelWithRetries = Rpc09ChannelWithRetries;
26
46
  class RpcProviderWithRetries extends starknet_1.RpcProvider {
47
+ /**
48
+ * Tries to do naive detection of the spec version based on the suffix of nodeUrl, better pass the `specVersion`
49
+ * in the options!
50
+ *
51
+ * @param options
52
+ * @param retryPolicy
53
+ */
27
54
  constructor(options, retryPolicy) {
55
+ if (options.specVersion == null)
56
+ options.specVersion = options.nodeUrl.endsWith("v0_8") ? "0.8.1" : "0.9.0";
28
57
  super(options);
29
- this.channel = new RpcChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
58
+ if (this.channel.id === "RPC081") {
59
+ this.channel = new Rpc08ChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
60
+ }
61
+ else if (this.channel.id === "RPC090") {
62
+ this.channel = new Rpc09ChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
63
+ }
30
64
  }
31
65
  }
32
66
  exports.RpcProviderWithRetries = RpcProviderWithRetries;
@@ -147,7 +147,7 @@ export declare class StarknetSwapContract extends StarknetContractBase<typeof Es
147
147
  txid: string;
148
148
  hex: string;
149
149
  height: number;
150
- }, requiredConfirmations: number, vout: number, commitedHeader?: StarknetBtcStoredHeader, synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>, initAta?: boolean, feeRate?: string): Promise<StarknetTx[] | null>;
150
+ }, requiredConfirmations: number, vout: number, commitedHeader?: StarknetBtcStoredHeader, synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>, initAta?: boolean, feeRate?: string): Promise<StarknetTx[]>;
151
151
  txsRefund(signer: string, swapData: StarknetSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<StarknetTx[]>;
152
152
  txsRefundWithAuthorization(signer: string, swapData: StarknetSwapData, { timeout, prefix, signature }: {
153
153
  timeout: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-starknet",
3
- "version": "6.0.0-beta.0",
3
+ "version": "6.0.0-beta.2",
4
4
  "description": "Starknet specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -222,7 +222,7 @@ export class StarknetBtcRelay<B extends BtcBlock>
222
222
  public async retrieveLogAndBlockheight(blockData: {blockhash: string}, requiredBlockheight?: number): Promise<{
223
223
  header: StarknetBtcStoredHeader,
224
224
  height: number
225
- } | null> {
225
+ }> {
226
226
  //TODO: we can fetch the blockheight and events in parallel
227
227
  const blockHeight = await this.getBlockHeight();
228
228
  if(requiredBlockheight!=null && blockHeight < requiredBlockheight) {
@@ -100,7 +100,16 @@ export class StarknetChainEventsBrowser implements ChainEvents<StarknetSwapData>
100
100
  claimHandler: IClaimHandler<any, any>
101
101
  ): () => Promise<StarknetSwapData> {
102
102
  return async () => {
103
- const trace: any = await this.provider.getTransactionTrace(event.txHash);
103
+ let trace: any;
104
+ try {
105
+ trace = await this.provider.getTransactionTrace(event.txHash);
106
+ } catch (e) {
107
+ this.logger.warn("getSwapDataGetter(): getter: starknet_traceTransaction not supported by the RPC: ", e);
108
+ const blockTraces: any[] = await this.provider.getBlockTransactionsTraces(event.blockHash);
109
+ const foundTrace = blockTraces.find(val => toHex(val.transaction_hash)===toHex(event.txHash));
110
+ if(foundTrace==null) throw new Error(`Cannot find ${event.txHash} in the block traces, block: ${event.blockHash}`);
111
+ trace = foundTrace.trace_root;
112
+ }
104
113
  if(trace==null) return null;
105
114
  if(trace.execute_invocation.revert_reason!=null) return null;
106
115
  return this.findInitSwapData(trace.execute_invocation as any, event.params.escrow_hash, claimHandler);
@@ -1,11 +1,37 @@
1
1
  import {
2
- RpcChannel,
2
+ RPC08, RPC09,
3
3
  RpcProvider,
4
4
  RpcProviderOptions
5
5
  } from "starknet";
6
6
  import {tryWithRetries} from "../../utils/Utils";
7
7
 
8
- export class RpcChannelWithRetries extends RpcChannel {
8
+ export class Rpc08ChannelWithRetries extends RPC08.RpcChannel {
9
+
10
+ readonly retryPolicy?: {
11
+ maxRetries?: number, delay?: number, exponential?: boolean
12
+ };
13
+
14
+ constructor(options?: RpcProviderOptions, retryPolicy?: {
15
+ maxRetries?: number, delay?: number, exponential?: boolean
16
+ }) {
17
+ super(options);
18
+ this.retryPolicy = retryPolicy;
19
+ }
20
+
21
+ protected fetchEndpoint(method: any, params?: any): Promise<any> {
22
+ return tryWithRetries(() => super.fetchEndpoint(method, params), this.retryPolicy, e => {
23
+ if(!e.message.startsWith("RPC: ")) return false;
24
+ const arr = e.message.split("\n");
25
+ const errorCode = parseInt(arr[arr.length-1]);
26
+ if(isNaN(errorCode)) return false;
27
+ if(errorCode < 0) return false; //Not defined error, e.g. Rate limit (-32097)
28
+ return true;
29
+ });
30
+ }
31
+
32
+ }
33
+
34
+ export class Rpc09ChannelWithRetries extends RPC09.RpcChannel {
9
35
 
10
36
  readonly retryPolicy?: {
11
37
  maxRetries?: number, delay?: number, exponential?: boolean
@@ -33,11 +59,23 @@ export class RpcChannelWithRetries extends RpcChannel {
33
59
 
34
60
  export class RpcProviderWithRetries extends RpcProvider {
35
61
 
62
+ /**
63
+ * Tries to do naive detection of the spec version based on the suffix of nodeUrl, better pass the `specVersion`
64
+ * in the options!
65
+ *
66
+ * @param options
67
+ * @param retryPolicy
68
+ */
36
69
  constructor(options?: RpcProviderOptions, retryPolicy?: {
37
70
  maxRetries?: number, delay?: number, exponential?: boolean
38
71
  }) {
72
+ if(options.specVersion==null) options.specVersion = options.nodeUrl.endsWith("v0_8") ? "0.8.1" : "0.9.0";
39
73
  super(options);
40
- this.channel = new RpcChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
74
+ if(this.channel.id==="RPC081") {
75
+ this.channel = new Rpc08ChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
76
+ } else if(this.channel.id==="RPC090") {
77
+ this.channel = new Rpc09ChannelWithRetries({ ...options, waitMode: false }, retryPolicy);
78
+ }
41
79
  }
42
80
 
43
81
  }
@@ -464,7 +464,7 @@ export class StarknetSwapContract
464
464
  synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>,
465
465
  initAta?: boolean,
466
466
  feeRate?: string
467
- ): Promise<StarknetTx[] | null> {
467
+ ): Promise<StarknetTx[]> {
468
468
  return this.Claim.txsClaimWithTxData(
469
469
  typeof(signer)==="string" ? signer : signer.getAddress(),
470
470
  swapData,