-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMEXC_spot.js
39 lines (33 loc) · 1.37 KB
/
MEXC_spot.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
require('dotenv').config();
const axios = require('axios');
const crypto = require('crypto');
function placeOrder(quantity, price) {
const apiKey = process.env.MEXC_API_KEY;
const secretKey = process.env.MEXC_API_SECRET;
const baseEndpoint = 'https://api.mexc.com';
const endpoint = '/api/v3/order';
const orderParams = {
symbol: 'ELUSDT',
side: 'BUY',
type: 'LIMIT',
quantity: quantity, // The quantity is set by the function argument.
price: price, // The price is set by the function argument.
recvWindow: 5000,
timestamp: Date.now() // Current Unix timestamp in milliseconds.
};
const queryString = Object.keys(orderParams).map(key => `${key}=${encodeURIComponent(orderParams[key])}`).join('&');
const signature = crypto.createHmac('sha256', secretKey).update(queryString).digest('hex');
orderParams.signature = signature;
const headers = {
'X-MEXC-APIKEY': apiKey,
'Content-Type': 'application/json'
};
axios.post(`${baseEndpoint}${endpoint}`, null, { headers: headers, params: orderParams })
.then(response => {
console.log('Order placed successfully:', response.data);
})
.catch(error => {
console.error('Error placing order:', error.response ? error.response.data : error.message);
});
}
placeOrder(1900, 0.005300);