forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionAwareTrade.java
191 lines (162 loc) · 7.31 KB
/
TransactionAwareTrade.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
/*
* 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.desktop.main.funds.transactions;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.offer.Offer;
import bisq.core.support.dispute.Dispute;
import bisq.core.support.dispute.arbitration.ArbitrationManager;
import bisq.core.support.dispute.refund.RefundManager;
import bisq.core.trade.model.Tradable;
import bisq.core.trade.model.TradeModel;
import bisq.core.trade.model.bisq_v1.Contract;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.common.crypto.PubKeyRing;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutput;
import javafx.collections.ObservableList;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j
class TransactionAwareTrade implements TransactionAwareTradable {
private final TradeModel tradeModel;
private final ArbitrationManager arbitrationManager;
private final RefundManager refundManager;
private final BtcWalletService btcWalletService;
private final PubKeyRing pubKeyRing;
TransactionAwareTrade(TradeModel tradeModel,
ArbitrationManager arbitrationManager,
RefundManager refundManager,
BtcWalletService btcWalletService,
PubKeyRing pubKeyRing) {
this.tradeModel = tradeModel;
this.arbitrationManager = arbitrationManager;
this.refundManager = refundManager;
this.btcWalletService = btcWalletService;
this.pubKeyRing = pubKeyRing;
}
@Override
public boolean isRelatedToTransaction(Transaction transaction) {
Sha256Hash hash = transaction.getTxId();
String txId = hash.toString();
boolean tradeRelated = false;
if (tradeModel instanceof Trade) {
Trade trade = (Trade) tradeModel;
boolean isTakerOfferFeeTx = txId.equals(trade.getTakerFeeTxId());
boolean isOfferFeeTx = isOfferFeeTx(txId);
boolean isDepositTx = isDepositTx(hash);
boolean isPayoutTx = isPayoutTx(hash);
boolean isDisputedPayoutTx = isDisputedPayoutTx(txId);
boolean isDelayedPayoutTx = transaction.getLockTime() != 0 && isDelayedPayoutTx(txId);
boolean isRefundPayoutTx = isRefundPayoutTx(trade, txId);
tradeRelated = isTakerOfferFeeTx ||
isOfferFeeTx ||
isDepositTx ||
isPayoutTx ||
isDisputedPayoutTx ||
isDelayedPayoutTx ||
isRefundPayoutTx;
}
return tradeRelated;
}
private boolean isPayoutTx(Sha256Hash txId) {
Trade trade = (Trade) tradeModel;
return Optional.ofNullable(trade.getPayoutTx())
.map(Transaction::getTxId)
.map(hash -> hash.equals(txId))
.orElse(false);
}
private boolean isDepositTx(Sha256Hash txId) {
Trade trade = (Trade) tradeModel;
return Optional.ofNullable(trade.getDepositTx())
.map(Transaction::getTxId)
.map(hash -> hash.equals(txId))
.orElse(false);
}
private boolean isOfferFeeTx(String txId) {
return Optional.ofNullable(tradeModel.getOffer())
.map(Offer::getOfferFeePaymentTxId)
.map(paymentTxId -> paymentTxId.equals(txId))
.orElse(false);
}
private boolean isDisputedPayoutTx(String txId) {
String delegateId = tradeModel.getId();
ObservableList<Dispute> disputes = arbitrationManager.getDisputesAsObservableList();
boolean isAnyDisputeRelatedToThis = arbitrationManager.getDisputedTradeIds().contains(tradeModel.getId());
return isAnyDisputeRelatedToThis && disputes.stream()
.anyMatch(dispute -> {
String disputePayoutTxId = dispute.getDisputePayoutTxId();
boolean isDisputePayoutTx = txId.equals(disputePayoutTxId);
String disputeTradeId = dispute.getTradeId();
boolean isDisputeRelatedToThis = delegateId.equals(disputeTradeId);
return isDisputePayoutTx && isDisputeRelatedToThis;
});
}
boolean isDelayedPayoutTx(String txId) {
Transaction transaction = btcWalletService.getTransaction(txId);
if (transaction == null)
return false;
if (transaction.getLockTime() == 0)
return false;
if (transaction.getInputs() == null)
return false;
return transaction.getInputs().stream()
.anyMatch(input -> {
TransactionOutput connectedOutput = input.getConnectedOutput();
if (connectedOutput == null) {
return false;
}
Transaction parentTransaction = connectedOutput.getParentTransaction();
if (parentTransaction == null) {
return false;
}
return isDepositTx(parentTransaction.getTxId());
});
}
private boolean isRefundPayoutTx(Trade trade, String txId) {
String tradeId = tradeModel.getId();
ObservableList<Dispute> disputes = refundManager.getDisputesAsObservableList();
boolean isAnyDisputeRelatedToThis = refundManager.getDisputedTradeIds().contains(tradeId);
if (isAnyDisputeRelatedToThis) {
Transaction tx = btcWalletService.getTransaction(txId);
if (tx != null) {
for (TransactionOutput txo : tx.getOutputs()) {
if (btcWalletService.isTransactionOutputMine(txo)) {
try {
Address receiverAddress = txo.getScriptPubKey().getToAddress(btcWalletService.getParams());
Contract contract = checkNotNull(trade.getContract());
String myPayoutAddressString = contract.isMyRoleBuyer(pubKeyRing) ?
contract.getBuyerPayoutAddressString() :
contract.getSellerPayoutAddressString();
if (receiverAddress != null && myPayoutAddressString.equals(receiverAddress.toString())) {
return true;
}
} catch (RuntimeException ignore) {
}
}
}
}
}
return false;
}
@Override
public Tradable asTradable() {
return tradeModel;
}
}