-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.js
125 lines (107 loc) · 3.39 KB
/
exchange.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
const axios = require('axios');
const CryptoJS = require('crypto-js');
class Exchange {
constructor(apiKey, apiSecret) {
this.baseUrl = 'https://exchange-api.lcx.com/api';
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}
async getMarketData(pair) {
const endpoint = '/book';
const params = { pair };
const config = {
method: 'get',
url: `${this.baseUrl}${endpoint}`,
headers: {
'Content-Type': 'application/json'
},
params
};
try {
const response = await axios(config);
return response.data;
} catch (error) {
console.error('Error fetching market data:', error.response ? error.response.data : error.message);
}
}
async getPairDetails(pair) {
const endpoint = '/pair';
const params = { pair };
const config = {
method: 'get',
url: `${this.baseUrl}${endpoint}`,
headers: {
'Content-Type': 'application/json'
},
params
};
try {
const response = await axios(config);
return response.data;
} catch (error) {
console.error('Error fetching pair details:', error.response ? error.response.data : error.message);
}
}
getHeaders(method, endpoint, payload) {
const requestString = method + endpoint + JSON.stringify(payload);
const hash = CryptoJS.HmacSHA256(requestString, this.apiSecret);
const signature = CryptoJS.enc.Base64.stringify(hash);
return {
'Content-Type': 'application/json',
'x-access-key': this.apiKey,
'x-access-sign': signature,
'x-access-timestamp': Date.now()
};
}
async createOrder(order) {
const endpoint = '/create';
const url = `${this.baseUrl}${endpoint}`;
const method = 'POST';
const headers = this.getHeaders(method, endpoint, order);
try {
const response = await axios.post(url, order, { headers });
return response.data;
} catch (error) {
console.error('Error creating order:', error.response ? error.response.data : error.message);
}
}
async cancelOrder(orderId) {
const endpoint = '/cancel';
const url = `${this.baseUrl}${endpoint}`;
const method = 'DELETE';
const params = { orderId };
const headers = this.getHeaders(method, endpoint, {});
try {
const response = await axios.delete(url, { headers, params });
return response.data;
} catch (error) {
console.error('Error canceling order:', error.response ? error.response.data : error.message);
}
}
async getBalances() {
const endpoint = '/balances';
const url = `${this.baseUrl}${endpoint}`;
const method = 'GET';
const headers = this.getHeaders(method, endpoint, {});
try {
const response = await axios.get(url, { headers });
return response.data;
} catch (error) {
console.error('Error fetching balances:', error.response ? error.response.data : error.message);
}
}
async getOpenOrders(pair, offset = 1) {
const endpoint = '/open';
const params = { pair, offset };
const url = `${this.baseUrl}${endpoint}`;
const method = 'GET';
const headers = this.getHeaders(method, endpoint, {});
try {
const response = await axios.get(url, { headers, params });
return response.data;
} catch (error) {
console.error('Error fetching open orders:', error.response ? error.response.data : error.message);
}
}
}
module.exports = Exchange;