Skip to content

Commit

Permalink
fix:handle different solc compile result raw json for abi bin and doc (
Browse files Browse the repository at this point in the history
…#38)

* feat: use only one source code copy to support mutiple solc version included 0.4.25 0.5.2 0,6.10 0.8.11

* fix: format code by google code format plugin.

* fix: The 0.8.11 solc return abi and doc as json object but 0.6.10 0.5.2 0.4.25 solc return abi and doc as string, so the parse logic should handle it。

* fix: auto fix by google code format

---------

Co-authored-by: dwzhan <[email protected]>
  • Loading branch information
HelloAldis and dwzhan authored Apr 28, 2024
1 parent 3937d3c commit c3ee274
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/main/java/org/fisco/solc/compiler/CompilationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ public static CompilationResult parse(String rawJson) throws IOException {
JsonObject contractJsonObject =
asJsonObject.get(contract.toString()).getAsJsonObject();
JsonObject abiObject = new JsonObject();
abiObject.addProperty("abi", contractJsonObject.get("abi").toString());
abiObject.addProperty("bin", contractJsonObject.get("bin").getAsString());
abiObject.addProperty("metadata", contractJsonObject.get("metadata").getAsString());
abiObject.addProperty("abi", getJsonValueAsString(contractJsonObject, "abi"));
abiObject.addProperty("bin", getJsonValueAsString(contractJsonObject, "bin"));
abiObject.addProperty(
"metadata", getJsonValueAsString(contractJsonObject, "metadata"));

if (contractJsonObject.get("userdoc") != null) {
abiObject.addProperty("userdoc", contractJsonObject.get("userdoc").toString());
abiObject.addProperty(
"userdoc", getJsonValueAsString(contractJsonObject, "userdoc"));
}

if (contractJsonObject.get("devdoc") != null) {
abiObject.addProperty("devdoc", contractJsonObject.get("devdoc").toString());
abiObject.addProperty(
"devdoc", getJsonValueAsString(contractJsonObject, "devdoc"));
}
contractObject.add(contract.toString(), abiObject);
}
Expand All @@ -65,6 +68,18 @@ public static CompilationResult parse(String rawJson) throws IOException {
}
}

private static String getJsonValueAsString(JsonObject jsonObject, String key) {
if (jsonObject == null || jsonObject.get(key) == null || jsonObject.get(key).isJsonNull()) {
return null;
}

if (jsonObject.get(key).isJsonPrimitive()) {
return jsonObject.get(key).getAsString();
}

return jsonObject.get(key).toString();
}

/**
* @param contractName The contract name
* @return the first contract found for a given contract name; use {@link #getContract(Path,
Expand Down

0 comments on commit c3ee274

Please sign in to comment.