From 23bfa2e7fc7948114e19dccbe41b307ee70a50d0 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 21 Nov 2020 15:22:15 -0300 Subject: [PATCH] Refactor didReadCountryField to set country on MoneyGram acct MoneyGram is not a CountryBasedPaymentAccount, but it does have a country field. --- .../api/model/PaymentAccountTypeAdapter.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/bisq/core/api/model/PaymentAccountTypeAdapter.java b/core/src/main/java/bisq/core/api/model/PaymentAccountTypeAdapter.java index e60d5c722a..13646d3f0d 100644 --- a/core/src/main/java/bisq/core/api/model/PaymentAccountTypeAdapter.java +++ b/core/src/main/java/bisq/core/api/model/PaymentAccountTypeAdapter.java @@ -21,6 +21,7 @@ package bisq.core.api.model; import bisq.core.locale.Country; import bisq.core.locale.FiatCurrency; import bisq.core.payment.CountryBasedPaymentAccount; +import bisq.core.payment.MoneyGramAccount; import bisq.core.payment.PaymentAccount; import bisq.core.payment.payload.PaymentAccountPayload; @@ -285,22 +286,31 @@ class PaymentAccountTypeAdapter extends TypeAdapter { } private boolean didReadCountryField(JsonReader in, PaymentAccount account, String fieldName) { - if (account.isCountryBasedPaymentAccount() && fieldName.equals("country")) { - // Read the country code, and use it to set the account's country and single - // trade currency fields. - String countryCode = nextStringOrNull(in); - Optional country = findCountryByCode(countryCode); - if (country.isPresent()) { + if (!fieldName.equals("country")) + return false; + + String countryCode = nextStringOrNull(in); + Optional country = findCountryByCode(countryCode); + if (country.isPresent()) { + + if (account.isCountryBasedPaymentAccount()) { ((CountryBasedPaymentAccount) account).setCountry(country.get()); FiatCurrency fiatCurrency = getCurrencyByCountryCode(checkNotNull(countryCode)); account.setSingleTradeCurrency(fiatCurrency); - return true; + } else if (account.isMoneyGramAccount()) { + ((MoneyGramAccount) account).setCountry(country.get()); } else { - throw new IllegalArgumentException( - format("'%s' is an invalid country code.", countryCode)); + String errMsg = format("cannot set the country on a %s", + paymentAccountType.getSimpleName()); + log.error(StringUtils.capitalize(errMsg) + "."); + throw new IllegalStateException("programmer error: " + errMsg); } + + return true; + } else { - return false; + throw new IllegalArgumentException( + format("'%s' is an invalid country code.", countryCode)); } } @@ -315,7 +325,6 @@ class PaymentAccountTypeAdapter extends TypeAdapter { paymentAccountType.getSimpleName()); log.error(StringUtils.capitalize(errMsg) + ".", ex); throw new IllegalStateException("programmer error: " + errMsg); - } }