forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreTradesService.java
423 lines (368 loc) · 17.4 KB
/
CoreTradesService.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.api;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferUtil;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.bisq_v1.TakeOfferModel;
import bisq.core.offer.bsq_swap.BsqSwapTakeOfferModel;
import bisq.core.trade.ClosedTradableFormatter;
import bisq.core.trade.ClosedTradableManager;
import bisq.core.trade.TradeManager;
import bisq.core.trade.bisq_v1.FailedTradesManager;
import bisq.core.trade.bisq_v1.TradeResultHandler;
import bisq.core.trade.bisq_v1.TradeUtil;
import bisq.core.trade.bsq_swap.BsqSwapTradeManager;
import bisq.core.trade.model.Tradable;
import bisq.core.trade.model.TradeModel;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;
import bisq.core.trade.protocol.bisq_v1.BuyerProtocol;
import bisq.core.trade.protocol.bisq_v1.SellerProtocol;
import bisq.core.user.User;
import bisq.core.util.validation.BtcAddressValidator;
import bisq.common.handlers.ErrorMessageHandler;
import bisq.proto.grpc.GetTradesRequest;
import org.bitcoinj.core.Coin;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import static bisq.core.btc.model.AddressEntry.Context.TRADE_PAYOUT;
import static bisq.proto.grpc.GetTradesRequest.Category.CLOSED;
import static java.lang.String.format;
@Singleton
@Slf4j
class CoreTradesService {
private final CoreContext coreContext;
// Dependencies on core api services in this package must be kept to an absolute
// minimum, but some trading functions require an unlocked wallet's key, so an
// exception is made in this case.
private final CoreWalletsService coreWalletsService;
private final BtcWalletService btcWalletService;
private final OfferUtil offerUtil;
private final BsqSwapTradeManager bsqSwapTradeManager;
private final ClosedTradableManager closedTradableManager;
private final ClosedTradableFormatter closedTradableFormatter;
private final FailedTradesManager failedTradesManager;
private final TakeOfferModel takeOfferModel;
private final BsqSwapTakeOfferModel bsqSwapTakeOfferModel;
private final TradeManager tradeManager;
private final TradeUtil tradeUtil;
private final User user;
@Inject
public CoreTradesService(CoreContext coreContext,
CoreWalletsService coreWalletsService,
BtcWalletService btcWalletService,
OfferUtil offerUtil,
BsqSwapTradeManager bsqSwapTradeManager,
ClosedTradableManager closedTradableManager,
ClosedTradableFormatter closedTradableFormatter,
FailedTradesManager failedTradesManager,
TakeOfferModel takeOfferModel,
BsqSwapTakeOfferModel bsqSwapTakeOfferModel,
TradeManager tradeManager,
TradeUtil tradeUtil,
User user) {
this.coreContext = coreContext;
this.coreWalletsService = coreWalletsService;
this.btcWalletService = btcWalletService;
this.offerUtil = offerUtil;
this.bsqSwapTradeManager = bsqSwapTradeManager;
this.closedTradableManager = closedTradableManager;
this.closedTradableFormatter = closedTradableFormatter;
this.failedTradesManager = failedTradesManager;
this.takeOfferModel = takeOfferModel;
this.bsqSwapTakeOfferModel = bsqSwapTakeOfferModel;
this.tradeManager = tradeManager;
this.tradeUtil = tradeUtil;
this.user = user;
}
// TODO We need to pass the intended trade amount, not default to the maximum.
void takeBsqSwapOffer(Offer offer,
TradeResultHandler<BsqSwapTrade> tradeResultHandler,
ErrorMessageHandler errorMessageHandler) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
bsqSwapTakeOfferModel.initWithData(offer);
bsqSwapTakeOfferModel.applyAmount(offer.getAmount());
log.info("Initiating take {} offer, {}",
offer.isBuyOffer() ? "buy" : "sell",
bsqSwapTakeOfferModel);
bsqSwapTakeOfferModel.onTakeOffer(tradeResultHandler,
log::warn,
errorMessageHandler,
coreContext.isApiUser());
}
// TODO We need to pass the intended trade amount, not default to the maximum.
void takeOffer(Offer offer,
String paymentAccountId,
String takerFeeCurrencyCode,
Consumer<Trade> resultHandler,
ErrorMessageHandler errorMessageHandler) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
offerUtil.maybeSetFeePaymentCurrencyPreference(takerFeeCurrencyCode);
var paymentAccount = user.getPaymentAccount(paymentAccountId);
if (paymentAccount == null)
throw new IllegalArgumentException(format("payment account with id '%s' not found", paymentAccountId));
var useSavingsWallet = true;
takeOfferModel.initModel(offer, paymentAccount, useSavingsWallet);
log.info("Initiating take {} offer, {}",
offer.isBuyOffer() ? "buy" : "sell",
takeOfferModel);
//noinspection ConstantConditions
tradeManager.onTakeOffer(offer.getAmount(),
takeOfferModel.getTxFeeFromFeeService(),
takeOfferModel.getTakerFee(),
takeOfferModel.isCurrencyForTakerFeeBtc(),
offer.getPrice().getValue(),
takeOfferModel.getFundsNeededForTrade(),
offer,
paymentAccountId,
useSavingsWallet,
coreContext.isApiUser(),
resultHandler::accept,
errorMessageHandler
);
}
void confirmPaymentStarted(String tradeId) {
var trade = getTrade(tradeId);
if (isFollowingBuyerProtocol(trade)) {
var tradeProtocol = tradeManager.getTradeProtocol(trade);
((BuyerProtocol) tradeProtocol).onPaymentStarted(
() -> {
},
errorMessage -> {
throw new IllegalStateException(errorMessage);
}
);
} else {
throw new IllegalStateException("you are the seller and not sending payment");
}
}
void confirmPaymentReceived(String tradeId) {
var trade = getTrade(tradeId);
if (isFollowingBuyerProtocol(trade)) {
throw new IllegalStateException("you are the buyer, and not receiving payment");
} else {
var tradeProtocol = tradeManager.getTradeProtocol(trade);
((SellerProtocol) tradeProtocol).onPaymentReceived(
() -> {
},
errorMessage -> {
throw new IllegalStateException(errorMessage);
}
);
}
}
void closeTrade(String tradeId) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
verifyTradeIsNotClosed(tradeId);
var trade = getOpenTrade(tradeId).orElseThrow(() ->
new IllegalArgumentException(format("trade with id '%s' not found", tradeId)));
log.info("Closing trade {}", tradeId);
tradeManager.onTradeCompleted(trade);
}
void withdrawFunds(String tradeId, String toAddress, String memo) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
verifyTradeIsNotClosed(tradeId);
var trade = getOpenTrade(tradeId).orElseThrow(() ->
new IllegalArgumentException(format("trade with id '%s' not found", tradeId)));
verifyIsValidBTCAddress(toAddress);
var fromAddressEntry = btcWalletService.getOrCreateAddressEntry(trade.getId(), TRADE_PAYOUT);
verifyFundsNotWithdrawn(fromAddressEntry);
var amount = trade.getPayoutAmount();
var fee = getEstimatedTxFee(fromAddressEntry.getAddressString(), toAddress, amount);
var receiverAmount = amount.subtract(fee);
log.info(format("Withdrawing funds received from trade %s:"
+ "%n From %s%n To %s%n Amt %s%n Tx Fee %s%n Receiver Amt %s%n Memo %s%n",
tradeId,
fromAddressEntry.getAddressString(),
toAddress,
amount.toFriendlyString(),
fee.toFriendlyString(),
receiverAmount.toFriendlyString(),
memo));
tradeManager.onWithdrawRequest(
toAddress,
amount,
fee,
coreWalletsService.getKey(),
trade,
memo.isEmpty() ? null : memo,
() -> {
},
(errorMessage, throwable) -> {
log.error(errorMessage, throwable);
throw new IllegalStateException(errorMessage, throwable);
});
}
TradeModel getTradeModel(String tradeId) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
Optional<Trade> openTrade = getOpenTrade(tradeId);
if (openTrade.isPresent())
return openTrade.get();
Optional<Trade> closedTrade = getClosedTrade(tradeId);
if (closedTrade.isPresent())
return closedTrade.get();
return tradeManager.findBsqSwapTradeById(tradeId).orElseThrow(() ->
new IllegalArgumentException(format("trade with id '%s' not found", tradeId)));
}
String getTradeRole(TradeModel tradeModel) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
var isBsqSwapTrade = tradeModel instanceof BsqSwapTrade;
try {
return isBsqSwapTrade
? tradeUtil.getRole((BsqSwapTrade) tradeModel)
: tradeUtil.getRole((Trade) tradeModel);
} catch (Exception ex) {
log.error("Role not found for trade with Id {}.", tradeModel.getId(), ex);
return "Not Available";
}
}
Trade getTrade(String tradeId) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
return getOpenTrade(tradeId).orElseGet(() ->
getClosedTrade(tradeId).orElseThrow(() ->
new IllegalArgumentException(format("trade with id '%s' not found", tradeId))
));
}
List<TradeModel> getOpenTrades() {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
return tradeManager.getTrades().stream().collect(Collectors.toList());
}
List<TradeModel> getTradeHistory(GetTradesRequest.Category category) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
if (category.equals(CLOSED)) {
var closedTrades = closedTradableManager.getClosedTrades().stream()
.map(t -> (TradeModel) t)
.collect(Collectors.toList());
closedTrades.addAll(bsqSwapTradeManager.getBsqSwapTrades());
return closedTrades;
} else {
var failedV1Trades = failedTradesManager.getTrades();
return failedV1Trades.stream().collect(Collectors.toList());
}
}
void failTrade(String tradeId) {
// TODO Recommend API users call this method with extra care because
// the API lacks methods for diagnosing trade problems, and does not support
// interaction with mediators. Users may accidentally fail valid trades,
// although they can easily be un-failed with the 'unfailtrade' method.
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
var trade = getTrade(tradeId);
tradeManager.onMoveInvalidTradeToFailedTrades(trade);
log.info("Trade {} changed to failed trade.", tradeId);
}
void unFailTrade(String tradeId) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
failedTradesManager.getTradeById(tradeId).ifPresentOrElse(failedTrade -> {
verifyCanUnfailTrade(failedTrade);
failedTradesManager.removeTrade(failedTrade);
tradeManager.addFailedTradeToPendingTrades(failedTrade);
log.info("Failed trade {} changed to open trade.", tradeId);
}, () -> {
throw new IllegalArgumentException(format("failed trade '%s' not found", tradeId));
});
}
List<OpenOffer> getCanceledOpenOffers() {
return closedTradableManager.getCanceledOpenOffers();
}
String getClosedTradeStateAsString(Tradable tradable) {
return closedTradableFormatter.getStateAsString(tradable);
}
private Optional<Trade> getOpenTrade(String tradeId) {
return tradeManager.getTradeById(tradeId);
}
private Optional<Trade> getClosedTrade(String tradeId) {
Optional<Tradable> tradable = closedTradableManager.getTradableById(tradeId);
return tradable.filter((t) -> t instanceof Trade).map(value -> (Trade) value);
}
private boolean isFollowingBuyerProtocol(Trade trade) {
return tradeManager.getTradeProtocol(trade) instanceof BuyerProtocol;
}
private Coin getEstimatedTxFee(String fromAddress, String toAddress, Coin amount) {
// TODO This and identical logic should be refactored into TradeUtil.
try {
return btcWalletService.getFeeEstimationTransaction(fromAddress,
toAddress,
amount,
TRADE_PAYOUT).getFee();
} catch (Exception ex) {
log.error("", ex);
throw new IllegalStateException(format("could not estimate tx fee: %s", ex.getMessage()));
}
}
// Throws a RuntimeException trade is already closed.
private void verifyTradeIsNotClosed(String tradeId) {
if (getClosedTrade(tradeId).isPresent())
throw new IllegalArgumentException(format("trade '%s' is already closed", tradeId));
}
// Throws a RuntimeException if address is not valid.
private void verifyIsValidBTCAddress(String address) {
try {
new BtcAddressValidator().validate(address);
} catch (Throwable t) {
log.error("", t);
throw new IllegalArgumentException(format("'%s' is not a valid btc address", address));
}
}
// Throws a RuntimeException if address has a zero balance.
private void verifyFundsNotWithdrawn(AddressEntry fromAddressEntry) {
Coin fromAddressBalance = btcWalletService.getBalanceForAddress(fromAddressEntry.getAddress());
if (fromAddressBalance.isZero())
throw new IllegalStateException(format("funds already withdrawn from address '%s'",
fromAddressEntry.getAddressString()));
}
// Throws a RuntimeException if failed trade cannot be changed to OPEN for any reason.
private void verifyCanUnfailTrade(Trade failedTrade) {
if (tradeUtil.getTradeAddresses(failedTrade) == null)
throw new IllegalStateException(
format("cannot change failed trade to open because no trade addresses found for '%s'",
failedTrade.getId()));
if (!failedTradesManager.hasDepositTx(failedTrade))
throw new IllegalStateException(
format("cannot change failed trade to open, no deposit tx found for '%s'",
failedTrade.getId()));
if (!failedTradesManager.hasDelayedPayoutTxBytes(failedTrade))
throw new IllegalStateException(
format("cannot change failed trade to open, no delayed payout tx found for '%s'",
failedTrade.getId()));
failedTradesManager.getBlockingTradeIds(failedTrade).ifPresent(tradeIds -> {
throw new IllegalStateException(
format("cannot change failed trade '%s' to open at this time,"
+ "%ntry again after completing trade(s):%n\t%s",
failedTrade.getId(),
String.join(", ", tradeIds)));
});
}
}