-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample-ual.js
More file actions
147 lines (118 loc) · 3.04 KB
/
example-ual.js
File metadata and controls
147 lines (118 loc) · 3.04 KB
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
'use strict'
const Sunbeam = require('.')
const UALPrivateKey = require('./ual-privatekey/PrivateKeyUser')
const config = require('./config/example-ual.config.json')
const env = 'staging'
const opts = config[env]
const fetch = require('node-fetch')
// nodejs
const { Api, JsonRpc } = require('eosjs')
const { TextDecoder, TextEncoder } = require('util')
const rpc = new JsonRpc(opts.eos.httpEndpoint, { fetch })
const api = new Api({
rpc,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
})
const client = {
rpc,
api
}
const ualUser = new UALPrivateKey(rpc, opts.auth.ual.accountName, opts.auth.ual.privateKey)
const ws = new Sunbeam(client, {
...opts,
eos: {
...opts.eos,
auth: {
ual: {
user: ualUser
}
}
}
})
const pair = 'tBTCUSD'
const placedOrders = []
ws.on('message', (m) => {
console.log(m)
})
ws.on('error', (m) => {
console.error('ERROR!')
console.error(m)
})
ws.on('open', async () => {
console.log(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)
})
ws.onPublicTrades({}, (data) => {
console.log('ws.onPublicTrades({})')
console.log('public 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: '11100',
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()