forked from codedot/xmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmm.js
executable file
·167 lines (150 loc) · 3.06 KB
/
xmm.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
162
163
164
165
166
167
#!/usr/bin/env node
"use strict";
function ask(tx)
{
const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
console.info(tx.hash);
console.info(JSON.parse(tx.json));
return new Promise(resolve => {
const expected = "submit";
const query = `Type "${expected}" to confirm: `;
rl.question(query, answer => {
rl.close();
resolve(expected == answer);
});
});
}
global.print = tx => {
console.info(tx.hash);
console.info(JSON.parse(tx.json));
console.info(`${tx.code}: ${tx.desc}`);
};
global.connect = callback => config => {
setTimeout(() => {
abort("Timed out");
}, config.timeout * 1e3);
if (!config.yes)
config.yes = ask;
callback = callback.bind(null, config);
require(".").connect(config).then(callback).catch(abort);
};
global.abort = (msg, error) => {
if (error)
console.error(error);
else
console.error(msg);
process.exit(1);
};
const getobj = x => ("string" == typeof x) ? JSON.parse(x) : x;
const opts = {
assets: {
coerce: getobj,
describe: "Dictionary of assets",
default: {}
},
count: {
alias: "n",
describe: "Number of ledgers to close",
default: 1
},
cushion: {
alias: "f",
describe: "Factor to multiply estimated fee",
default: 1
},
delta: {
alias: "d",
describe: "Stake to trade",
default: 0.01
},
dry: {
alias: "p",
describe: "Output script without running",
boolean: true
},
hedge: {
coerce: getobj,
describe: "List of pairs to trade",
default: []
},
ledger: {
alias: "l",
describe: "Historical ledger version",
number: true
},
maxfee: {
alias: "m",
describe: "Maximum fee to pay",
default: 1e-5
},
offset: {
alias: "o",
describe: "Offset from the current legder",
default: 3
},
server: {
alias: "s",
describe: "WebSocket server",
default: "wss://s1.ripple.com"
},
timeout: {
alias: "t",
describe: "Timeout in seconds",
default: 60
},
wallets: {
coerce: getobj,
describe: "Dictionary of wallets",
default: {}
},
yes: {
alias: "y",
describe: "Do not ask for confirmation",
boolean: true
}
};
const home = require("os").homedir();
const conf = require("path").join(home, ".xmm.json");
function load(path)
{
try {
const read = require("fs").readFileSync;
const json = read(path, "utf-8");
const dict = JSON.parse(json);
return dict;
} catch (error) {
console.warn("%s: Could not load configuration", path);
return {};
}
}
require("yargs")
.usage("Usage: $0 [options] <command> [arguments]")
.options(opts)
.config("config", load)
.alias("config", "c")
.default("config", conf, "~/.xmm.json")
.command(require("./altnet"))
.command(require("./balance"))
.command(require("./cost"))
.command(require("./generate"))
.command(require("./hedge"))
.command(require("./kill"))
.command(require("./ledger"))
.command(require("./offer"))
.command(require("./send"))
.command(require("./trust"))
.command(require("./view"))
.command(require("./what"))
.demand(1)
.strict()
.recommendCommands()
.version()
.alias("version", "v")
.help()
.alias("help", "h")
.wrap(70)
.fail(abort)
.argv;