@stellar-expert/ui-framework 1.14.15 → 1.14.17

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,7 +1,7 @@
1
1
  import {useRef} from 'react'
2
2
  import isEqual from 'react-fast-compare'
3
3
  import {parseQuery, stringifyQuery, navigation} from '@stellar-expert/navigation'
4
- import {getCurrentStellarNetwork, useStellarNetwork} from '../state/stellar-network-hooks'
4
+ import {useStellarNetwork} from '../state/stellar-network-hooks'
5
5
  import {useDependantState} from '../state/state-hooks'
6
6
  import {fetchExplorerApi} from './explorer-api-call'
7
7
  import apiCache from './api-cache'
@@ -0,0 +1,101 @@
1
+ import {getCurrentStellarNetwork} from '../state/stellar-network-hooks'
2
+ import {fetchExplorerApi} from './explorer-api-call'
3
+
4
+ class LedgerStream {
5
+ constructor() {
6
+ this.listeners = new Set()
7
+ }
8
+
9
+ /**
10
+ * @type {boolean}
11
+ * @private
12
+ */
13
+ listening = false
14
+ /**
15
+ * @type {Set<function>}
16
+ * @private
17
+ */
18
+ listeners
19
+
20
+ /**
21
+ * @param {function} listener
22
+ */
23
+ on(listener) {
24
+ this.listeners.add(listener)
25
+ if (!this.listening) {
26
+ this.listening = true
27
+ setTimeout(() => this.waitForLedger(), 100)
28
+ }
29
+ }
30
+
31
+ /**
32
+ * @param {function} listener
33
+ */
34
+ off(listener) {
35
+ this.listeners.delete(listener)
36
+ if (!this.listeners.size) {
37
+ this.listening = false
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @param {number} ledger
43
+ * @private
44
+ */
45
+ notify(ledger) {
46
+ for (const listener of this.listeners) {
47
+ listener(ledger)
48
+ }
49
+ }
50
+
51
+ /**
52
+ * @return {Promise}
53
+ * @private
54
+ */
55
+ async waitForLedger() {
56
+ const url = `${explorerApiOrigin}/explorer/${getCurrentStellarNetwork()}/ledger/stream`
57
+ try {
58
+ const resp = await fetch(url, {keepalive: true, cache: 'no-cache'})
59
+ if (!resp.ok) {
60
+ let errorExt
61
+ try {
62
+ errorExt = await resp.json()
63
+ } catch (parsingError) {
64
+ errorExt = {}
65
+ }
66
+ const err = new Error(errorExt?.error || resp.statusText || 'Failed to fetch data from the server')
67
+ err.status = resp.status
68
+ err.ext = errorExt
69
+ throw err
70
+ }
71
+ const {ledger} = await resp.json()
72
+ this.notify(ledger)
73
+ if (this.listening) {
74
+ setTimeout(() => this.waitForLedger(), 1000)
75
+ }
76
+ } catch (e) {
77
+ console.error(e)
78
+ if (e instanceof Error) {
79
+ e = {
80
+ error: e.message,
81
+ status: e.status || 500,
82
+ ext: e.ext
83
+ }
84
+ }
85
+ if (e.ext && e.ext.status) {
86
+ e.status = e.ext.status
87
+ }
88
+ return e
89
+ }
90
+ }
91
+
92
+ async getLast() {
93
+ return fetchExplorerApi(getCurrentStellarNetwork() + '/ledger/last')
94
+ }
95
+
96
+ async getLastSequence() {
97
+ return (await this.getLast()).sequence
98
+ }
99
+ }
100
+
101
+ export const ledgerStream = new LedgerStream()
package/index.js CHANGED
@@ -19,6 +19,7 @@ export * from './api/explorer-api-hooks'
19
19
  export * from './api/explorer-api-paginated-list-hooks'
20
20
  export * from './api/explorer-tx-api'
21
21
  export * from './api/explorer-batch-info-loader'
22
+ export * from './api/ledger-stream'
22
23
  //Horizon API binding and utils
23
24
  export * from './stellar/ledger-generic-id'
24
25
  export * from './horizon/horizon-client-helpers'
@@ -59,6 +60,7 @@ export * from './date/date-selector'
59
60
  //ledger-entries-related components
60
61
  export * from './ledger/ledger-entry-link'
61
62
  export * from './ledger/ledger-entry-href-formatter'
63
+ export * from './ledger/ledger-info-parser'
62
64
  //account-related components
63
65
  export * from './account/identicon'
64
66
  export * from './account/account-address'
@@ -0,0 +1,17 @@
1
+ import {xdr} from '@stellar/stellar-base'
2
+
3
+ export function retrieveLedgerInfo(data) {
4
+ const parsed = xdr.LedgerHeader.fromXDR(data.xdr, 'base64')
5
+ return {
6
+ sequence: data.sequence,
7
+ ts: data.ts,
8
+ protocol: data.protocol,
9
+ operations: data.operations || 0,
10
+ txSuccess: data.txSuccess,
11
+ txFailed: data.txFailed,
12
+ xlm: parsed.totalCoins().toBigInt(),
13
+ feePool: parsed.feePool().toBigInt(),
14
+ baseFee: parsed.baseFee(),
15
+ baseReserve: parsed.baseReserve()
16
+ }
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-expert/ui-framework",
3
- "version": "1.14.15",
3
+ "version": "1.14.17",
4
4
  "description": "StellarExpert shared UI components library",
5
5
  "main": "index.js",
6
6
  "module": "./index.js",