-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
87 lines (73 loc) · 1.96 KB
/
api.js
File metadata and controls
87 lines (73 loc) · 1.96 KB
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
const axios = require('axios')
const crypto = require('crypto')
const query = require('querystring')
const base = process.env.API_URL
const apiKey = process.env.API_KEY
const secret = process.env.SECRET_KEY
/**
* For connect with user's data
* @param path
* @param data
* @param method
* @returns {Promise<any>}
*/
async function privateCall(path, data = {}, method = 'GET') {
let timestamp = Date.now()
let signature = crypto.createHmac("sha256", secret)
.update(`${query.stringify({...data, timestamp})}`)
.digest("hex")
let newData = {...data, timestamp, signature}
try {
let qs = newData ? `?${query.stringify(newData)}` : ""
let result = await axios({
method,
url: `${base}${path}${qs}`,
headers: {"X-MBX-APIKEY": apiKey}
})
return result.data
} catch (e) {
console.log(e.response); // this is the main part. Use the response property from the error object
return e.response;
}
}
/**
* Generic function for use without API_KEY
* @param path
* @param data
* @param method
* @returns {Promise<any>}
*/
async function publicCall(path, data, method = 'GET') {
try {
let qs = data ? `?${query.stringify(data)}` : ""
let result = await axios({
method,
url: `${base}${path}${qs}`
})
return result.data
} catch (e) {
console.log(e.response); // this is the main part. Use the response property from the error object
return e.response;
}
}
/**
* Public Functions
*/
async function time() {
return publicCall("time")
}
//bids = compras
//asks = vendas
async function depth(symbol = process.env.SYMBOL, limit = 5) {
return publicCall("depth", {symbol, limit})
}
async function exchange() {
return publicCall("exchangeInfo")
}
/**
* Private Functions
*/
async function account() {
return privateCall("account")
}
module.exports = {time, depth, exchange, account}