/
UserServiceDryModeAOP.java
187 lines (162 loc) · 7.98 KB
/
UserServiceDryModeAOP.java
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
package tech.cassandre.trading.bot.util.dry;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.dto.account.AccountInfo;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.account.Wallet;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import tech.cassandre.trading.bot.dto.user.AccountDTO;
import tech.cassandre.trading.bot.dto.user.UserDTO;
import tech.cassandre.trading.bot.strategy.CassandreStrategy;
import tech.cassandre.trading.bot.strategy.CassandreStrategyInterface;
import tech.cassandre.trading.bot.strategy.GenericCassandreStrategy;
import tech.cassandre.trading.bot.util.base.service.BaseService;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import static java.math.BigDecimal.ZERO;
/**
* AOP for user service in dry mode.
*/
@Aspect
@Component
@ConditionalOnExpression("${cassandre.trading.bot.exchange.modes.dry:true}")
public class UserServiceDryModeAOP extends BaseService {
/** User file prefix. */
private static final String USER_FILE_PREFIX = "user-";
/** User file suffix. */
private static final String USER_FILE_SUFFIX = ".*sv";
/** User ID. */
private static final String USER_ID = "user";
/** Application context. */
private final ApplicationContext applicationContext;
/** Account information. */
private AccountInfo accountInfo;
/**
* Constructor.
*
* @param newApplicationContext application context
*/
public UserServiceDryModeAOP(final ApplicationContext newApplicationContext) {
applicationContext = newApplicationContext;
Collection<Wallet> wallets = new LinkedHashSet<>();
getFilesToLoad().forEach(file -> {
if (file.getFilename() != null) {
// Account.
final int accountIndexStart = file.getFilename().indexOf(USER_FILE_PREFIX) + USER_FILE_PREFIX.length();
final int accountIndexStop = file.getFilename().indexOf("sv") - 2;
final String account = file.getFilename().substring(accountIndexStart, accountIndexStop);
logger.info("Adding account '" + account + "'");
// Balances.
Collection<Balance> balances = new LinkedHashSet<>();
try (Scanner scanner = new Scanner(file.getFile())) {
while (scanner.hasNextLine()) {
try (Scanner rowScanner = new Scanner(scanner.nextLine())) {
if (file.getFilename().endsWith("tsv")) {
rowScanner.useDelimiter("\t");
} else {
rowScanner.useDelimiter(",");
}
// Data retrieved from file.
final String currency = rowScanner.next().replaceAll("\"", "");
final String amount = rowScanner.next().replaceAll("\"", "");
// Creating balance.
logger.info("- Adding balance " + amount + " " + currency);
balances.add(new Balance(new Currency(currency), new BigDecimal(amount)));
}
}
} catch (FileNotFoundException e) {
logger.error("{} not found !", file.getFilename());
} catch (IOException e) {
logger.error("IOException: " + e);
}
// Creating wallet.
wallets.add(new Wallet(account, account, balances, Collections.emptySet(), ZERO, ZERO));
}
});
// Creates the account info.
accountInfo = new AccountInfo(USER_ID, wallets);
}
@Around("execution(* org.knowm.xchange.service.account.AccountService.getAccountInfo())")
public final AccountInfo getAccountInfo(final ProceedingJoinPoint pjp) {
return accountInfo;
}
/**
* Update balance of trade account (method called by trade service).
*
* @param strategy strategy
* @param currency currency
* @param amount amount
*/
public void addToBalance(final GenericCassandreStrategy strategy, final Currency currency, final BigDecimal amount) {
final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
if (tradeAccount.isEmpty()) {
logger.error("Trading account not found!");
} else {
// We build a new account information from what we saved.
Collection<Wallet> wallets = new LinkedHashSet<>();
// We retreat all the wallets we have.
accountInfo.getWallets()
.forEach((name, wallet) -> {
HashMap<Currency, Balance> balances = new LinkedHashMap<>();
// For each balance, we add it if nothing changed or, if on trading account, and we need to change the amount,
// Then we do it.
wallet.getBalances().forEach((balanceCurrency, balance) -> {
if (name.equals(tradeAccount.get().getName()) && balanceCurrency.equals(currency)) {
// If we are on the account and currency to update, we calculate the new value.
balances.put(balanceCurrency, new Balance(balanceCurrency, balance.getTotal().add(amount)));
} else {
// Else we keep the same value.
balances.put(balanceCurrency, balance);
}
});
// if for the trading account, we don't have a balance for the currency we are trying to add/remove
// amounts, then we create a new balance.
if (name.equals(tradeAccount.get().getName()) && balances.get(currency) == null) {
balances.put(currency, new Balance(currency, amount));
}
// We add the wallet.
wallets.add(new Wallet(name, name, balances.values(), Collections.emptySet(), ZERO, ZERO));
});
// Creates the account info.
accountInfo = new AccountInfo(USER_ID, wallets);
// Updates all strategies.
final UserDTO userDTO = accountMapper.mapToUserDTO(accountInfo);
applicationContext.getBeansWithAnnotation(CassandreStrategy.class)
.values() // We get the list of all required cp of all strategies.
.stream()
.map(o -> (CassandreStrategyInterface) o)
.forEach(cassandreStrategyInterface -> cassandreStrategyInterface.initializeAccounts(userDTO.getAccounts()));
}
}
/**
* Returns the list of files to import.
*
* @return files to import.
*/
private List<Resource> getFilesToLoad() {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
final Resource[] resources = resolver.getResources("classpath*:" + USER_FILE_PREFIX + "*" + USER_FILE_SUFFIX);
return Arrays.asList(resources);
} catch (IOException e) {
logger.error("UserServiceDryModeAOP encountered an error: " + e.getMessage());
}
return Collections.emptyList();
}
}