Skip to content

Commit

Permalink
revamp entire code to a better already existant version
Browse files Browse the repository at this point in the history
  • Loading branch information
U65535F committed Feb 28, 2025
1 parent c0eb41a commit 99edd2b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
20 changes: 17 additions & 3 deletions core/src/main/java/haveno/core/payment/PaymentAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import haveno.common.proto.ProtoUtil;
import haveno.common.proto.persistable.PersistablePayload;
import haveno.common.util.Utilities;
Expand Down Expand Up @@ -71,6 +72,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -341,9 +343,21 @@ public void revertChanges() {

public String toJson() {
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(PaymentAccount.class, new PaymentAccountTypeAdapter(this.getClass()))
.create();
return gson.toJson(this);
.registerTypeAdapter(PaymentAccountPayload.class, new PaymentAccountTypeAdapter(this.getClass()))
.create();

Map<String, Object> jsonMap = new HashMap<>();
if (paymentAccountPayload != null) {
String payloadJson = gson.toJson(paymentAccountPayload);
Map<String, Object> payloadMap = gson.fromJson(payloadJson, new TypeToken<Map<String, Object>>(){}.getType());
jsonMap.putAll(payloadMap);
}

jsonMap.put("accountName", getAccountName());
jsonMap.put("accountId", getId());
if (paymentAccountPayload != null) jsonMap.put("salt", getSaltAsHex());

return gson.toJson(jsonMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static haveno.common.util.ReflectionUtils.getSetterMethodForFieldInClassHierarchy;
import static haveno.common.util.ReflectionUtils.getVisibilityModifierAsString;
Expand Down Expand Up @@ -103,12 +102,6 @@ public void write(JsonWriter out, PaymentAccount account) throws IOException {
// We're not serializing a real payment account instance here.
out.beginObject();

// Write extra fields
out.name("accountName");
out.value(account.getAccountName());
out.name("accountId");
out.value(account.getId());

writeComments(out, account);

out.name("paymentMethodId");
Expand All @@ -119,8 +112,7 @@ public void write(JsonWriter out, PaymentAccount account) throws IOException {

// The last field in all json forms is the empty, editable salt field.
out.name("salt");
out.value(account.getSaltAsHex());

out.value("");

out.endObject();
}
Expand All @@ -143,18 +135,19 @@ private void writeComments(JsonWriter out, PaymentAccount account) throws IOExce
}


private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) throws IOException {
private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) {
if (account instanceof CountryBasedPaymentAccount) {
writeAcceptedCountryCodesField(out, account);
}

if (account.hasMultipleCurrencies()) {
writeTradeCurrenciesField(out, account);
writeSelectedTradeCurrencyField(out, account);
}

fieldSettersMap.forEach((field, value) -> {
try {
// Write out a json element if there is a @Setter for this field.
if (value.isPresent()) {
log.debug("Append form with settable field: {} {} {} setter: {}",
getVisibilityModifierAsString(field),
Expand All @@ -163,13 +156,14 @@ private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) thr
value);
String fieldName = field.getName();
out.name(fieldName);
if (fieldName.equals("country"))
out.value("your two letter country code");
else
out.value("your " + fieldName.toLowerCase());
if (fieldName.equals("country")) out.value("your two letter country code");
else out.value("your " + fieldName.toLowerCase());
}
} catch (IOException ex) {
throw new RuntimeException("Error writing field " + field.getName(), ex);
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
});
}
Expand All @@ -178,16 +172,27 @@ private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) thr
// field in the json form, though the 'tradeCurrencies' field has no setter method in
// the PaymentAccount class hierarchy. At of time of this change, TransferwiseAccount
// is the only known exception to the rule.
private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) throws IOException {
out.name("tradeCurrencies");
List<TradeCurrency> tradeCurrencies = account.getTradeCurrencies();
String tradeCurrenciesValue = "";
if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) {
tradeCurrenciesValue = tradeCurrencies.stream()
.map(TradeCurrency::getCode)
.collect(Collectors.joining(","));
private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) {
try {
String fieldName = "tradeCurrencies";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);
List<TradeCurrency> tradeCurrencies = account.getTradeCurrencies();
if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) {
String tradeCurrenciesValue = tradeCurrencies.stream()
.map(TradeCurrency::getCode) // convert each currency to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(tradeCurrenciesValue);
} else {
out.value(""); // if no currencies exist, write an empty string.
}
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
out.value(tradeCurrenciesValue);
}

// PaymentAccounts that support multiple 'tradeCurrencies' need to define a
Expand All @@ -206,24 +211,35 @@ private void writeSelectedTradeCurrencyField(JsonWriter out, PaymentAccount acco
throw new IllegalStateException("programmer error: " + errMsg);
}
}

private void writeAcceptedCountryCodesField(JsonWriter out, PaymentAccount account) {
try {
String fieldName = "acceptedCountryCodes";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);

private void writeAcceptedCountryCodesField(JsonWriter out, PaymentAccount account) throws IOException {
out.name("acceptedCountryCodes");
if (account instanceof CountryBasedPaymentAccount) {
List<Country> acceptedCountries = ((CountryBasedPaymentAccount) account).getAcceptedCountries();
String countryCodesValue = "";
if (acceptedCountries != null && !acceptedCountries.isEmpty()) {
countryCodesValue = acceptedCountries.stream()
.map(c -> c.code) // assuming 'code' is accessible or use getCode()
.collect(Collectors.joining(","));
if (account instanceof CountryBasedPaymentAccount) {
List<Country> acceptedCountries = ((CountryBasedPaymentAccount) account).getAcceptedCountries();
if (acceptedCountries != null && !acceptedCountries.isEmpty()) {
String countryCodesValue = acceptedCountries.stream()
.map(e -> e.code) // convert each country to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(countryCodesValue);
} else {
out.value(""); // if no countries exist, write an empty string.
}
} else {
out.value(""); // default empty value for non-country-based accounts.
}
out.value(countryCodesValue);
} else {
out.value("");
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}


@Override
public PaymentAccount read(JsonReader in) throws IOException {
PaymentAccount account = initNewPaymentAccount();
Expand Down

0 comments on commit 99edd2b

Please sign in to comment.