Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:handle different solc compile result raw json for abi bin and doc #38

Merged
merged 4 commits into from
Apr 28, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 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,16 @@ 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 +65,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
Loading