-
Notifications
You must be signed in to change notification settings - Fork 5
/
create-orders.js
68 lines (58 loc) · 1.95 KB
/
create-orders.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
'use strict'
const {RippleAPI} = require('ripple-lib')
const WebSocket = require('ws');
const txUtils = require('./tx-utils.js')
const winston = require('winston')
const loga = require('./env-utils.js').loga(winston)
const logs = require('./env-utils.js').logs(winston)
const env = require('./env-utils.js').makeEnv()
const createInitialOrders = require('./env-utils').createInitialOrders
const api = new RippleAPI({
server: env.net
})
const ws = new WebSocket(env.net) // need ws because ripple-lib getTransaction sucks
api.on('error', (errorCode, errorMessage) => {
loga.error(errorCode + ': ' + errorMessage)
})
api.on('disconnected', (code) => {
loga.info('disconnected, code:', code)
})
function start(){
var txCache = new txUtils.TxCache()
let orders = []
env.botConfigs.forEach(botConfig => {
orders = orders.concat(createInitialOrders(botConfig))
});
var wait = 10
ws.on('open', function open() {
api.connect().then(() => {
loga.info('connected')
loga.info('sending orders')
txUtils.sendMultipleOrders(env.address, env.secret, api, orders)
api.on('ledger', ledger => {
let tx = {
minLedgerVersion : ledger.ledgerVersion
}
loga.info(ledger.ledgerVersion)
if (txCache.immediates.length > 0){
txUtils.sendMultipleOrders(env.address, env.secret, api, txCache.getImmediatesAndReset(), txCache)
}
txCache.getUnvalidateds(ws, ledger.ledgerVersion).forEach(unvalidated => {
ws.send(unvalidated)
})
wait--
if (wait <= 0 && Object.keys(txCache.delayeds).length == 0 && txCache.immediates.length == 0){
process.exit(1)
}
})
})
})
ws.on('message', function incoming(data) {
var info = JSON.parse(data)
if(info.result.hasOwnProperty('validated') && info.result.validated){
loga.info('validated = true for ' + info.result.hash)
txCache.delayeds.delete(info.result.hash)
}
})
}
start()