@exodus/ethereum-api 8.73.1 → 8.73.3

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,88 +0,0 @@
1
- import WebSocket from '@exodus/fetch/websocket'
2
- import EventEmitter from 'events/events.js' // forces it to use the module from node_modules
3
- import ms from 'ms'
4
-
5
- const RECONNECT_INTERVAL = ms('10s')
6
- const PING_INTERVAL = ms('20s')
7
-
8
- export default function createWebSocket(url) {
9
- const addresses = new Set()
10
- const events = new EventEmitter().setMaxListeners(20)
11
- const pingMessage = JSON.stringify({ event: 'ping' })
12
- let ws
13
- let wsOpened = false
14
- let opened = false
15
- let openTimeoutId
16
- let pingIntervalId
17
-
18
- function subscribeAddress(address) {
19
- const data = JSON.stringify({ event: 'txlist', address })
20
- ws.send(data)
21
- }
22
-
23
- function onMessage(data) {
24
- data = JSON.parse(data)
25
- switch (data.event) {
26
- case 'txlist':
27
- for (const tx of data.result) events.emit(`address-${data.address}`, tx)
28
- break
29
-
30
- case 'subscribe-txlist':
31
- const match = data.message.toLowerCase().match(/0x[\da-f]{40}/)
32
- if (match && data.status === '1') events.emit(`address-${match[0]}-subscribed`)
33
- else ws.close()
34
- break
35
- }
36
- }
37
-
38
- function isOpened() {
39
- return wsOpened
40
- }
41
-
42
- function open() {
43
- opened = true
44
- clearTimeout(openTimeoutId)
45
- if (ws) return
46
-
47
- ws = new WebSocket(url)
48
-
49
- ws.on('message', (data) => {
50
- try {
51
- onMessage(data)
52
- } catch {}
53
- })
54
- ws.once('open', () => {
55
- for (const address of addresses.values()) subscribeAddress(address)
56
- pingIntervalId = setInterval(() => ws && ws.send(pingMessage), PING_INTERVAL)
57
- wsOpened = true
58
- events.emit('open')
59
- })
60
- ws.once('close', () => {
61
- ws = null
62
- clearInterval(pingIntervalId)
63
- if (opened) openTimeoutId = setTimeout(open, RECONNECT_INTERVAL)
64
- wsOpened = false
65
- events.emit('close')
66
- })
67
- }
68
-
69
- function close() {
70
- opened = false
71
- clearTimeout(openTimeoutId)
72
- if (!ws) return
73
-
74
- ws.close()
75
- ws = null
76
- }
77
-
78
- function watch(address) {
79
- address = address.toLowerCase()
80
-
81
- if (addresses.has(address)) return
82
- addresses.add(address)
83
-
84
- if (wsOpened) subscribeAddress(address)
85
- }
86
-
87
- return { events, isOpened, open, close, watch }
88
- }