-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
209 lines (177 loc) · 5.54 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const express = require("express");
const kraken = require("./kraken-api.js")
const https = require('https');
const fs = require('fs')
const tolerance = 1E-3;
const app = express();
const API_KEY_FILE_NAME = "api_key"
let api_key,
desired_btc_ratio,
desired_eth_ratio,
btc_rate,
eth_rate,
btc_amount,
eth_amount;
function readApiKeyFromFile(){
return new Promise((resolve, reject) => {
if (api_key === undefined){
fs.readFile(API_KEY_FILE_NAME, (err, data) => {
if (err) {
reject (err)
return
}
resolve(data.toString());
})
}
else{
resolve(api_key);
}
})
}
function updateRate(crypto_currency_code, on_rate_update){
let options = {
"method": "GET",
"hostname": "rest.coinapi.io",
"path": `/v1/exchangerate/${crypto_currency_code}/USD`,
"headers": {'X-CoinAPI-Key': api_key}
};
let request = https.request(options, function (response) {
let chunks = [];
response.on("data", function (chunk) {
chunks.push(chunk);
});
response.on("end", function (chunk) {
on_rate_update(JSON.parse(chunks));
});
response.on("error", function (e) {
throw new Error(e.message);
});
});
request.end();
}
function getActualPortfolioValue() {
return getBtcValue() + getEthValue();
}
function getBtcValue() {
return btc_amount * btc_rate;
}
function getEthValue() {
return eth_amount * eth_rate;
}
function getPortfolioValues()
{
return {
"actual_portfolio_value" : getActualPortfolioValue(),
"desired_btc_ratio" : desired_btc_ratio,
"desired_eth_ratio" : desired_eth_ratio,
"btc_amount" : btc_amount,
"eth_amount" : eth_amount,
}
}
function validateValues(body){
if (body.btc_ratio <= 0 || body.eth_ratio <= 0 || body.btc_ratio + body.eth_ratio !== 1.0) {
throw new Error("BTC/ETH ratios must be greater than 0 and amount to 1.0");
}
desired_btc_ratio = body.btc_ratio;
desired_eth_ratio = body.eth_ratio;
}
function initializePortfolio(){
return new Promise((resolve, reject) => {
updateRate("BTC", (btc_json) => {
btc_rate = btc_json.rate;
updateRate("ETH", (eth_json) => {
eth_rate = eth_json.rate;
kraken.getBalance().then(function (balance) {
const balanceFromKraken = balance['result'];
btc_amount = parseFloat(balanceFromKraken[kraken.BTC_SYMBOL]);
eth_amount = parseFloat(balanceFromKraken[kraken.ETH_SYMBOL]);
console.log(btc_amount);
console.log(eth_amount);
resolve(getPortfolioValues());
})
});
});
})
}
function validateExpectedRatios(eth_to_buy, btc_to_buy){
var newEthAmount = eth_amount + eth_to_buy;
var newBtcAmount = btc_amount + btc_to_buy;
var newEthRatio = newEthAmount * eth_rate / (newEthAmount * eth_rate + newBtcAmount * btc_rate);
var newBtcRatio = newBtcAmount * btc_rate / (newEthAmount * eth_rate + newBtcAmount * btc_rate);
if (Math.abs(newBtcRatio - desired_btc_ratio) > tolerance ||
Math.abs(newEthRatio - desired_eth_ratio) > tolerance){
throw new Error(`Expected ratios to be - BTC : ${desired_btc_ratio} ETH : ${desired_eth_ratio},
instead got - BTC : ${newBtcRatio} ETH : ${newEthRatio}`)
}
}
function balancePortfolio(){
return new Promise((resolve, reject) => {
updateRate("BTC", (btc_json) => {
btc_rate = btc_json.rate;
updateRate("ETH", (eth_json) => {
eth_rate = eth_json.rate;
let btc_to_buy = getActualPortfolioValue() * desired_btc_ratio / btc_rate - btc_amount; // - new_btc_ratio * btc_rate; //btc_amount * (btc_ratio - new_btc_ratio);
let eth_to_buy = -btc_to_buy * (btc_rate / eth_rate);
validateExpectedRatios(eth_to_buy, btc_to_buy);
kraken.buyEthSellBtc(eth_to_buy).then(krakenResult => {
console.log(`result of operation at Kraken : ${krakenResult}`);
let result = getPortfolioValues();
result['action'] = `ETH buy/sell amount : ${eth_to_buy}, will result in converted to BTC amount : ${btc_to_buy}`;
resolve(result)
})
});
});
})
}
app.use(express.json());
app.get("/buyEthSellBtc", (req, res) => {
kraken.buyEthSellBtc().then(function (ans) {
res.json(ans);
})
})
app.get("/get-balance", (req, res) => {
kraken.getBalance().then(function (balance) {
var balanceFromKraken = balance['result'];
btc_amount = parseFloat(balanceFromKraken[kraken.BTC_SYMBOL]);
eth_amount = parseFloat(balanceFromKraken[kraken.ETH_SYMBOL]);
res.json(balance)
})
})
app.get("/assets", (req, res) => {
kraken.getAssets().then(function (assets) {
res.json(assets);
})
})
app.get("/status", (req, res) => {
res.json(getPortfolioValues());
})
app.post("/balance", (req, res) => {
balancePortfolio().then(
function whenOk(response) {
res.json(response)
}).catch(function notOk(e) {
console.error(e);
res.statusCode = 400;
res.end(e.message);
})
});
app.post("/init", (req, res) => {
validateValues(req.body);
readApiKeyFromFile().then(
function(data) {
api_key = data;
initializePortfolio().then(
function whenOk(response) {
res.json(response)
}).catch(function notOk(e) {
console.error(e);
res.statusCode = 400;
res.end(e.message);
})
}).then(() => {
kraken.readApiKeyFromFile().then((krakenApiKeyInitResult => { console.log(`kraken API key init result : ${krakenApiKeyInitResult}`)}));
})
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});