Skip to content

Commit

Permalink
add method ABIJSON.parseABIField
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Oct 16, 2024
1 parent 93af791 commit c199793
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,24 @@ public static <T extends ABIObject> List<T> parseElements(String arrayJson, Set<
}

public static <T extends ABIObject> List<T> parseElements(int flags, String arrayJson, Set<TypeEnum> types) {
// return ABIJSON.<T>stream(flags, arrayJson, types).collect(Collectors.toList());
try (final JsonReader reader = read(arrayJson)) {
return parseArray(reader, types, flags);
} catch (IOException io) {
throw new IllegalStateException(io);
}
}

private static <T extends ABIObject> List<T> parseArray(final JsonReader reader, Set<TypeEnum> types, int flags) throws IOException {
final List<T> list = new ArrayList<>();
reader.beginArray();
final MessageDigest digest = Function.newDefaultDigest();
final JsonReader jsonReader = read(arrayJson);
try {
jsonReader.beginArray();
while (jsonReader.peek() != JsonToken.END_ARRAY) {
T e = tryParseStreaming(jsonReader, types, digest, flags);
if (e != null) {
list.add(e);
}
while (reader.peek() != JsonToken.END_ARRAY) {
T e = tryParseStreaming(reader, types, digest, flags);
if (e != null) {
list.add(e);
}
jsonReader.endArray();
} catch (IOException io) {
throw new IllegalStateException(io);
}
reader.endArray();
return list;
}

Expand Down Expand Up @@ -525,4 +527,19 @@ public static TupleType<?> parseTupleType(JsonReader reader, final boolean event
private static JsonReader read(String json) {
return new JsonReader(new StringReader(json));
}

public static <T extends ABIObject> List<T> parseABIField(int flags, String objectJson, Set<TypeEnum> types) {
try (final JsonReader reader = read(objectJson)) {
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
if ("abi".equals(reader.nextName())) {
return parseArray(reader, types, flags);
}
reader.skipValue();
}
} catch (IOException io) {
throw new IllegalStateException(io);
}
throw new IllegalStateException("abi key not found");
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,26 @@ public void testUserDefinedValueTypes() {

assertEquals(json, f.toString());
}

@Test
public void testParseABIField() {
final String json = "{\n" +
" \"abi\": [\n" +
" {\n" +
" \"name\": \"aller\",\n" +
" \"inputs\": [\n" +
" { \"name\": \"amt\", \"type\": \"uint256\" },\n" +
" { \"name\": \"balance\", \"type\": \"uint256\" },\n" +
" { \"name\": \"rate\", \"type\": \"uint256\" },\n" +
" { \"name\": \"dest\", \"type\": \"address\" },\n" +
" { \"name\": \"msg\", \"type\": \"string\" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
final List<ABIObject> objects = ABIJSON.parseABIField(FLAGS_NONE, json, ABIJSON.ALL);
assertEquals(1, objects.size());
assertEquals(TypeEnum.FUNCTION, objects.get(0).getType());
assertEquals("aller(uint256,uint256,uint256,address,string)", objects.get(0).getCanonicalSignature());
}
}

0 comments on commit c199793

Please sign in to comment.