@ravnexchange/react 0.1.5 → 0.1.7

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.
@@ -1,16 +1,26 @@
1
- import { type RavnClient, type StatusDTO } from "@ravnexchange/sdk";
1
+ import { type RavnClient, type SettlementStatus, type StatusDTO } from "@ravnexchange/sdk";
2
2
  export interface UseRavnStatusResult {
3
3
  status: StatusDTO | null;
4
4
  isLoading: boolean;
5
5
  error: unknown;
6
6
  }
7
7
  export declare function isPermanentError(err: unknown): boolean;
8
+ /** Whether to poll again after `status`: until it's terminal, then once more after a success when
9
+ * `txHash` is set, as RAVN's node may be a block behind yours on that fee tx and a success it
10
+ * answers before seeing it books no fee. */
11
+ export declare function pollAgain(status: SettlementStatus, txHash: string | undefined, rechecked: boolean): boolean;
12
+ /** How long to wait before the next poll after `status`: an expired swap's refund can take hours
13
+ * (Across: a later settlement bundle), so it's asked about once a minute, not every few seconds. */
14
+ export declare function nextPollMs(status: SettlementStatus, intervalMs: number): number;
8
15
  /**
9
16
  * Polls /v1/status until it reaches a terminal state (see isPollingTerminal: "unknown"
10
- * counts as terminal, it means the venue has no live tracker and never will for this swap).
17
+ * counts as terminal, it means the venue has no live tracker and never will for this swap;
18
+ * "expired" doesn't, a refund is due, and is polled once a minute until it lands; nor does
19
+ * "not_found", the venue has no record of this swap: brief right after sending, otherwise it
20
+ * stays so while mounted).
11
21
  * Pass `params: null` to not poll at all (e.g. before execution has produced a ref).
12
- * `txHash`: the fee payment a DEPOSIT's `fee.transaction` asked for (or a two-hop route's hop-1
13
- * tx). Without it the swap's status still arrives, but its fee is never proven and nothing is owed.
22
+ * `txHash`: executeAndTrack's `statusTxHash`, or the fee payment a DEPOSIT's `fee.transaction`
23
+ * asked for (or a two-hop route's hop-1 tx). Without it the swap's status still arrives, but its fee is never proven and nothing is owed.
14
24
  */
15
25
  export declare function useRavnStatus(client: RavnClient, params: {
16
26
  quoteToken: string;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isPermanentError = isPermanentError;
4
+ exports.pollAgain = pollAgain;
5
+ exports.nextPollMs = nextPollMs;
4
6
  exports.useRavnStatus = useRavnStatus;
5
7
  const react_1 = require("react");
6
8
  const sdk_1 = require("@ravnexchange/sdk");
@@ -12,12 +14,26 @@ const PERMANENT_ERROR_CODES = new Set(["UNAUTHORIZED", "INVALID_REQUEST", "QUOTE
12
14
  function isPermanentError(err) {
13
15
  return err instanceof sdk_1.RavnApiError && PERMANENT_ERROR_CODES.has(err.code);
14
16
  }
17
+ /** Whether to poll again after `status`: until it's terminal, then once more after a success when
18
+ * `txHash` is set, as RAVN's node may be a block behind yours on that fee tx and a success it
19
+ * answers before seeing it books no fee. */
20
+ function pollAgain(status, txHash, rechecked) {
21
+ return !(0, polling_1.isPollingTerminal)(status) || (status === "success" && !!txHash && !rechecked);
22
+ }
23
+ /** How long to wait before the next poll after `status`: an expired swap's refund can take hours
24
+ * (Across: a later settlement bundle), so it's asked about once a minute, not every few seconds. */
25
+ function nextPollMs(status, intervalMs) {
26
+ return status === "expired" ? Math.max(intervalMs, 60000) : intervalMs;
27
+ }
15
28
  /**
16
29
  * Polls /v1/status until it reaches a terminal state (see isPollingTerminal: "unknown"
17
- * counts as terminal, it means the venue has no live tracker and never will for this swap).
30
+ * counts as terminal, it means the venue has no live tracker and never will for this swap;
31
+ * "expired" doesn't, a refund is due, and is polled once a minute until it lands; nor does
32
+ * "not_found", the venue has no record of this swap: brief right after sending, otherwise it
33
+ * stays so while mounted).
18
34
  * Pass `params: null` to not poll at all (e.g. before execution has produced a ref).
19
- * `txHash`: the fee payment a DEPOSIT's `fee.transaction` asked for (or a two-hop route's hop-1
20
- * tx). Without it the swap's status still arrives, but its fee is never proven and nothing is owed.
35
+ * `txHash`: executeAndTrack's `statusTxHash`, or the fee payment a DEPOSIT's `fee.transaction`
36
+ * asked for (or a two-hop route's hop-1 tx). Without it the swap's status still arrives, but its fee is never proven and nothing is owed.
21
37
  */
22
38
  function useRavnStatus(client, params, intervalMs = 4000) {
23
39
  const [status, setStatus] = (0, react_1.useState)(null);
@@ -29,6 +45,7 @@ function useRavnStatus(client, params, intervalMs = 4000) {
29
45
  return;
30
46
  }
31
47
  let cancelled = false;
48
+ let rechecked = false;
32
49
  let timer;
33
50
  const poll = () => {
34
51
  setIsLoading(true);
@@ -39,8 +56,9 @@ function useRavnStatus(client, params, intervalMs = 4000) {
39
56
  return;
40
57
  setStatus(s);
41
58
  setError(null);
42
- if (!(0, polling_1.isPollingTerminal)(s.status)) {
43
- timer = setTimeout(poll, intervalMs);
59
+ if (pollAgain(s.status, params.txHash, rechecked)) {
60
+ rechecked || (rechecked = (0, polling_1.isPollingTerminal)(s.status));
61
+ timer = setTimeout(poll, nextPollMs(s.status, intervalMs));
44
62
  }
45
63
  })
46
64
  .catch((err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ravnexchange/react",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "React hooks for @ravnexchange/sdk: quote/execute/status state management. Bring your own wallet.",
5
5
  "keywords": ["ravn", "cross-chain", "swap", "swap-api", "dex-aggregator", "bitcoin", "native-btc", "solana", "evm", "crypto", "defi", "react", "react-hooks", "typescript"],
6
6
  "main": "./dist/index.js",
@@ -14,7 +14,7 @@
14
14
  "build": "tsc -p tsconfig.build.json"
15
15
  },
16
16
  "peerDependencies": {
17
- "@ravnexchange/sdk": ">=0.1.13",
17
+ "@ravnexchange/sdk": ">=0.2.0",
18
18
  "react": ">=18"
19
19
  },
20
20
  "devDependencies": {