-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
147 lines (129 loc) · 4.73 KB
/
index.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
const { makeInternalRequest } = require("../helpers/fetch")
const {
onHttps,
getKillSwitch,
getWorkingCapital,
toggleKillSwitch,
setDefaultWorkingCapital,
recordProfitsTaken,
resetTradeCounter,
resetWorkingCapital
} = require("../Firebase");
const { externalHeaderAuthKey, killSwitchHeaderAuthKey, internalHeaderAuthKey, zoidbergGetKillSwitchEndpoint } = require("../constants");
const { log, logNews, logError } = require("../helpers/log");
//
// Kill switch
//
exports.toggleZoidbergKillSwitch = (isProd) => {
return onHttps(async (req, res) => {
let killSwitchAuth = req.get("KillSwitch-Auth");
if (killSwitchAuth && killSwitchAuth === killSwitchHeaderAuthKey) {
let result = await toggleKillSwitch(isProd);
res.json({ killSwitch: result })
} else {
res.sendStatus(404)
}
})
}
exports.getZoidbergKillSwitch = (isProd) => {
return onHttps(async (req, res) => {
let internal = req.get("Internal-Auth");
if (internal && internal === internalHeaderAuthKey) {
let result = await getKillSwitch(isProd);
res.json({ killSwitch: result });
} else {
res.sendStatus(404);
}
});
};
exports.checkZoigbergKillSwitch = async () => {
let result = await makeInternalRequest(zoidbergGetKillSwitchEndpoint);
return result.killSwitch
}
//
// Working capital
//
exports.setNewDefaultWorkingCapital = (isProd) => {
return onHttps(async (req, res) => {
let external = req.get("External-Auth");
if (external && external === externalHeaderAuthKey) {
let newWorkingCapital = Number(req.body.newWorkingCapital);
await setDefaultWorkingCapital(isProd, newWorkingCapital).catch(e => {
logError({ title: "Error in setNewDefaultWorkingCapital", message: e.message, details: e.stack });
res.sendStatus(412);
return
});
// Should also delete `currency-workingCapital` if currently no positions
// Will auto-recreate doc using the new default if `currency-workingCapital` does not exist
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
}
//
// Profits
//
exports.getProfits = (isProd) => {
return onHttps(async (req, res) => {
let external = req.get("External-Auth");
if (external && external === externalHeaderAuthKey) {
let currency = req.body.currency;
let exchange = req.body.exchange;
let pairedAsset = req.body.pairedAsset;
let profits = await _getProfits(isProd, currency, exchange, pairedAsset).catch((e) => {
logError({ title: "Error in getProfits", message: e.message, details: e.stack });
res.sendStatus(412);
return;
});
res.json({ profits });
} else {
res.sendStatus(404);
}
});
}
exports.takeProfits = (isProd) => {
return onHttps(async (req, res) => {
let external = req.get("External-Auth");
if (external && external === externalHeaderAuthKey) {
let currency = req.body.currency;
let exchange = req.body.exchange;
let pairedAsset = req.body.pairedAsset;
await _takeProfits(isProd, currency, exchange, pairedAsset).catch((e) => {
logError({ title: "Error in takeProfits", message: e.message, details: e.stack });
res.sendStatus(412);
return;
});
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
}
//
// Internal
//
async function _getProfits(isProd, currency, exchange, pairedAsset) {
let { currentWC, defaultWC } = await getWorkingCapital(isProd, currency, exchange, pairedAsset);
console.log(currentWC, defaultWC)
let profit = Number(currentWC) - Number(defaultWC)
return profit
}
async function _takeProfits(isProd, currency, exchange, pairedAsset) {
try {
let profits = await _getProfits(isProd, currency, exchange, pairedAsset)
logNews({
title: `🦀 Zoidberg profits to be withdrawn`,
message: `Profits for ${currency}-${pairedAsset} on ${exchange} are: ${profits}`,
details: `🚨 Currently unimplemented, manually withdraw on ${exchange}: ${profits}`,
});
// TODO: - Placeholder for taking profits (requires trade permissions on CB)
// Get payment methods on CB - https://docs.pro.coinbase.com/#payment-methods
// withdraw EUR - https://docs.pro.coinbase.com/#withdrawals
await recordProfitsTaken(isProd, currency, exchange, pairedAsset, profits)
await resetWorkingCapital(isProd, currency, exchange, pairedAsset)
await resetTradeCounter(isProd, currency, exchange, pairedAsset)
} catch (e) {
throw e
}
}