-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcli.ts
52 lines (43 loc) · 1.16 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import WebSocket from 'ws'
import {WebSocketEvents} from './events'
import {
getCurrencyFromAddress,
loadWalletBalanceLoop,
printBalance,
sendSocketMessage,
setupKeyListener,
} from './utils'
const ws = new WebSocket('ws://localhost:3000')
const address = process.argv[2]
const currency = getCurrencyFromAddress(address)
let balance: number | undefined
let price: number | undefined
async function shutdown() {
Array.apply(null, Array(4)).forEach(() => process.stdout.write('\n'))
await ws.close()
process.exit(0)
}
ws.on('open', () => {
sendSocketMessage(ws, WebSocketEvents.SetupWallet, address)
setupKeyListener({
onEnter: () => sendSocketMessage(ws, WebSocketEvents.ReadBalance),
onClose: () => shutdown(),
})
loadWalletBalanceLoop(ws, 60)
})
ws.on('message', (json: string) => {
const {data, type} = JSON.parse(json)
switch (type) {
case WebSocketEvents.BalanceUpdated: {
balance = data.balance
printBalance(currency, price, balance)
break
}
case WebSocketEvents.PriceUpdated: {
price = data.price
printBalance(currency, price, balance)
break
}
}
})
ws.on('close', () => shutdown())