-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.ts
143 lines (129 loc) · 4.14 KB
/
orders.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
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
import request from "request";
import crypto from "crypto";
import { key, secret } from "./config";
const baseurl = "https://api.coindcx.com";
const timeStamp = Math.floor(Date.now());
export type TMarketDetail = {
coindcx_name: string | null;
base_currency_short_name: string | null;
target_currency_short_name: string | null;
target_currency_name: string | null;
base_currency_name: string | null;
min_quantity: number | null;
max_quantity: number | null;
max_quantity_market: number | null;
min_price: number | null;
max_price: number | null;
min_notional: number | null;
base_currency_precision: number | null;
target_currency_precision: number | null;
step: number | null;
order_types: string[] | null;
symbol: string | null;
ecode: string | null;
bo_sl_safety_percent: any | null;
max_leverage: number | null;
max_leverage_short: number | null;
pair: string | null;
status: string | null;
};
export const createOrder = async (
side: "buy" | "sell",
market: string,
price: number,
quantity: number,
clientOrderId: string
): Promise<void> => {
console.log("placing order");
return new Promise((resolve, reject) => {
const body = {
side,
order_type: "limit_order",
market,
price_per_unit: price,
total_quantity: quantity,
timestamp: timeStamp,
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, response, body) {
if (error) {
console.log("error while cancelling all orders:", error);
reject(error);
} else {
console.log(body);
resolve();
}
});
});
};
export const cancelMultipleOrdersById = () => {};
export const marketDetails = async (market: string) => {
return new Promise<TMarketDetail | undefined>((resolve, reject) => {
request.get(
baseurl + "/exchange/v1/markets_details",
function (error, response, body) {
if (error) {
console.log(
"There was an error while fetching market details:",
error
);
reject(error);
} else {
const marketInfo: TMarketDetail[] = JSON.parse(
response.body
);
const sortedData = marketInfo.find(
(x) => x.coindcx_name === market
);
resolve(sortedData);
}
}
);
});
};
export const cancelAllOrders = async (market: string): Promise<void> => {
console.log("cancelling order");
return new Promise((resolve, reject) => {
const body = {
market,
timestamp: timeStamp,
};
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, response, body) {
if (error) {
console.log("error while cancelling all orders:", error);
reject(error);
} else {
console.log(body);
resolve();
}
});
});
};