Skip to content

Commit afbe6c8

Browse files
Merge pull request #1 from OpenElements/event-support
Event support
2 parents b4f0169 + ef9c426 commit afbe6c8

File tree

19 files changed

+240
-15
lines changed

19 files changed

+240
-15
lines changed

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/GsonAbiParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public GsonAbiParser() {
1818
this.gson = new GsonBuilder()
1919
.registerTypeAdapterFactory(new AbiTypeAdapterFactory())
2020
.create();
21-
2221
}
2322

2423
public @NonNull AbiModel parse(@NonNull Reader abiReader) throws AbiParserException {

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/implementation/AbiEntryTypeAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.ArrayList;
1313
import java.util.Collections;
1414
import java.util.List;
15+
import org.jspecify.annotations.NonNull;
1516

1617
public class AbiEntryTypeAdapter extends BasicTypeAdapter<AbiEntry> {
1718

@@ -20,7 +21,7 @@ public AbiEntryTypeAdapter(Gson gson) {
2021
}
2122

2223
@Override
23-
public AbiEntry read(JsonReader in) throws IOException {
24+
public AbiEntry read(@NonNull final JsonReader in) throws IOException {
2425
final JsonObject abiEntryObject = readObject(in);
2526
final AbiEntryType type = getStringValue(abiEntryObject, "type")
2627
.map(AbiEntryType::of)

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/implementation/AbiModelTypeAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public AbiModelTypeAdapter(@NonNull Gson gson) {
1818
}
1919

2020
@Override
21-
public AbiModel read(JsonReader in) throws IOException {
21+
public AbiModel read(@NonNull final JsonReader in) throws IOException {
2222
JsonArray array = readArray(in);
2323
final List<AbiEntry> entries = new ArrayList<>();
2424
array.forEach(jsonElement -> {

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/implementation/AbiParameterTypeAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.List;
12+
import org.jspecify.annotations.NonNull;
1213

1314
public class AbiParameterTypeAdapter extends BasicTypeAdapter<AbiParameter> {
1415

@@ -17,7 +18,7 @@ public AbiParameterTypeAdapter(Gson gson) {
1718
}
1819

1920
@Override
20-
public AbiParameter read(JsonReader in) throws IOException {
21+
public AbiParameter read(@NonNull final JsonReader in) throws IOException {
2122
final JsonObject parameterObject = readObject(in);
2223

2324
final String name = getStringValue(parameterObject, "name")

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/implementation/AbiTypeAdapterFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import com.openelements.hiero.smartcontract.abi.model.AbiEntry;
88
import com.openelements.hiero.smartcontract.abi.model.AbiModel;
99
import com.openelements.hiero.smartcontract.abi.model.AbiParameter;
10+
import java.util.Objects;
11+
import org.jspecify.annotations.NonNull;
1012

1113
public class AbiTypeAdapterFactory implements TypeAdapterFactory {
1214

1315
@Override
14-
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
16+
public <T> TypeAdapter<T> create(@NonNull final Gson gson, @NonNull final TypeToken<T> type) {
17+
Objects.requireNonNull(type, "type must not be null");
1518
if (type.getRawType().equals(AbiModel.class)) {
1619
return (TypeAdapter<T>) new AbiModelTypeAdapter(gson);
1720
}

abi-parser-simple/src/main/java/com/openelements/hiero/smartcontract/abi/simple/implementation/BasicTypeAdapter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,30 @@ public abstract class BasicTypeAdapter<T> extends TypeAdapter<T> {
2121

2222
private final TypeAdapter<JsonArray> arrayTypeAdapter;
2323

24-
25-
public BasicTypeAdapter(final Gson gson) {
24+
public BasicTypeAdapter(@NonNull final Gson gson) {
2625
this.gson = Objects.requireNonNull(gson, "gson");
2726
objectTypeAdapter = gson.getAdapter(JsonObject.class);
2827
arrayTypeAdapter = gson.getAdapter(JsonArray.class);
2928
}
3029

31-
protected JsonObject readObject(JsonReader in) throws IOException {
30+
protected JsonObject readObject(@NonNull final JsonReader in) throws IOException {
3231
return objectTypeAdapter.read(in);
3332
}
3433

35-
protected JsonArray readArray(JsonReader in) throws IOException {
34+
protected JsonArray readArray(@NonNull final JsonReader in) throws IOException {
3635
return arrayTypeAdapter.read(in);
3736
}
3837

39-
40-
protected <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
38+
protected <T> T fromJson(@NonNull final JsonElement json, @NonNull final Class<T> classOfT) throws JsonSyntaxException {
4139
return gson.fromJson(json, classOfT);
4240
}
4341

44-
42+
@NonNull
4543
protected Gson getGson() {
4644
return gson;
4745
}
4846

47+
@NonNull
4948
protected TypeAdapter<JsonObject> getObjectTypeAdapter() {
5049
return objectTypeAdapter;
5150
}
@@ -55,6 +54,7 @@ public void write(JsonWriter out, T value) throws IOException {
5554
throw new UnsupportedOperationException("Not supported yet.");
5655
}
5756

57+
@NonNull
5858
protected JsonArray getArray(@NonNull final JsonObject jsonObject, @NonNull final String key) {
5959
Objects.requireNonNull(jsonObject, "jsonObject");
6060
Objects.requireNonNull(key, "key");
@@ -69,6 +69,7 @@ protected JsonArray getArray(@NonNull final JsonObject jsonObject, @NonNull fina
6969
return new JsonArray();
7070
}
7171

72+
@NonNull
7273
protected Optional<String> getStringValue(@NonNull final JsonObject jsonObject, @NonNull final String key) {
7374
Objects.requireNonNull(jsonObject, "jsonObject");
7475
Objects.requireNonNull(key, "key");
@@ -82,6 +83,7 @@ protected Optional<String> getStringValue(@NonNull final JsonObject jsonObject,
8283
return Optional.empty();
8384
}
8485

86+
@NonNull
8587
protected Optional<Boolean> getBooleanValue(@NonNull final JsonObject jsonObject, @NonNull final String key) {
8688
Objects.requireNonNull(jsonObject, "jsonObject");
8789
Objects.requireNonNull(key, "key");

abi-parser/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
<artifactId>jspecify</artifactId>
2424
<scope>compile</scope>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.bouncycastle</groupId>
28+
<artifactId>bcprov-jdk15to18</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter</artifactId>
33+
<scope>test</scope>
34+
</dependency>
2635
</dependencies>
2736

2837
<build>

abi-parser/src/main/java/com/openelements/hiero/smartcontract/abi/model/AbiEvent.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.openelements.hiero.smartcontract.abi.model;
22

3+
import com.openelements.hiero.smartcontract.abi.util.HexConverter;
4+
import com.openelements.hiero.smartcontract.abi.util.KeccakUtil;
5+
import java.nio.charset.StandardCharsets;
36
import java.util.List;
47
import java.util.Objects;
58
import org.jspecify.annotations.NonNull;
6-
import org.jspecify.annotations.Nullable;
79

810
public record AbiEvent(@NonNull String name, @NonNull List<AbiParameter> inputs, boolean anonymous) implements AbiEntry{
911

@@ -16,4 +18,32 @@ public record AbiEvent(@NonNull String name, @NonNull List<AbiParameter> inputs,
1618
public AbiEntryType type() {
1719
return AbiEntryType.EVENT;
1820
}
21+
22+
@NonNull
23+
public List<AbiParameter> getIndexedInputParameters() {
24+
return inputs.stream().filter(AbiParameter::indexed).toList();
25+
}
26+
27+
@NonNull
28+
public List<AbiParameter> getNonIndexedInputParameters() {
29+
return inputs.stream().filter(parameter -> !parameter.indexed()).toList();
30+
}
31+
32+
@NonNull
33+
private String createEventSignature() {
34+
final List<String> canonicalParameterTypes = inputs.stream().map(AbiParameter::getCanonicalType).toList();
35+
return name + "(" + String.join(",", canonicalParameterTypes) + ")";
36+
}
37+
38+
@NonNull
39+
private byte[] createEventSignatureHash() {
40+
final String eventSignature = createEventSignature();
41+
return KeccakUtil.keccak256(eventSignature.getBytes(StandardCharsets.UTF_8));
42+
}
43+
44+
@NonNull
45+
public String createEventSignatureHashAsHex() {
46+
final byte[] eventSignatureHash = createEventSignatureHash();
47+
return "0x" + HexConverter.bytesToHex(eventSignatureHash);
48+
}
1949
}

abi-parser/src/main/java/com/openelements/hiero/smartcontract/abi/model/AbiFunction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ public record AbiFunction(@NonNull AbiEntryType type, @Nullable String name, @No
3636
Objects.requireNonNull(inputs, "inputs");
3737
Objects.requireNonNull(outputs, "outputs");
3838
Objects.requireNonNull(stateMutability, "stateMutability");
39-
4039
}
4140
}

abi-parser/src/main/java/com/openelements/hiero/smartcontract/abi/model/AbiModel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ public record AbiModel(@NonNull List<AbiEntry> entries) {
1010
Objects.requireNonNull(entries, "entries");
1111
}
1212

13+
@NonNull
1314
public List<AbiFunction> getFunctions() {
1415
return getEntriesOfType(AbiFunction.class);
1516
}
1617

18+
@NonNull
1719
public List<AbiEvent> getEvents() {
1820
return getEntriesOfType(AbiEvent.class);
1921
}
2022

23+
@NonNull
2124
public List<AbiError> getErrors() {
2225
return getEntriesOfType(AbiError.class);
2326
}
2427

25-
private <T extends AbiEntry> List<T> getEntriesOfType(Class<T> type) {
28+
@NonNull
29+
private <T extends AbiEntry> List<T> getEntriesOfType(@NonNull final Class<T> type) {
30+
Objects.requireNonNull(type, "type must not be null");
2631
return entries.stream()
2732
.filter(type::isInstance)
2833
.map(type::cast)

0 commit comments

Comments
 (0)