-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.ts
79 lines (69 loc) · 2.48 KB
/
Order.ts
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
const request = require('request')
const crypto = require('crypto')
const baseurl = "https://api.coindcx.com"
import { key, secret } from "./config"
export const createOrder = (side: "buy" | "sell", market: string, price: number, quantity: number, clientOrderId: string) => {
return new Promise<void>((resolve, reject) => {
const body = {
side,
order_type: "limit_order",
market,
price_per_unit: price,
total_quantity: quantity,
timestamp: Math.floor(Date.now()),
client_order_id: clientOrderId
};
const payload = new Buffer(JSON.stringify(body)).toString();
const signature = crypto.createHmac("sha256", secret).update(payload).digest("hex");
const options = {
url: baseurl + "/exchange/v1/orders/create",
headers: {
"X-AUTH-APIKEY": key,
"X-AUTH-SIGNATURE": signature
},
json: true,
body: body
};
request.post(options, function(error: any, response: any, body: any) {
if (error) {
console.log("Error while Creating Orders");
reject("Error while Creating Orders");
} else if (body.code === 400) {
console.log("Insufficient Funds");
reject("Insufficient Funds");
} else {
console.log("Placed all Orders");
console.log(body);
resolve();
}
});
});
};
export const cancelAll = (market: string) => {
return new Promise<void>((resolve) => {
const body = {
market,
timestamp: Math.floor(Date.now())
}
const payload = new Buffer(JSON.stringify(body)).toString();
const signature = crypto.createHmac('sha256', secret).update(payload).digest('hex')
const options = {
url: baseurl + "/exchange/v1/orders/cancel_all",
headers: {
'X-AUTH-APIKEY': key,
'X-AUTH-SIGNATURE': signature
},
json: true,
body: body
}
request.post(options, function(error: any, response: any, body: any) {
if (error) {
console.log("Error while Cancelling Orders");
} else {
console.log("Cancelled all orders");
console.log(body);
}
resolve();
})
})
}