forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TakeOfferModel.java
315 lines (275 loc) · 12.4 KB
/
TakeOfferModel.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
/*
* 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.offer.bisq_v1;
import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.monetary.Price;
import bisq.core.monetary.Volume;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferUtil;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.provider.fee.FeeService;
import bisq.core.provider.price.PriceFeedService;
import bisq.common.taskrunner.Model;
import org.bitcoinj.core.Coin;
import javax.inject.Inject;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import static bisq.core.btc.model.AddressEntry.Context.OFFER_FUNDING;
import static bisq.core.offer.OfferDirection.SELL;
import static bisq.core.util.VolumeUtil.getAdjustedVolumeForHalCash;
import static bisq.core.util.VolumeUtil.getRoundedFiatVolume;
import static bisq.core.util.coin.CoinUtil.minCoin;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.bitcoinj.core.Coin.ZERO;
import static org.bitcoinj.core.Coin.valueOf;
@Slf4j
public class TakeOfferModel implements Model {
// Immutable
private final AccountAgeWitnessService accountAgeWitnessService;
private final BtcWalletService btcWalletService;
private final FeeService feeService;
private final OfferUtil offerUtil;
private final PriceFeedService priceFeedService;
// Mutable
@Getter
private AddressEntry addressEntry;
@Getter
private Coin amount;
@Getter
private boolean isCurrencyForTakerFeeBtc;
private Offer offer;
private PaymentAccount paymentAccount;
@Getter
private Coin securityDeposit;
private boolean useSavingsWallet;
// Use an average of a typical trade fee tx with 1 input, deposit tx and payout tx.
private final int feeTxVsize = 192; // (175+233+169)/3
private Coin txFeePerVbyteFromFeeService;
@Getter
private Coin txFeeFromFeeService;
@Getter
private Coin takerFee;
@Getter
private Coin totalToPayAsCoin;
@Getter
private Coin missingCoin = ZERO;
@Getter
private Coin totalAvailableBalance;
@Getter
private Coin balance;
@Getter
private boolean isBtcWalletFunded;
@Getter
private Volume volume;
@Inject
public TakeOfferModel(AccountAgeWitnessService accountAgeWitnessService,
BtcWalletService btcWalletService,
FeeService feeService,
OfferUtil offerUtil,
PriceFeedService priceFeedService) {
this.accountAgeWitnessService = accountAgeWitnessService;
this.btcWalletService = btcWalletService;
this.feeService = feeService;
this.offerUtil = offerUtil;
this.priceFeedService = priceFeedService;
}
public void initModel(Offer offer,
PaymentAccount paymentAccount,
boolean useSavingsWallet) {
this.clearModel();
this.offer = offer;
this.paymentAccount = paymentAccount;
this.addressEntry = btcWalletService.getOrCreateAddressEntry(offer.getId(), OFFER_FUNDING);
validateModelInputs();
this.useSavingsWallet = useSavingsWallet;
this.amount = valueOf(Math.min(offer.getAmount().value, getMaxTradeLimit()));
this.securityDeposit = offer.getDirection() == SELL
? offer.getBuyerSecurityDeposit()
: offer.getSellerSecurityDeposit();
this.isCurrencyForTakerFeeBtc = offerUtil.isCurrencyForTakerFeeBtc(amount);
this.takerFee = offerUtil.getTakerFee(isCurrencyForTakerFeeBtc, amount);
calculateTxFees();
calculateVolume();
calculateTotalToPay();
offer.resetState();
priceFeedService.setCurrencyCode(offer.getCurrencyCode());
}
@Override
public void onComplete() {
// empty
}
private void calculateTxFees() {
// Taker pays 3 times the tx fee (taker fee, deposit, payout) because the mining
// fee might be different when maker created the offer and reserved his funds.
// Taker creates at least taker fee and deposit tx at nearly the same moment.
// Just the payout will be later and still could lead to issues if the required
// fee changed a lot in the meantime. using RBF and/or multiple batch-signed
// payout tx with different fees might be an option but RBF is not supported yet
// in BitcoinJ and batched txs would add more complexity to the trade protocol.
// A typical trade fee tx has about 175 vbytes (if one input). The trade txs has
// about 169-263 vbytes. We use 192 as a average value.
// Fee calculations:
// Trade fee tx: 175 vbytes (1 input)
// Deposit tx: 233 vbytes (1 MS output+ OP_RETURN) - 263 vbytes
// (1 MS output + OP_RETURN + change in case of smaller trade amount)
// Payout tx: 169 vbytes
// Disputed payout tx: 139 vbytes
txFeePerVbyteFromFeeService = getTxFeePerVbyte();
txFeeFromFeeService = offerUtil.getTxFeeByVsize(txFeePerVbyteFromFeeService, feeTxVsize);
log.info("{} txFeePerVbyte = {}", feeService.getClass().getSimpleName(), txFeePerVbyteFromFeeService);
}
private Coin getTxFeePerVbyte() {
try {
CompletableFuture<Void> feeRequestFuture = CompletableFuture.runAsync(feeService::requestFees);
feeRequestFuture.get(); // Block until async fee request is complete.
return feeService.getTxFeePerVbyte();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Could not request fees from fee service.", e);
}
}
private void calculateTotalToPay() {
// Taker pays 2 times the tx fee because the mining fee might be different when
// maker created the offer and reserved his funds, so that would not work well
// with dynamic fees. The mining fee for the takeOfferFee tx is deducted from
// the createOfferFee and not visible to the trader.
Coin feeAndSecDeposit = getTotalTxFee().add(securityDeposit);
if (isCurrencyForTakerFeeBtc)
feeAndSecDeposit = feeAndSecDeposit.add(takerFee);
totalToPayAsCoin = offer.isBuyOffer()
? feeAndSecDeposit.add(amount)
: feeAndSecDeposit;
updateBalance();
}
private void calculateVolume() {
Price tradePrice = offer.getPrice();
Volume volumeByAmount = Objects.requireNonNull(tradePrice).getVolumeByAmount(amount);
if (offer.getPaymentMethod().getId().equals(PaymentMethod.HAL_CASH_ID))
volumeByAmount = getAdjustedVolumeForHalCash(volumeByAmount);
else if (offer.isFiatOffer())
volumeByAmount = getRoundedFiatVolume(volumeByAmount);
volume = volumeByAmount;
updateBalance();
}
private void updateBalance() {
Coin tradeWalletBalance = btcWalletService.getBalanceForAddress(addressEntry.getAddress());
if (useSavingsWallet) {
Coin savingWalletBalance = btcWalletService.getSavingWalletBalance();
totalAvailableBalance = savingWalletBalance.add(tradeWalletBalance);
if (totalToPayAsCoin != null)
balance = minCoin(totalToPayAsCoin, totalAvailableBalance);
} else {
balance = tradeWalletBalance;
}
missingCoin = offerUtil.getBalanceShortage(totalToPayAsCoin, balance);
isBtcWalletFunded = offerUtil.isBalanceSufficient(totalToPayAsCoin, balance);
}
private long getMaxTradeLimit() {
return accountAgeWitnessService.getMyTradeLimit(paymentAccount,
offer.getCurrencyCode(),
offer.getMirroredDirection());
}
public Coin getTotalTxFee() {
Coin totalTxFees = txFeeFromFeeService.add(getTxFeeForDepositTx()).add(getTxFeeForPayoutTx());
if (isCurrencyForTakerFeeBtc)
return totalTxFees;
else
return totalTxFees.subtract(takerFee);
}
@NotNull
public Coin getFundsNeededForTrade() {
// If taking a buy offer, taker needs to reserve the offer.amt too.
return securityDeposit
.add(getTxFeeForDepositTx())
.add(getTxFeeForPayoutTx())
.add(offer.isBuyOffer() ? amount : ZERO);
}
private Coin getTxFeeForDepositTx() {
// TODO fix with new trade protocol!
// Unfortunately we cannot change that to the correct fees as it would break
// backward compatibility. We still might find a way with offer version or app
// version checks so lets keep that commented out code as that shows how it
// should be.
return txFeeFromFeeService;
}
private Coin getTxFeeForPayoutTx() {
// TODO fix with new trade protocol!
// Unfortunately we cannot change that to the correct fees as it would break
// backward compatibility. We still might find a way with offer version or app
// version checks so lets keep that commented out code as that shows how it
// should be.
return txFeeFromFeeService;
}
private void validateModelInputs() {
checkNotNull(offer, "offer must not be null");
checkNotNull(offer.getAmount(), "offer amount must not be null");
checkArgument(offer.getAmount().value > 0, "offer amount must not be zero");
checkNotNull(offer.getPrice(), "offer price must not be null");
checkNotNull(paymentAccount, "payment account must not be null");
checkNotNull(addressEntry, "address entry must not be null");
}
private void clearModel() {
this.addressEntry = null;
this.amount = null;
this.balance = null;
this.isBtcWalletFunded = false;
this.isCurrencyForTakerFeeBtc = false;
this.missingCoin = ZERO;
this.offer = null;
this.paymentAccount = null;
this.securityDeposit = null;
this.takerFee = null;
this.totalAvailableBalance = null;
this.totalToPayAsCoin = null;
this.txFeeFromFeeService = null;
this.txFeePerVbyteFromFeeService = null;
this.useSavingsWallet = true;
this.volume = null;
}
@Override
public String toString() {
return "TakeOfferModel{" +
" offer.id=" + offer.getId() + "\n" +
" offer.state=" + offer.getState() + "\n" +
", paymentAccount.id=" + paymentAccount.getId() + "\n" +
", paymentAccount.method.id=" + paymentAccount.getPaymentMethod().getId() + "\n" +
", useSavingsWallet=" + useSavingsWallet + "\n" +
", addressEntry=" + addressEntry + "\n" +
", amount=" + amount + "\n" +
", securityDeposit=" + securityDeposit + "\n" +
", feeTxVsize=" + feeTxVsize + "\n" +
", txFeePerVbyteFromFeeService=" + txFeePerVbyteFromFeeService + "\n" +
", txFeeFromFeeService=" + txFeeFromFeeService + "\n" +
", takerFee=" + takerFee + "\n" +
", totalToPayAsCoin=" + totalToPayAsCoin + "\n" +
", missingCoin=" + missingCoin + "\n" +
", totalAvailableBalance=" + totalAvailableBalance + "\n" +
", balance=" + balance + "\n" +
", volume=" + volume + "\n" +
", fundsNeededForTrade=" + getFundsNeededForTrade() + "\n" +
", isCurrencyForTakerFeeBtc=" + isCurrencyForTakerFeeBtc + "\n" +
", isBtcWalletFunded=" + isBtcWalletFunded + "\n" +
'}';
}
}