Skip to content

Commit

Permalink
solution: move most of JSON mapping config to annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Oct 21, 2024
1 parent 3b22b71 commit 041bbf1
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 176 deletions.
4 changes: 4 additions & 0 deletions etherjar-rpc-json/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= JSON Mapping for RPC API

.Reference:
- https://github.com/ethereum/execution-apis/tree/main/src/schemas
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.Map;

// Uses a custom deserializer to read the JSON object, b/c the structure is a bit too complex for annotations
@JsonDeserialize(using = StateDiffJsonDeserializer.class)
public class StateDiffJson {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class StateDiffJsonDeserializer extends JsonDeserializer<StateDiffJson> {

protected static final Function<String, Wei> WEI_CONVERTER = Wei::from;
protected static final Function<String, Wei> WEI_CONVERTER = Wei::fromHex;
protected static final Function<String, HexData> CODE_CONVERTER = HexData::from;
protected static final Function<String, Long> NONCE_CONVERTER = (s) -> HexQuantity.from(s).getValue().longValueExact();
protected static final Function<String, Hex32> STORAGE_CONVERTER = Hex32::from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

// Uses a custom serialization because it's either a boolean or an object
@JsonDeserialize(using = SyncingJsonDeserializer.class)
@JsonSerialize(using = SyncingJsonSerializer.class)
public class SyncingJson {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package io.emeraldpay.etherjar.rpc.json;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.emeraldpay.etherjar.domain.Address;
import io.emeraldpay.etherjar.domain.BlockHash;
import io.emeraldpay.etherjar.domain.TransactionId;
Expand All @@ -27,18 +30,24 @@
import java.io.Serializable;
import java.util.List;

@JsonDeserialize(using = TraceItemJsonDeserializer.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TraceItemJson implements Serializable {

private TraceType type;
private Action action;
private BlockHash blockHash;
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long blockNumber;
private Result result;
private String error;
private Long subtraces;
@JsonDeserialize(contentUsing = HexLongDeserializer.class)
@JsonSerialize(contentUsing = HexLongSerializer.class)
private List<Long> traceAddress;
private TransactionId transactionHash;
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long transactionPosition;

public Action getAction() {
Expand Down Expand Up @@ -157,6 +166,8 @@ public static class Action implements Serializable {

private CallType callType;
private Address from;
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long gas;
private HexData init;
private HexData input;
Expand Down Expand Up @@ -284,14 +295,30 @@ public int hashCode() {
}

public static enum CallType {
NONE, CALL, CALLCODE, DELEGATECALL, STATICCALL;
@JsonProperty("none")
NONE,
@JsonProperty("call")
CALL,
@JsonProperty("callcode")
CALLCODE,
@JsonProperty("delegatecall")
DELEGATECALL,
@JsonProperty("staticcall")
STATICCALL;
}

public static enum TraceType {
CREATE, CALL, SUICIDE;
@JsonProperty("create")
CREATE,
@JsonProperty("call")
CALL,
@JsonProperty("suicide")
SUICIDE;
}

public static class Result implements Serializable {
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long gasUsed;
private HexData output;
private Address address;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@

package io.emeraldpay.etherjar.rpc.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.emeraldpay.etherjar.domain.Address;
import io.emeraldpay.etherjar.domain.Wei;
import io.emeraldpay.etherjar.hex.HexData;

import java.io.Serializable;

@JsonSerialize(using = TransactionCallJsonSerializer.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TransactionCallJson implements Serializable {

private Address from;
private Address to;
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long gas;
private Wei gasPrice;
private Wei value;
private HexData data;
@JsonDeserialize(using = HexLongDeserializer.class)
@JsonSerialize(using = HexLongSerializer.class)
private Long nonce;

public TransactionCallJson() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.emeraldpay.etherjar.rpc.json;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.emeraldpay.etherjar.domain.Address;
import io.emeraldpay.etherjar.domain.Wei;
Expand All @@ -9,6 +10,7 @@
/**
* @see <a href="https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml">https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml</a>
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class WithdrawalJson {
@JsonDeserialize(using = HexLongDeserializer.class)
private Long index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class JacksonEthRpcConverterSpec extends Specification {
act == '{"jsonrpc":"2.0","method":"eth_call","params":[' +
'{"from":"0xb7819ff807d9d52a9ce5d713dc7053e8871e077b",' +
'"to":"0x57d90b64a1a57749b0f932f1a3395792e12e7055",' +
'"gas":"0x0186a0",' +
'"gasPrice":"0x071afd498d0000",' +
'"gas":"0x186a0",' +
'"gasPrice":"0x71afd498d0000",' +
'"value":"0x14d1120d7b160000",' +
'"data":"0xa9059cbb00000000000000000000000014dd45d07d1d700579a9b7cfb3a4536890aafdc2"}' +
',"latest"],"id":1}'
Expand Down

0 comments on commit 041bbf1

Please sign in to comment.