Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Payment account creation and removal from UI to core #3586

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/src/main/java/bisq/core/exceptions/NotFoundException.java
@@ -0,0 +1,24 @@
/*
* 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.exceptions;

public class NotFoundException extends RuntimeException {
blabno marked this conversation as resolved.
Show resolved Hide resolved
public NotFoundException(String message) {
super(message);
}
}
39 changes: 39 additions & 0 deletions core/src/main/java/bisq/core/exceptions/ValidationException.java
@@ -0,0 +1,39 @@
/*
* 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.exceptions;

/**
* Copy of ValidationException from javax.validation:validation-api
*/
public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}

public ValidationException() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only first one constructor with String message as param is used anywhere in application.

super();
}

public ValidationException(String message, Throwable cause) {
super(message, cause);
}

public ValidationException(Throwable cause) {
super(cause);
}
}
136 changes: 136 additions & 0 deletions core/src/main/java/bisq/core/payment/PaymentAccountManager.java
@@ -0,0 +1,136 @@
/*
* 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.payment;

import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.exceptions.NotFoundException;
import bisq.core.exceptions.ValidationException;
import bisq.core.locale.CryptoCurrency;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.FiatCurrency;
import bisq.core.locale.TradeCurrency;
import bisq.core.offer.Offer;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.validation.AltCoinAddressValidator;
import bisq.core.trade.TradeManager;
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.validation.InputValidator;

import com.google.inject.Inject;

import java.util.List;

import static java.lang.String.format;

public class PaymentAccountManager {

private final AccountAgeWitnessService accountAgeWitnessService;
private final OpenOfferManager openOfferManager;
private final Preferences preferences;
private final TradeManager tradeManager;
private final User user;
private AltCoinAddressValidator altCoinAddressValidator;

@Inject
public PaymentAccountManager(AccountAgeWitnessService accountAgeWitnessService,
AltCoinAddressValidator altCoinAddressValidator,
OpenOfferManager openOfferManager,
Preferences preferences,
TradeManager tradeManager,
User user) {
this.accountAgeWitnessService = accountAgeWitnessService;
this.altCoinAddressValidator = altCoinAddressValidator;
this.openOfferManager = openOfferManager;
this.preferences = preferences;
this.tradeManager = tradeManager;
this.user = user;
}

public PaymentAccount addPaymentAccount(PaymentAccount paymentAccount) {
if (paymentAccount instanceof CryptoCurrencyAccount) {
CryptoCurrencyAccount cryptoCurrencyAccount = (CryptoCurrencyAccount) paymentAccount;
TradeCurrency tradeCurrency = cryptoCurrencyAccount.getSingleTradeCurrency();
if (tradeCurrency == null) {
throw new ValidationException("CryptoCurrency account must have exactly one trade currency");
blabno marked this conversation as resolved.
Show resolved Hide resolved
}
altCoinAddressValidator.setCurrencyCode(tradeCurrency.getCode());
InputValidator.ValidationResult validationResult = altCoinAddressValidator.validate(cryptoCurrencyAccount.getAddress());
if (!validationResult.isValid) {
throw new ValidationException(validationResult.errorMessage);
}
}

TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency();
List<TradeCurrency> tradeCurrencies = paymentAccount.getTradeCurrencies();
if (singleTradeCurrency != null) {
if (singleTradeCurrency instanceof FiatCurrency)
preferences.addFiatCurrency((FiatCurrency) singleTradeCurrency);
else
preferences.addCryptoCurrency((CryptoCurrency) singleTradeCurrency);
} else if (!tradeCurrencies.isEmpty()) {
tradeCurrencies.forEach(tradeCurrency -> {
if (tradeCurrency instanceof FiatCurrency)
preferences.addFiatCurrency((FiatCurrency) tradeCurrency);
else
preferences.addCryptoCurrency((CryptoCurrency) tradeCurrency);
});
}

user.addPaymentAccount(paymentAccount);

if (!(paymentAccount instanceof CryptoCurrencyAccount)) {
if (singleTradeCurrency == null && !tradeCurrencies.isEmpty()) {
if (tradeCurrencies.contains(CurrencyUtil.getDefaultTradeCurrency()))
paymentAccount.setSelectedTradeCurrency(CurrencyUtil.getDefaultTradeCurrency());
else
paymentAccount.setSelectedTradeCurrency(tradeCurrencies.get(0));

}
accountAgeWitnessService.publishMyAccountAgeWitness(paymentAccount.getPaymentAccountPayload());
}
return paymentAccount;
}

public boolean removePaymentAccount(PaymentAccount paymentAccount) {
String id = paymentAccount.getId();
boolean isPaymentAccountUsed = openOfferManager.getObservableList().stream()
.anyMatch(openOffer -> id.equals(openOffer.getOffer().getMakerPaymentAccountId()));
if (isPaymentAccountUsed) {
return false;
}
isPaymentAccountUsed = tradeManager.getTradableList().stream()
.anyMatch(trade -> {
Offer offer = trade.getOffer();
return null != offer && id.equals(offer.getMakerPaymentAccountId()) || id.equals(trade.getTakerPaymentAccountId());
});
if (isPaymentAccountUsed) {
return false;
}
user.removePaymentAccount(paymentAccount);
return true;
}

public boolean removePaymentAccount(String id) {
PaymentAccount paymentAccount = user.getPaymentAccount(id);
if (paymentAccount == null) {
throw new NotFoundException(format("Payment account %s not found", id));
}
return removePaymentAccount(paymentAccount);
}
}
Expand Up @@ -22,6 +22,10 @@
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;

import bisq.asset.AddressValidationResult;
import bisq.asset.Asset;
import bisq.asset.AssetRegistry;

import bisq.common.app.DevEnv;

import com.google.inject.Inject;
Expand All @@ -30,12 +34,6 @@

import lombok.extern.slf4j.Slf4j;



import bisq.asset.AddressValidationResult;
import bisq.asset.Asset;
import bisq.asset.AssetRegistry;

@Slf4j
public final class AltCoinAddressValidator extends InputValidator {

Expand Down