-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptopia.js
123 lines (108 loc) · 3.4 KB
/
cryptopia.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
const axios = require('axios');
const crypto = require('crypto');
const cryptopiaConfig = {
API_KEY: 'YOUR_API_KEY',
API_SECRET: 'YOUR_API_SECRET',
HOST_URL: 'https://www.cryptopia.co.nz/api',
};
const buildAuth = (data, config, path) => {
const nonce = crypto.randomBytes(64).toString('hex');
const md5 = crypto.createHash('md5').update(JSON.stringify(data)).digest('base64');
const signature = `${config.API_KEY}POST${encodeURIComponent(config.HOST_URL + path).toLowerCase()}${nonce}${md5}`;
const hmac = crypto.createHmac('sha256', new Buffer(config.API_SECRET, "base64")).update(signature).digest('base64');
return `amx ${config.API_KEY}:${hmac}:${nonce}`;
};
const privateRequest = async (data, path) => {
const requestConfig = {
method: 'POST',
url: cryptopiaConfig.HOST_URL + path,
data: data,
headers: {
'Authorization': buildAuth(data, cryptopiaConfig, path),
'Content-Type': 'application/json; charset=utf-8',
},
};
try {
const response = await axios(requestConfig);
return response;
}
catch (err) {
return err;
}
};
const publicRequest = async (path) => {
const requestConfig = {
method: 'GET',
url: cryptopiaConfig.HOST_URL + path,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
responseType: 'json',
};
try {
const response = await axios(requestConfig);
return response;
}
catch (err) {
return err;
}
};
const getCurrentMarketPrice = async (pair) => {
try {
const currentMarketPrice = await publicRequest(`/getmarket/${pair}`);
return {
buyPrice: currentMarketPrice.data.Data.AskPrice,
sellPrice: currentMarketPrice.data.Data.BidPrice,
};
}
catch (err) {
return err;
}
};
const checkCoinBalance = async (coin) => {
const data = {
Currency: coin,
};
try {
const balance = await privateRequest(data, '/getbalance');
return balance.data.Data[0].Available;
}
catch (err) {
return err;
}
};
const putOrder = async (pair, price, amount, type) => {
const data = {
Market: pair,
Type: type,
Rate: price,
Amount: amount,
};
const response = await privateRequest(data, '/submittrade');
// console.log('putOrderResponse', response);
return response;
};
const autoBuyAndSell = async (coin, market, buyPercent, sellPercent, amount) => {
try {
const pair = `${coin}_${market}`;
const buyFee = (amount * 0.002).toFixed(8);
const currentMarketPrice = await getCurrentMarketPrice(pair);
console.log('buyFee: ', buyFee);
console.log('maker price', currentMarketPrice);
const orderBuyPrice = (currentMarketPrice.buyPrice * buyPercent).toFixed(8);
console.log('orderBuyPrice', orderBuyPrice);
const orderBuyAmount = ((amount - buyFee) / orderBuyPrice).toString().match(/^-?\d+(?:\.\d{0,8})?/)[0];
console.log('order buy amount', orderBuyAmount);
const orderSellPrice = (currentMarketPrice.sellPrice * sellPercent).toString().match(/^-?\d+(?:\.\d{0,8})?/)[0];
console.log('order sell price', orderSellPrice);
const orderSellAmount = await checkCoinBalance(coin);
console.log('order sell amount', orderSellAmount);
const newBuyOrder = await putOrder(pair, orderBuyPrice, orderBuyAmount, 'Buy');
// const newSellOrder = await putOrder(pair, orderSellPrice, orderSellAmount, 'Sell');
return newBuyOrder;
}
catch (err) {
return err;
}
};
module.exports = autoBuyAndSell;