-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
97 lines (78 loc) · 2.41 KB
/
core.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
const baseUrl = 'https://crp.is:8182/';
function body(obj){
let res = '';
for(const p in obj) if(obj[p]) res += `${encodeURIComponent(p)}=${encodeURIComponent(obj[p])}&`;
return res.slice(0, -1);
}
function argsInUrl(method){
return method === 'GET';
}
export default class CryptonAPI{
#transport
#saveToken
constructor(transport, saveToken){
this.#transport = transport;
this.#saveToken = saveToken;
}
async #call(method, httpMethod, args){
const urlArgs = argsInUrl(method);
const data = args ? body(args) : '';
let url = `${baseUrl}${method}`;
if(urlArgs) url += `?${data}`;
const parsed = JSON.parse(await this.#transport(httpMethod, url, urlArgs ? null : data));
if(parsed.success){
return parsed.result;
} else {
throw new Error(`${parsed.code}: ${parsed.text}`)
}
}
async login(pubkey, password, pin){
const { auth_token, user_session: { session, user } } = await this.#call('user/login', 'POST', {
PublicKey: pubkey,
password,
'2fa_pin': pin,
});
this.session = session;
this.user = user;
await this.#saveToken(auth_token);
}
async logout(){
await this.#call('user/logout', 'POST');
}
balance(){
return this.#call('user/balance', 'GET');
}
buy(pair, amount, price){
return this.#call('market/buy', 'POST', { pair, amount, price });
}
sell(pair, amount, price){
return this.#call('market/sell', 'POST', { pair, amount, price });
}
hold(orderId){
return this.#call('market/hold', 'POST', { order_id: orderId });
}
hold(orderId){
return this.#call('market/cancel', 'POST', { order_id: orderId });
}
pairs(){
return this.#call('market/pairs', 'GET');
}
panel(pair){
return this.#call('market/panel', 'POST', { pair });
}
curlist(){
return this.#call('market/curlist', 'GET');
}
orders(status, task){
return this.#call('orders', 'GET', { status, task });
}
orderHistory(orderId){
return this.#call('orders/history', 'POST', { order_id: orderId });
}
history(type){
return this.#call('history', 'POST', { type });
}
tradeHistory(orderId){
return this.#call('history/trade', 'GET', { order_id: orderId });
}
}