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

Add a condition on the duplicated event names #1872

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,14 @@ private String buildEventDefinitionName(String eventName) {
return eventName.toUpperCase() + "_EVENT";
}

private List<MethodSpec> buildFunctionDefinitions(
List<MethodSpec> buildFunctionDefinitions(
String className,
TypeSpec.Builder classBuilder,
List<AbiDefinition> functionDefinitions)
throws ClassNotFoundException {

Set<String> duplicateFunctionNames = getDuplicateFunctionNames(functionDefinitions);
changeDuplicateEventsName(functionDefinitions);
List<MethodSpec> methodSpecs = new ArrayList<>();
for (AbiDefinition functionDefinition : functionDefinitions) {
if (functionDefinition.getType().equals(TYPE_FUNCTION)) {
Expand All @@ -436,6 +437,27 @@ private List<MethodSpec> buildFunctionDefinitions(
return methodSpecs;
}

private void changeDuplicateEventsName(List<AbiDefinition> functionDefinitions) {

Map<String, Integer> countMap = new HashMap<>();

functionDefinitions.stream()
.filter(
function ->
TYPE_EVENT.equals(function.getType()) && function.getName() != null)
.forEach(
function -> {
String functionName = function.getName();
if (countMap.containsKey(functionName)) {
int count = countMap.get(functionName);
function.setName(functionName + count);
countMap.put(functionName, count + 1);
} else {
countMap.put(functionName, 1);
}
});
}

private List<TypeSpec> buildStructTypes(final List<AbiDefinition> functionDefinitions)
throws ClassNotFoundException {
final List<AbiDefinition.NamedType> orderedKeys = extractStructs(functionDefinitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,133 @@ public void testBuildFuncNameConstants() throws Exception {
assertEquals(builder.build().toString(), (expected));
}

@Test
public void testBuildFunctionDuplicatedEventNames() throws Exception {

AbiDefinition firstEventDefinition =
new AbiDefinition(
false,
Arrays.asList(
new NamedType("action", "string", false),
new NamedType("pauseState", "bool", false)),
"eventName",
Collections.emptyList(),
"event",
false);
AbiDefinition secondEventDefinition =
new AbiDefinition(
false,
Arrays.asList(
new NamedType("cToken", "address", false),
new NamedType("action", "string", false),
new NamedType("pauseState", "bool", false)),
"eventName",
Collections.emptyList(),
"event",
false);
TypeSpec.Builder builder = TypeSpec.classBuilder("testClass");
builder.addMethods(
solidityFunctionWrapper.buildFunctionDefinitions(
"testClass",
builder,
Arrays.asList(firstEventDefinition, secondEventDefinition)));

String expected =
"class testClass {\n"
+ " public static final org.web3j.abi.datatypes.Event EVENTNAME_EVENT = new org.web3j.abi.datatypes.Event(\"eventName\", \n"
+ " java.util.Arrays.<org.web3j.abi.TypeReference<?>>asList(new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Bool>() {}));\n"
+ " ;\n"
+ "\n"
+ " public static final org.web3j.abi.datatypes.Event EVENTNAME1_EVENT = new org.web3j.abi.datatypes.Event(\"eventName1\", \n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Event name shouldn't be modified new org.web3j.abi.datatypes.Event(\"eventName1\"... will fail in request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please give me some more details about that?
Do you mean it is not possible to use the abiDef.setName() function on TYPE_EVENTS objects?

Thanks

+ " java.util.Arrays.<org.web3j.abi.TypeReference<?>>asList(new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Bool>() {}));\n"
+ " ;\n"
+ "\n"
+ " public static java.util.List<EventNameEventResponse> getEventNameEvents(org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceipt) {\n"
+ " java.util.List<org.web3j.tx.Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(EVENTNAME_EVENT, transactionReceipt);\n"
+ " java.util.ArrayList<EventNameEventResponse> responses = new java.util.ArrayList<EventNameEventResponse>(valueList.size());\n"
+ " for (org.web3j.tx.Contract.EventValuesWithLog eventValues : valueList) {\n"
+ " EventNameEventResponse typedResponse = new EventNameEventResponse();\n"
+ " typedResponse.log = eventValues.getLog();\n"
+ " typedResponse.action = (java.lang.String) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.pauseState = (java.lang.Boolean) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " responses.add(typedResponse);\n"
+ " }\n"
+ " return responses;\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<EventNameEventResponse> eventNameEventFlowable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
+ " return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<org.web3j.protocol.core.methods.response.Log, EventNameEventResponse>() {\n"
+ " @java.lang.Override\n"
+ " public EventNameEventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(EVENTNAME_EVENT, log);\n"
+ " EventNameEventResponse typedResponse = new EventNameEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.action = (java.lang.String) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.pauseState = (java.lang.Boolean) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ " });\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<EventNameEventResponse> eventNameEventFlowable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
+ " org.web3j.protocol.core.methods.request.EthFilter filter = new org.web3j.protocol.core.methods.request.EthFilter(startBlock, endBlock, getContractAddress());\n"
+ " filter.addSingleTopic(org.web3j.abi.EventEncoder.encode(EVENTNAME_EVENT));\n"
+ " return eventNameEventFlowable(filter);\n"
+ " }\n"
+ "\n"
+ " public static java.util.List<EventName1EventResponse> getEventName1Events(org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceipt) {\n"
+ " java.util.List<org.web3j.tx.Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(EVENTNAME1_EVENT, transactionReceipt);\n"
+ " java.util.ArrayList<EventName1EventResponse> responses = new java.util.ArrayList<EventName1EventResponse>(valueList.size());\n"
+ " for (org.web3j.tx.Contract.EventValuesWithLog eventValues : valueList) {\n"
+ " EventName1EventResponse typedResponse = new EventName1EventResponse();\n"
+ " typedResponse.log = eventValues.getLog();\n"
+ " typedResponse.cToken = (java.lang.String) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.action = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " typedResponse.pauseState = (java.lang.Boolean) eventValues.getNonIndexedValues().get(2).getValue();\n"
+ " responses.add(typedResponse);\n"
+ " }\n"
+ " return responses;\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<EventName1EventResponse> eventName1EventFlowable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
+ " return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<org.web3j.protocol.core.methods.response.Log, EventName1EventResponse>() {\n"
+ " @java.lang.Override\n"
+ " public EventName1EventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(EVENTNAME1_EVENT, log);\n"
+ " EventName1EventResponse typedResponse = new EventName1EventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.cToken = (java.lang.String) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.action = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " typedResponse.pauseState = (java.lang.Boolean) eventValues.getNonIndexedValues().get(2).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ " });\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<EventName1EventResponse> eventName1EventFlowable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
+ " org.web3j.protocol.core.methods.request.EthFilter filter = new org.web3j.protocol.core.methods.request.EthFilter(startBlock, endBlock, getContractAddress());\n"
+ " filter.addSingleTopic(org.web3j.abi.EventEncoder.encode(EVENTNAME1_EVENT));\n"
+ " return eventName1EventFlowable(filter);\n"
+ " }\n"
+ "\n"
+ " public static class EventNameEventResponse extends org.web3j.protocol.core.methods.response.BaseEventResponse {\n"
+ " public java.lang.String action;\n"
+ "\n"
+ " public java.lang.Boolean pauseState;\n"
+ " }\n"
+ "\n"
+ " public static class EventName1EventResponse extends org.web3j.protocol.core.methods.response.BaseEventResponse {\n"
+ " public java.lang.String cToken;\n"
+ "\n"
+ " public java.lang.String action;\n"
+ "\n"
+ " public java.lang.Boolean pauseState;\n"
+ " }\n"
+ "}\n";

assertEquals(builder.build().toString(), (expected));
}

@Test
public void testBuildFunctionTransactionAndCall() throws Exception {
AbiDefinition functionDefinition =
Expand Down