Skip to content

Commit b7bb749

Browse files
committed
WIP
1 parent a69b5f1 commit b7bb749

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+10989
-103
lines changed

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/jackson/modules/TransactionDeserializer.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,12 +27,14 @@
2727
import com.fasterxml.jackson.databind.node.ObjectNode;
2828
import org.xrpl.xrpl4j.model.transactions.Transaction;
2929
import org.xrpl.xrpl4j.model.transactions.TransactionType;
30+
import org.xrpl.xrpl4j.model.transactions.UnknownTransaction;
31+
import org.xrpl.xrpl4j.model.transactions.UnlModify;
3032

3133
import java.io.IOException;
3234

3335
/**
34-
* Custom deserializer for {@link Transaction}s, which deserializes to a specific {@link Transaction} type
35-
* based on the TransactionType JSON field.
36+
* Custom deserializer for {@link Transaction}s, which deserializes to a specific {@link Transaction} type based on the
37+
* TransactionType JSON field.
3638
*/
3739
public class TransactionDeserializer extends StdDeserializer<Transaction> {
3840

@@ -49,6 +51,35 @@ public Transaction deserialize(JsonParser jsonParser, DeserializationContext ctx
4951
ObjectNode objectNode = objectMapper.readTree(jsonParser);
5052

5153
TransactionType transactionType = TransactionType.forValue(objectNode.get("TransactionType").asText());
52-
return objectMapper.treeToValue(objectNode, Transaction.typeMap.inverse().get(transactionType));
54+
Class<? extends Transaction> transactionTypeClass = Transaction.typeMap.inverse().get(transactionType);
55+
56+
// If the transaction is of type `UnknownTransaction`, the `TransactionType` property must _not_ be removed. Thi
57+
// is so that the derived functions related to `TransactionType` in that class operate properly. However, for all
58+
// _other_ transaction types, the `TransactionType` property _must_ be removed so that it doesn't errantly show up
59+
// in the `unknownTransactionType` map. This approach works because every subclass of `Transaction` has a derived
60+
// Java method that specifies the type (thus allowing us to ignore this fiele in the general case).
61+
if (!UnknownTransaction.class.isAssignableFrom(transactionTypeClass)) {
62+
objectNode.remove("TransactionType");
63+
}
64+
65+
// If the transaction is of type `UnlModify`, then remove the `Account` property. This is because the JSON returned
66+
// by the rippled/clio API v1 has a bug where the account value is often an empty string. For this particular
67+
// transaction type (i.e., `UnlModify`) the Java value for the account is always set to ACCOUNT_ZERO via a default
68+
// method, so we remove this value from the JSON before deserialization because (1) it's not needed (the default
69+
// method handles population in Java) and (2) if not removed, this field will end up in the `unknownFields`
70+
// property, which is incorrect.
71+
if (UnlModify.class.isAssignableFrom(transactionTypeClass)) {
72+
// if (objectNode.get("Account").isEmpty()) {
73+
if (objectNode.has("Account")) {
74+
objectNode.remove("Account");
75+
}
76+
}
77+
78+
// TODO: Verify, and document if keeping.
79+
if (UnlModify.class.isAssignableFrom(transactionTypeClass)) {
80+
objectNode.remove("Account");
81+
}
82+
83+
return objectMapper.treeToValue(objectNode, transactionTypeClass);
5384
}
5485
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.xrpl.xrpl4j.model.jackson.modules;
2+
3+
/*-
4+
* ========================LICENSE_START=================================
5+
* xrpl4j :: model
6+
* %%
7+
* Copyright (C) 2020 - 2022 XRPL Foundation and its contributors
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =========================LICENSE_END==================================
21+
*/
22+
23+
import com.fasterxml.jackson.core.JsonGenerator;
24+
import com.fasterxml.jackson.core.JsonParser;
25+
import com.fasterxml.jackson.databind.DeserializationContext;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import com.fasterxml.jackson.databind.SerializerProvider;
28+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
29+
import com.fasterxml.jackson.databind.node.ObjectNode;
30+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
31+
import org.xrpl.xrpl4j.model.jackson.ObjectMapperFactory;
32+
import org.xrpl.xrpl4j.model.transactions.Transaction;
33+
import org.xrpl.xrpl4j.model.transactions.TransactionType;
34+
import org.xrpl.xrpl4j.model.transactions.UnknownTransaction;
35+
import org.xrpl.xrpl4j.model.transactions.UnlModify;
36+
37+
import java.io.IOException;
38+
39+
/**
40+
* Custom deserializer for {@link Transaction}s, which deserializes to a specific {@link Transaction} type based on the
41+
* TransactionType JSON field.
42+
*/
43+
public class TransactionSerializer { //} extends StdSerializer<Transaction> {
44+
//
45+
// /**
46+
// * No-args constructor.
47+
// */
48+
// protected TransactionSerializer() {
49+
// super(Transaction.class);
50+
// }
51+
//
52+
// @Override
53+
// public void serialize(Transaction value, JsonGenerator gen, SerializerProvider provider) throws IOException {
54+
//
55+
// if (UnlModify.class.isAssignableFrom(value.getClass())) {
56+
// gen.writeStartObject();
57+
//
58+
// // 1. Delegate to Jackson for the existing fields:
59+
// provider.defaultSerializeValue(value, gen); // Delegate to Jackson
60+
//
61+
//// ObjectMapperFactory.create().writeValue(gen, value);
62+
//
63+
// // Serialize the existing fields (delegate to Jackson's default if possible)
64+
//// gen.writeObject((UnlModify) value); // This will handle the standard Transaction fields
65+
//
66+
// // Add the extra "Account" field here because by marking it @Derived in the Immutable, this field does not show
67+
// // up during serialization.
68+
// gen.writeFieldName("Account");
69+
// gen.writeString(value.account().value());
70+
//
71+
// gen.writeEndObject();
72+
// } else {
73+
// // Rely on Jackson's automatic serialization
74+
// gen.writeObject(value);
75+
// }
76+
// }
77+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.xrpl.xrpl4j.model.jackson.modules;
2+
3+
/*-
4+
* ========================LICENSE_START=================================
5+
* xrpl4j :: model
6+
* %%
7+
* Copyright (C) 2020 - 2022 XRPL Foundation and its contributors
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =========================LICENSE_END==================================
21+
*/
22+
23+
import com.fasterxml.jackson.core.JsonGenerator;
24+
import com.fasterxml.jackson.databind.SerializerProvider;
25+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
26+
import org.xrpl.xrpl4j.model.transactions.Transaction;
27+
import org.xrpl.xrpl4j.model.transactions.UnlModify;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* Custom deserializer for {@link Transaction}s, which deserializes to a specific {@link Transaction} type based on the
33+
* TransactionType JSON field.
34+
*/
35+
public class UnlModifyTransactionSerializer extends StdSerializer<UnlModify> {
36+
37+
/**
38+
* No-args constructor.
39+
*/
40+
protected UnlModifyTransactionSerializer() {
41+
super(UnlModify.class);
42+
}
43+
44+
@Override
45+
public void serialize(UnlModify value, JsonGenerator gen, SerializerProvider provider) throws IOException {
46+
47+
// if (UnlModify.class.isAssignableFrom(value.getClass())) {
48+
// gen.writeStartObject();
49+
50+
// if (gen.getCurrentValue() == null
51+
// gen.getCurrentValue().toString().length() == 1
52+
// ) {
53+
// 1. Delegate to Jackson for the existing fields:
54+
provider.defaultSerializeValue(value, gen); // Delegate to Jackson
55+
// }
56+
57+
// ObjectMapperFactory.create().writeValue(gen, value);
58+
59+
// Serialize the existing fields (delegate to Jackson's default if possible)
60+
// gen.writeObject((UnlModify) value); // This will handle the standard Transaction fields
61+
62+
// Add the extra "Account" field here because by marking it @Derived in the Immutable, this field does not show
63+
// up during serialization.
64+
// gen.writeFieldName("Account");
65+
// gen.writeString(value.account().value());
66+
67+
// gen.writeEndObject();
68+
// } else {
69+
// Rely on Jackson's automatic serialization
70+
// gen.writeObject(value);
71+
// }
72+
}
73+
}

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/jackson/modules/Xrpl4jModule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,6 +29,7 @@
2929
import org.xrpl.xrpl4j.model.flags.Flags;
3030
import org.xrpl.xrpl4j.model.transactions.CurrencyAmount;
3131
import org.xrpl.xrpl4j.model.transactions.Transaction;
32+
import org.xrpl.xrpl4j.model.transactions.UnlModify;
3233
import org.xrpl.xrpl4j.model.transactions.metadata.AffectedNode;
3334
import org.xrpl.xrpl4j.model.transactions.metadata.MetaLedgerEntryType;
3435

@@ -60,10 +61,11 @@ public Xrpl4jModule() {
6061
addSerializer(LedgerIndex.class, new LedgerIndexSerializer());
6162
addDeserializer(LedgerIndex.class, new LedgerIndexDeserializer());
6263

64+
addSerializer(UnlModify.class, new UnlModifyTransactionSerializer());
6365
addDeserializer(Transaction.class, new TransactionDeserializer());
6466

6567
addDeserializer(ServerInfo.class, new ServerInfoDeserializer());
66-
68+
6769
addSerializer(UnsignedByteArray.class, new UnsignedByteArraySerializer());
6870
addDeserializer(UnsignedByteArray.class, new UnsignedByteArrayDeserializer());
6971

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AccountDelete.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ default TransactionFlags flags() {
8181
@JsonProperty("DestinationTag")
8282
Optional<UnsignedInteger> destinationTag();
8383

84+
@JsonProperty(value = "TransactionType")
85+
@Value.Derived
86+
default TransactionType transactionType() {
87+
return TransactionType.ACCOUNT_DELETE;
88+
}
8489
}

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AccountSet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ default void checkTickSize() {
373373

374374
}
375375

376+
@JsonProperty(value = "TransactionType")
377+
@Value.Derived
378+
default TransactionType transactionType() {
379+
return TransactionType.ACCOUNT_SET;
380+
}
381+
376382
/**
377383
* There are several options which can be either enabled or disabled for an account. Account options are represented
378384
* by different types of flags depending on the situation.

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AmmBid.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ default TransactionFlags flags() {
107107
@JsonProperty("AuthAccounts")
108108
List<AuthAccountWrapper> authAccounts();
109109

110+
@JsonProperty(value = "TransactionType")
111+
@Value.Derived
112+
default TransactionType transactionType() {
113+
return TransactionType.AMM_BID;
114+
}
110115
}

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AmmCreate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ default TransactionFlags flags() {
6868
@JsonProperty("TradingFee")
6969
TradingFee tradingFee();
7070

71+
@JsonProperty(value = "TransactionType")
72+
@Value.Derived
73+
default TransactionType transactionType() {
74+
return TransactionType.AMM_CREATE;
75+
}
7176
}

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AmmDelete.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ default TransactionFlags flags() {
5858
@JsonProperty("Asset2")
5959
Issue asset2();
6060

61+
@JsonProperty(value = "TransactionType")
62+
@Value.Derived
63+
default TransactionType transactionType() {
64+
return TransactionType.AMM_DELETE;
65+
}
6166
}

xrpl4j-core/src/main/java/org/xrpl/xrpl4j/model/transactions/AmmDeposit.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ static ImmutableAmmDeposit.Builder builder() {
105105
*/
106106
@JsonProperty("TradingFee")
107107
Optional<TradingFee> tradingFee();
108+
109+
@JsonProperty(value = "TransactionType")
110+
@Value.Derived
111+
default TransactionType transactionType() {
112+
return TransactionType.AMM_DEPOSIT;
113+
}
108114
}

0 commit comments

Comments
 (0)