yhtx 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +65 -0
  2. package/index.js +102 -0
  3. package/package.json +24 -0
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # About
2
+
3
+ Simple HTX (HTX.com, previouly Huobi.pro) api for Node.js to access REST API.
4
+
5
+ ## Usage example 1 (api class)
6
+
7
+ ```
8
+ const api = require('yhtx')
9
+ let acc = new api(<apiKey>, <secret>);
10
+
11
+ async function test(){
12
+ let r = await acc.get('/v1/account/accounts')
13
+ console.log(r)
14
+ }
15
+ test()
16
+ ```
17
+
18
+ ## Usage example 2 (call function)
19
+
20
+ ```
21
+ const {call} = require('yhtx')
22
+ let apiKey = <apiKey>;
23
+ let secret = <secret>;
24
+
25
+ async function test(){
26
+ let data = {}
27
+ let r = await call('/v1/account/accounts', {apiKey, secret, data})
28
+ console.log(r)
29
+ }
30
+ test()
31
+ ```
32
+
33
+ ## Installation
34
+
35
+ ```
36
+
37
+ npm i yhtx
38
+
39
+ ```
40
+
41
+ ## Parameters (call function)
42
+
43
+ function call(path, {data, apiKey, secret, method} )
44
+
45
+ - path = eg: "/v5/account/wallet-balance" [ref](https://bybit-exchange.github.io/docs/api-explorer/v5/account/wallet)
46
+ - apiKey = Bybit apiKey
47
+ - secret = Bybit api secret
48
+ - optional
49
+ - data = params to be passed in, eg: {symbol:'BTC'}
50
+ - method = "GET" (default) OR "POST"
51
+
52
+ ## Parameters (api function)
53
+
54
+ ```
55
+
56
+ api.get(path[, data])
57
+ api.post(path[, data])
58
+
59
+ ```
60
+
61
+ - data (optional) = refer above
62
+
63
+ ## return
64
+
65
+ return object (result from HTX api)
package/index.js ADDED
@@ -0,0 +1,102 @@
1
+ const crypto = require("crypto");
2
+ const axios = require("axios");
3
+
4
+ function getSign(queryString, secret, method, path) {
5
+ let signString = [method, "api.huobi.pro", path, queryString].join("\n");
6
+ return crypto.createHmac("sha256", secret).update(signString).digest("base64");
7
+ }
8
+
9
+ async function call(path, { apiKey, secret, data, method, recvWindow } = {}) {
10
+ apiKey ||= "";
11
+ secret ||= "";
12
+ method ||= "GET";
13
+ recvWindow ??= 30000;
14
+ const origin = "https://api.huobi.pro"; //https:api-aws.huobi.pro
15
+ data ||= {};
16
+
17
+ let timestamp = Date.now();
18
+ let url;
19
+ let sign;
20
+
21
+ timestamp = new Date(timestamp).toISOString().slice(0, 19);
22
+ body = {
23
+ AccessKeyId: apiKey,
24
+ SignatureMethod: "HmacSHA256",
25
+ SignatureVersion: 2,
26
+ Timestamp: timestamp,
27
+ ...data,
28
+ };
29
+ log({ body });
30
+ let pars = [];
31
+ for (let k in body) pars.push(k + "=" + encodeURIComponent(body[k]));
32
+ let queryString = pars.sort().join("&");
33
+
34
+ if (/*not yet supported*/ method === "POST") {
35
+ url = origin + path + "?" + queryString;
36
+ sign = getSign(queryString, secret, method, path);
37
+ url += `&Signature=${sign}`;
38
+ body.Signature = sign;
39
+ } /*GET*/ else {
40
+ url = origin + path + "?" + queryString;
41
+ sign = getSign(queryString, secret, method, path);
42
+ url += `&Signature=${sign}`;
43
+ body.Signature = sign;
44
+ }
45
+
46
+ var headers = {
47
+ // "Content-Type": "application/json",
48
+ // "User-Agent":
49
+ // "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
50
+ };
51
+
52
+ if (method === "POST") {
53
+ headers["Content-Type"] = "application/json; charset=utf-8";
54
+ }
55
+
56
+ var config = {
57
+ method,
58
+ url,
59
+ headers,
60
+ ...body,
61
+ };
62
+
63
+ let r = await axios(config)
64
+ .then((r) => {
65
+ // log(r);
66
+ if (r.data?.code != 200 /*success*/) console.log(r.data);
67
+ let rs = r.data.data;
68
+ return rs;
69
+ })
70
+ .catch(function (e) {
71
+ console.log(e.response.data);
72
+ });
73
+
74
+ return r;
75
+ }
76
+
77
+ class api {
78
+ constructor(apiKey = "", secret = "") {
79
+ this.apiKey = apiKey;
80
+ this.secret = secret;
81
+ }
82
+ async get(path, pr = {}) {
83
+ let data = {
84
+ apiKey: this.apiKey,
85
+ secret: this.secret,
86
+ data: pr,
87
+ };
88
+ return call(path, data);
89
+ }
90
+ async post(path, pr = {}) {
91
+ let data = {
92
+ apiKey: this.apiKey,
93
+ secret: this.secret,
94
+ method: "POST",
95
+ data: pr,
96
+ };
97
+ return call(path, data);
98
+ }
99
+ }
100
+
101
+ module.exports = api;
102
+ module.exports.call = call;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "yhtx",
3
+ "version": "1.0.0",
4
+ "description": "HTX API",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "dependencies": {
10
+ "axios": "^1.7.2"
11
+ },
12
+ "keywords": [
13
+ "htx",
14
+ "api",
15
+ "huobi",
16
+ "exchange",
17
+ "crypto",
18
+ "currency",
19
+ "cryptocurrency",
20
+ "coin"
21
+ ],
22
+ "author": "",
23
+ "license": "ISC"
24
+ }