-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoveOrder.js
executable file
·122 lines (116 loc) · 3.6 KB
/
moveOrder.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
#!/usr/bin/node
const RippleAPI = require('ripple-lib').RippleAPI;
const BigNumber = require('bignumber.js');
const Tx = require('./tx.js');
const formatNewOrder = require('./orderUtil.js').formatNewOrder;
const formatValue = require('./orderUtil.js').formatValue;
const fs = require('fs');
var removeComma = function(arg) {
return arg.replace(/,/g, '');
}
const api = new RippleAPI({
server: 'wss://s1.ripple.com', // Public rippled server hosted by Ripple, Inc.
maxFeeXRP: '0.00001'
});
const argv = require('yargs')
.option('debug', {
alias: 'd',
describe: 'If true, just print out target price, do not submit',
type: 'boolean',
default: false
})
.option('type', {
alias: 't',
describe: 'The order type to place or replace',
choices: ['buy', 'sell']
})
.option('old', {
alias: 'o',
describe: 'The price of the old order to be replaced',
type: 'string',
})
.option('new', {
alias: 'n',
describe: 'The price of the new order to be placed',
type: 'string',
})
.option('secret_file', {
describe: 'The path of secrt file',
type: 'string',
default: '.secret.json'
})
.coerce('old', removeComma)
.coerce('new', removeComma)
.demandOption(['type', 'old', 'new'])
.help()
.strict()
.argv
var secrets = JSON.parse(fs.readFileSync(argv.secret_file, 'utf8'));
var account = secrets.account;
var secret = secrets.secret;
var pair = secrets.pair
api.on('error', (errorCode, errorMessage, data) => {
console.error('api error: ', errorCode + ': ' + errorMessage + ': ' + data);
});
api.connect().then(() => {
return api.getOrders(account, {
}).then(response => {
var order;
if (argv.old !== undefined) {
response.forEach(function(i) {
var spec = i.specification
var direction = spec.direction;
var quantity = spec.quantity;
var totalPrice = spec.totalPrice;
if ((argv.type === direction && quantity.currency === pair.base.currency && totalPrice.currency === pair.counter.currency) ||
(argv.type !== direction && quantity.currency === pair.counter.currency && totalPrice.currency === pair.base.curency)) {
var targetPrice = BigNumber(argv.old)
var price = BigNumber(spec.totalPrice.value).div(BigNumber(
spec.quantity.value))
if (targetPrice.minus(price).abs().lt(0.001)) {
order = i
}
}
});
if (order === undefined) {
throw 'order not found, price: ' + argv.old
}
}
return {
orderToReplace: order,
newPrice: argv.type === 'sell' ? BigNumber(argv.new) : BigNumber(1).div(
BigNumber(argv.new))
};
}).then(rv => {
var order = rv.orderToReplace;
var spec = order.specification;
var newOrder = {
direction: spec.direction,
quantity: spec.quantity,
totalPrice: spec.totalPrice,
orderToReplace: order.properties.sequence
};
if (argv.type === 'sell') {
newOrder.totalPrice.value = formatValue(
BigNumber(newOrder.quantity.value).times(rv.newPrice),
newOrder.totalPrice.currency);
} else {
newOrder.quantity.value = formatValue(
BigNumber(newOrder.totalPrice.value).times(rv.newPrice),
newOrder.quantity.currency);
}
console.log(newOrder);
if (argv.debug) {
return
}
return api.prepareOrder(account, newOrder, {
maxLedgerVersionOffset: 5
}).then(prepared => {
return Tx.submit(api, prepared, secret, Tx.reportSequence, console.log, console.log);
});
})
}).then(() => {
api.disconnect().then(() => {
process.exit();
});
}).catch(console.error);