mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 23:18:17 +01:00
Use getter/setter instead of public fields in PlaceOfferModel
This commit is contained in:
parent
8f06131a58
commit
08affae63a
6 changed files with 50 additions and 55 deletions
|
@ -24,23 +24,27 @@ import io.bisq.core.btc.wallet.TradeWalletService;
|
||||||
import io.bisq.core.offer.Offer;
|
import io.bisq.core.offer.Offer;
|
||||||
import io.bisq.core.offer.OfferBookService;
|
import io.bisq.core.offer.OfferBookService;
|
||||||
import io.bisq.core.user.User;
|
import io.bisq.core.user.User;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
import org.bitcoinj.core.Transaction;
|
import org.bitcoinj.core.Transaction;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Getter
|
||||||
public class PlaceOfferModel implements Model {
|
public class PlaceOfferModel implements Model {
|
||||||
private static final Logger log = LoggerFactory.getLogger(PlaceOfferModel.class);
|
private final Offer offer;
|
||||||
|
private final Coin reservedFundsForOffer;
|
||||||
|
private final boolean useSavingsWallet;
|
||||||
|
private final BtcWalletService walletService;
|
||||||
|
private final TradeWalletService tradeWalletService;
|
||||||
|
private final BsqWalletService bsqWalletService;
|
||||||
|
private final OfferBookService offerBookService;
|
||||||
|
private final User user;
|
||||||
|
|
||||||
public final Offer offer;
|
@Setter
|
||||||
public final Coin reservedFundsForOffer;
|
private boolean offerAddedToOfferBook;
|
||||||
public final boolean useSavingsWallet;
|
@Setter
|
||||||
public final BtcWalletService walletService;
|
|
||||||
public final TradeWalletService tradeWalletService;
|
|
||||||
public final BsqWalletService bsqWalletService;
|
|
||||||
public final OfferBookService offerBookService;
|
|
||||||
public final User user;
|
|
||||||
public boolean offerAddedToOfferBook;
|
|
||||||
private Transaction transaction;
|
private Transaction transaction;
|
||||||
|
|
||||||
public PlaceOfferModel(Offer offer,
|
public PlaceOfferModel(Offer offer,
|
||||||
|
@ -61,21 +65,11 @@ public class PlaceOfferModel implements Model {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTransaction(Transaction transaction) {
|
|
||||||
this.transaction = transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transaction getTransaction() {
|
|
||||||
return transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void persist() {
|
public void persist() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onComplete() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class PlaceOfferProtocol {
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public void placeOffer() {
|
public void placeOffer() {
|
||||||
log.debug("model.offer.id" + model.offer.getId());
|
log.debug("model.offer.id" + model.getOffer().getId());
|
||||||
TaskRunner<PlaceOfferModel> taskRunner = new TaskRunner<>(model,
|
TaskRunner<PlaceOfferModel> taskRunner = new TaskRunner<>(model,
|
||||||
() -> {
|
() -> {
|
||||||
log.debug("sequence at handleRequestTakeOfferMessage completed");
|
log.debug("sequence at handleRequestTakeOfferMessage completed");
|
||||||
|
@ -58,10 +58,10 @@ public class PlaceOfferProtocol {
|
||||||
(errorMessage) -> {
|
(errorMessage) -> {
|
||||||
log.error(errorMessage);
|
log.error(errorMessage);
|
||||||
|
|
||||||
if (model.offerAddedToOfferBook) {
|
if (model.isOfferAddedToOfferBook()) {
|
||||||
model.offerBookService.removeOffer(model.offer.getOfferPayload(),
|
model.getOfferBookService().removeOffer(model.getOffer().getOfferPayload(),
|
||||||
() -> {
|
() -> {
|
||||||
model.offerAddedToOfferBook = false;
|
model.setOfferAddedToOfferBook(false);
|
||||||
log.debug("OfferPayload removed from offer book.");
|
log.debug("OfferPayload removed from offer book.");
|
||||||
},
|
},
|
||||||
log::error);
|
log::error);
|
||||||
|
|
|
@ -36,19 +36,19 @@ public class AddOfferToRemoteOfferBook extends Task<PlaceOfferModel> {
|
||||||
protected void run() {
|
protected void run() {
|
||||||
try {
|
try {
|
||||||
runInterceptHook();
|
runInterceptHook();
|
||||||
model.offerBookService.addOffer(model.offer,
|
model.getOfferBookService().addOffer(model.getOffer(),
|
||||||
() -> {
|
() -> {
|
||||||
model.offerAddedToOfferBook = true;
|
model.setOfferAddedToOfferBook(true);
|
||||||
complete();
|
complete();
|
||||||
},
|
},
|
||||||
errorMessage -> {
|
errorMessage -> {
|
||||||
model.offer.setErrorMessage("Could not add offer to offerbook.\n" +
|
model.getOffer().setErrorMessage("Could not add offer to offerbook.\n" +
|
||||||
"Please check your network connection and try again.");
|
"Please check your network connection and try again.");
|
||||||
|
|
||||||
failed(errorMessage);
|
failed(errorMessage);
|
||||||
});
|
});
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
model.offer.setErrorMessage("An error occurred.\n" +
|
model.getOffer().setErrorMessage("An error occurred.\n" +
|
||||||
"Error message:\n"
|
"Error message:\n"
|
||||||
+ t.getMessage());
|
+ t.getMessage());
|
||||||
|
|
||||||
|
|
|
@ -42,13 +42,13 @@ public class BroadcastMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
protected void run() {
|
protected void run() {
|
||||||
try {
|
try {
|
||||||
runInterceptHook();
|
runInterceptHook();
|
||||||
model.tradeWalletService.broadcastTx(model.getTransaction(), new FutureCallback<Transaction>() {
|
model.getTradeWalletService().broadcastTx(model.getTransaction(), new FutureCallback<Transaction>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Transaction transaction) {
|
public void onSuccess(Transaction transaction) {
|
||||||
log.debug("Broadcast of offer fee payment succeeded: transaction = " + transaction.toString());
|
log.debug("Broadcast of offer fee payment succeeded: transaction = " + transaction.toString());
|
||||||
|
|
||||||
if (model.getTransaction().getHashAsString().equals(transaction.getHashAsString())) {
|
if (model.getTransaction().getHashAsString().equals(transaction.getHashAsString())) {
|
||||||
model.offer.setState(Offer.State.OFFER_FEE_PAID);
|
model.getOffer().setState(Offer.State.OFFER_FEE_PAID);
|
||||||
// No tx malleability happened after broadcast (still not in blockchain)
|
// No tx malleability happened after broadcast (still not in blockchain)
|
||||||
complete();
|
complete();
|
||||||
} else {
|
} else {
|
||||||
|
@ -56,19 +56,19 @@ public class BroadcastMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
// Tx malleability happened after broadcast. We first remove the malleable offer.
|
// Tx malleability happened after broadcast. We first remove the malleable offer.
|
||||||
// Then we publish the changed offer to the P2P network again after setting the new TxId.
|
// Then we publish the changed offer to the P2P network again after setting the new TxId.
|
||||||
// Normally we use a delay for broadcasting to the peers, but at shut down we want to get it fast out
|
// Normally we use a delay for broadcasting to the peers, but at shut down we want to get it fast out
|
||||||
model.offerBookService.removeOffer(model.offer.getOfferPayload(),
|
model.getOfferBookService().removeOffer(model.getOffer().getOfferPayload(),
|
||||||
() -> {
|
() -> {
|
||||||
log.debug("We store now the changed txID to the offer and add that again.");
|
log.debug("We store now the changed txID to the offer and add that again.");
|
||||||
// We store now the changed txID to the offer and add that again.
|
// We store now the changed txID to the offer and add that again.
|
||||||
model.offer.setOfferFeePaymentTxId(transaction.getHashAsString());
|
model.getOffer().setOfferFeePaymentTxId(transaction.getHashAsString());
|
||||||
model.setTransaction(transaction);
|
model.setTransaction(transaction);
|
||||||
model.offerBookService.addOffer(model.offer,
|
model.getOfferBookService().addOffer(model.getOffer(),
|
||||||
BroadcastMakerFeeTx.this::complete,
|
BroadcastMakerFeeTx.this::complete,
|
||||||
errorMessage -> {
|
errorMessage -> {
|
||||||
log.error("addOffer failed");
|
log.error("addOffer failed");
|
||||||
addOfferFailed = true;
|
addOfferFailed = true;
|
||||||
updateStateOnFault();
|
updateStateOnFault();
|
||||||
model.offer.setErrorMessage("An error occurred when adding the offer to the P2P network.\n" +
|
model.getOffer().setErrorMessage("An error occurred when adding the offer to the P2P network.\n" +
|
||||||
"Error message:\n"
|
"Error message:\n"
|
||||||
+ errorMessage);
|
+ errorMessage);
|
||||||
failed(errorMessage);
|
failed(errorMessage);
|
||||||
|
@ -78,7 +78,7 @@ public class BroadcastMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
log.error("removeOffer failed");
|
log.error("removeOffer failed");
|
||||||
removeOfferFailed = true;
|
removeOfferFailed = true;
|
||||||
updateStateOnFault();
|
updateStateOnFault();
|
||||||
model.offer.setErrorMessage("An error occurred when removing the offer from the P2P network.\n" +
|
model.getOffer().setErrorMessage("An error occurred when removing the offer from the P2P network.\n" +
|
||||||
"Error message:\n"
|
"Error message:\n"
|
||||||
+ errorMessage);
|
+ errorMessage);
|
||||||
failed(errorMessage);
|
failed(errorMessage);
|
||||||
|
@ -89,14 +89,14 @@ public class BroadcastMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NotNull Throwable t) {
|
public void onFailure(@NotNull Throwable t) {
|
||||||
updateStateOnFault();
|
updateStateOnFault();
|
||||||
model.offer.setErrorMessage("An error occurred.\n" +
|
model.getOffer().setErrorMessage("An error occurred.\n" +
|
||||||
"Error message:\n"
|
"Error message:\n"
|
||||||
+ t.getMessage());
|
+ t.getMessage());
|
||||||
failed(t);
|
failed(t);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
model.offer.setErrorMessage("An error occurred.\n" +
|
model.getOffer().setErrorMessage("An error occurred.\n" +
|
||||||
"Error message:\n"
|
"Error message:\n"
|
||||||
+ t.getMessage());
|
+ t.getMessage());
|
||||||
failed(t);
|
failed(t);
|
||||||
|
@ -106,7 +106,7 @@ public class BroadcastMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
private void updateStateOnFault() {
|
private void updateStateOnFault() {
|
||||||
if (!removeOfferFailed && !addOfferFailed) {
|
if (!removeOfferFailed && !addOfferFailed) {
|
||||||
// If broadcast fails we need to remove offer from offerbook
|
// If broadcast fails we need to remove offer from offerbook
|
||||||
model.offerBookService.removeOffer(model.offer.getOfferPayload(),
|
model.getOfferBookService().removeOffer(model.getOffer().getOfferPayload(),
|
||||||
() -> log.debug("OfferPayload removed from offerbook because broadcast failed."),
|
() -> log.debug("OfferPayload removed from offerbook because broadcast failed."),
|
||||||
errorMessage -> log.error("removeOffer failed. " + errorMessage));
|
errorMessage -> log.error("removeOffer failed. " + errorMessage));
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,29 +50,30 @@ public class CreateMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
Offer offer = model.offer;
|
Offer offer = model.getOffer();
|
||||||
try {
|
try {
|
||||||
runInterceptHook();
|
runInterceptHook();
|
||||||
|
|
||||||
NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(model.user.getAcceptedArbitratorAddresses(),
|
NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(model.getUser().getAcceptedArbitratorAddresses(),
|
||||||
model.offer);
|
model.getOffer());
|
||||||
log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
|
log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
|
||||||
Arbitrator selectedArbitrator = model.user.getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
|
Arbitrator selectedArbitrator = model.getUser().getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
|
||||||
checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
|
checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
|
||||||
BtcWalletService walletService = model.walletService;
|
BtcWalletService walletService = model.getWalletService();
|
||||||
String id = offer.getId();
|
String id = offer.getId();
|
||||||
Address fundingAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING).getAddress();
|
Address fundingAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING).getAddress();
|
||||||
Address reservedForTradeAddress = walletService.getOrCreateAddressEntry(id,
|
Address reservedForTradeAddress = walletService.getOrCreateAddressEntry(id,
|
||||||
AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
|
AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
|
||||||
Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
|
Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
|
||||||
|
|
||||||
|
final TradeWalletService tradeWalletService1 = model.getTradeWalletService();
|
||||||
if (offer.isCurrencyForMakerFeeBtc()) {
|
if (offer.isCurrencyForMakerFeeBtc()) {
|
||||||
Transaction btcTransaction = model.tradeWalletService.createBtcTradingFeeTx(
|
Transaction btcTransaction = tradeWalletService1.createBtcTradingFeeTx(
|
||||||
fundingAddress,
|
fundingAddress,
|
||||||
reservedForTradeAddress,
|
reservedForTradeAddress,
|
||||||
changeAddress,
|
changeAddress,
|
||||||
model.reservedFundsForOffer.subtract(offer.getMakerFee()),
|
model.getReservedFundsForOffer().subtract(offer.getMakerFee()),
|
||||||
model.useSavingsWallet,
|
model.isUseSavingsWallet(),
|
||||||
offer.getMakerFee(),
|
offer.getMakerFee(),
|
||||||
offer.getTxFee(),
|
offer.getTxFee(),
|
||||||
selectedArbitrator.getBtcAddress());
|
selectedArbitrator.getBtcAddress());
|
||||||
|
@ -85,18 +86,18 @@ public class CreateMakerFeeTx extends Task<PlaceOfferModel> {
|
||||||
|
|
||||||
complete();
|
complete();
|
||||||
} else {
|
} else {
|
||||||
final BsqWalletService bsqWalletService = model.bsqWalletService;
|
final BsqWalletService bsqWalletService = model.getBsqWalletService();
|
||||||
final TradeWalletService tradeWalletService = model.tradeWalletService;
|
final TradeWalletService tradeWalletService = tradeWalletService1;
|
||||||
Transaction preparedBurnFeeTx = model.bsqWalletService.getPreparedBurnFeeTx(offer.getMakerFee());
|
Transaction preparedBurnFeeTx = model.getBsqWalletService().getPreparedBurnFeeTx(offer.getMakerFee());
|
||||||
Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx,
|
Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx,
|
||||||
fundingAddress,
|
fundingAddress,
|
||||||
reservedForTradeAddress,
|
reservedForTradeAddress,
|
||||||
changeAddress,
|
changeAddress,
|
||||||
model.reservedFundsForOffer,
|
model.getReservedFundsForOffer(),
|
||||||
model.useSavingsWallet,
|
model.isUseSavingsWallet(),
|
||||||
offer.getTxFee());
|
offer.getTxFee());
|
||||||
|
|
||||||
Transaction signedTx = model.bsqWalletService.signTx(txWithBsqFee);
|
Transaction signedTx = model.getBsqWalletService().signTx(txWithBsqFee);
|
||||||
WalletService.checkAllScriptSignaturesForTx(signedTx);
|
WalletService.checkAllScriptSignaturesForTx(signedTx);
|
||||||
bsqWalletService.commitTx(txWithBsqFee);
|
bsqWalletService.commitTx(txWithBsqFee);
|
||||||
// We need to create another instance, otherwise the tx would trigger an invalid state exception
|
// We need to create another instance, otherwise the tx would trigger an invalid state exception
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class ValidateOffer extends Task<PlaceOfferModel> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
Offer offer = model.offer;
|
Offer offer = model.getOffer();
|
||||||
try {
|
try {
|
||||||
runInterceptHook();
|
runInterceptHook();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue