-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
symbol-update-last-buy-price.js
138 lines (116 loc) · 3.43 KB
/
symbol-update-last-buy-price.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
const _ = require('lodash');
const { cache, mongo, PubSub } = require('../../../helpers');
const {
getAccountInfo,
saveLastBuyPrice
} = require('../../../cronjob/trailingTradeHelper/common');
const queue = require('../../../cronjob/trailingTradeHelper/queue');
/**
* Delete last buy price
*
* @param {*} logger
* @param {*} ws
* @param {*} symbol
* @returns
*/
const deleteLastBuyPrice = async (logger, ws, symbol) => {
await mongo.deleteOne(logger, 'trailing-trade-symbols', {
key: `${symbol}-last-buy-price`
});
queue.executeFor(logger, symbol, {
correlationId: _.get(logger, 'fields.correlationId', '')
});
PubSub.publish('frontend-notification', {
type: 'success',
title: `The last buy price for ${symbol} has been removed successfully.`
});
ws.send(
JSON.stringify({
result: true,
type: 'symbol-update-result',
message: `The last buy price for ${symbol} has been removed successfully.`
})
);
return true;
};
/**
* Update last buy price
*
* @param {*} logger
* @param {*} ws
* @param {*} symbol
* @param {*} lastBuyPrice
* @returns
*/
const updateLastBuyPrice = async (logger, ws, symbol, lastBuyPrice) => {
// Retrieve symbol info
const cachedSymbolInfo =
JSON.parse(
await cache.hget('trailing-trade-symbols', `${symbol}-symbol-info`)
) || {};
if (_.isEmpty(cachedSymbolInfo) === true) {
PubSub.publish('frontend-notification', {
type: 'error',
title:
`The bot could not retrieve the cached symbol information for ${symbol}.` +
` Wait for the symbol information to be cached and try again.`
});
ws.send(
JSON.stringify({
result: false,
type: 'symbol-update-last-buy-price-result',
message:
`The bot could not retrieve the cached symbol information for ${symbol}.` +
` Wait for the symbol information to be cached and try again.`
})
);
return false;
}
const { baseAsset } = cachedSymbolInfo;
// Retrieve account info from cache
const accountInfo = await getAccountInfo(logger);
// Get asset balances
const baseAssetBalance = accountInfo.balances.filter(
b => b.asset === baseAsset
)[0] || {
asset: baseAsset,
free: 0,
locked: 0,
total: 0,
estimatedValue: 0
};
// Calculate total quantity
const baseAssetTotalBalance =
parseFloat(baseAssetBalance.free) + parseFloat(baseAssetBalance.locked);
await saveLastBuyPrice(logger, symbol, {
lastBuyPrice,
quantity: baseAssetTotalBalance
});
queue.executeFor(logger, symbol, {
correlationId: _.get(logger, 'fields.correlationId', '')
});
PubSub.publish('frontend-notification', {
type: 'success',
title: `The last buy price for ${symbol} has been configured successfully.`
});
ws.send(
JSON.stringify({
result: true,
type: 'symbol-update-result',
message: `The last buy price for ${symbol} has been configured successfully.`
})
);
return true;
};
const handleSymbolUpdateLastBuyPrice = async (logger, ws, payload) => {
logger.info({ payload }, 'Start symbol update');
const { data: symbolInfo } = payload;
// Update last-buy-price
const { symbol } = symbolInfo;
const { lastBuyPrice } = symbolInfo.sell;
if (parseFloat(lastBuyPrice) <= 0) {
return deleteLastBuyPrice(logger, ws, symbol);
}
return updateLastBuyPrice(logger, ws, symbol, lastBuyPrice);
};
module.exports = { handleSymbolUpdateLastBuyPrice };