-
Notifications
You must be signed in to change notification settings - Fork 21
/
example-ws.js
161 lines (128 loc) · 3.66 KB
/
example-ws.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict'
const Sunbeam = require('.')
const config = require('./config/example-ws.config.json')
const env = 'staging'
const opts = config[env]
const keys = opts.keys
const httpEndpoint = opts.eos.httpEndpoint
const { Api, JsonRpc } = require('eosjs')
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig')
const fetch = require('node-fetch')
const { TextDecoder, TextEncoder } = require('util')
const signatureProvider = new JsSignatureProvider(keys)
const rpc = new JsonRpc(httpEndpoint, { fetch })
const api = new Api({
// use available keys to get partially signed transaction on the client side
authorityProvider: {
getRequiredKeys: () => signatureProvider.getAvailableKeys()
},
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
})
const client = {
rpc,
api
}
// setup sunbeam
const ws = new Sunbeam(client, opts)
ws.on('message', (m, t) => {
console.log('------- msg -----')
console.log(t, m)
})
ws.on('error', (m) => {
console.error('ERROR!')
console.error(m)
})
const pair = 'tBTCUSD'
const placedOrders = []
ws.on('open', async () => {
/*
* Pubkey update requires modification of the eosjs api instance.
* Modified API instance is required on the Sunbeam instance creation
*
* const api = new Api({
* // use available keys to get partially signed transaction on the client side
* authorityProvider: {
* getRequiredKeys: () => signatureProvider.getAvailableKeys()
* },
* rpc,
* signatureProvider,
* textDecoder: new TextDecoder(),
* textEncoder: new TextEncoder()
* })
*/
// await ws.updateKey({ account: 'dfuser111111', pubkey: 'EOS4yoFY8MChyDzQUk1H6hr7CwEfPmWhRatSqaCCyxk8hJUZZ3uii' })
await ws.auth()
ws.send('priv', { event: 'chain' })
// or const meta = await ws.requestChainMeta('priv')
ws.send('priv', { event: 'symbols' })
ws.subscribePublicTrades(pair)
ws.subscribeOrderbook(pair)
setTimeout(() => {
ws.unsubscribePublicTrades(pair)
ws.unsubscribeOrderbook(pair)
}, 3000)
ws.onOrderbook({ symbol: pair }, (ob) => {
console.log(`ws.onOrderbook({ symbol: ${pair} })`)
console.log(ob)
})
ws.onWallet({}, (wu) => {
console.log('ws.onWallet')
console.log(wu)
})
ws.onManagedWallet({}, (mw) => {
console.log('ws.onManagedWallet')
console.log(mw)
})
ws.onOrders({}, (data) => {
console.log('ws.onOrders({})')
console.log(data)
})
// filter enabled
ws.onOrders({ symbol: pair }, (data) => {
console.log(`ws.onOrders({ symbol: ${pair} })`)
console.log(data)
if (!Array.isArray(data[0])) {
placedOrders.push(data[0])
}
})
ws.onManagedOrders({}, (orders) => {
console.log('ws.onManagedOrdersUpdate')
console.log(orders)
})
ws.onManagedOrders({ symbol: pair }, (orders) => {
console.log(`ws.onManagedOrders({ symbol: ${pair} }`)
console.log(orders)
})
ws.onPrivateTrades({}, (data) => {
console.log('ws.onPrivateTrades({})')
console.log('private trade', data)
})
/* const { txResult, txData } = await ws.deposit({
currency: 'BTC',
amount: '0.687'
})
const { txResult, txData } = await ws.withdraw({
currency: 'BTC',
amount: '0.02'
}) */
// available types: EXCHANGE MARKET, EXCHANGE LIMIT, EXCHANGE STOP, EXCHANGE STOP LIMIT, EXCHANGE TRAILING STOP, EXCHANGE FOK, EXCHANGE IOC
const order = {
symbol: pair,
price: '900',
amount: '-0.01',
type: 'EXCHANGE LIMIT',
cid: '1332'
}
await ws.place(order)
setTimeout(() => {
console.log('state', JSON.stringify(ws.state, null, 2))
const a = placedOrders[0]
if (a) {
ws.cancel({ id: Number(a) })
}
}, 4000)
})
ws.open()