diff --git a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index 8e75a4356..b29dd6c18 100644 --- a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -586,7 +586,11 @@ private List buildStructTypes(final List functionDefini @NotNull private static String getStructName(String internalType) { final String fullStructName = internalType.substring(internalType.lastIndexOf(" ") + 1); - final String tempStructName = fullStructName.substring(fullStructName.lastIndexOf(".") + 1); + String tempStructName = fullStructName.substring(fullStructName.lastIndexOf(".") + 1); + int arrayPos = tempStructName.indexOf("["); + if (arrayPos > -1) { + tempStructName = tempStructName.substring(0, arrayPos); + } final String structName = SourceVersion.isName(tempStructName) ? tempStructName : "_" + tempStructName; return structName; @@ -633,7 +637,7 @@ private List extractStructs( parameters.addAll(definition.getOutputs()); return parameters.stream() .map(this::normalizeNamedType) - .filter(namedType -> namedType.getType().equals("tuple")); + .filter(namedType -> namedType.getType().startsWith("tuple")); }) .forEach( namedType -> { @@ -642,7 +646,7 @@ private List extractStructs( .map(this::normalizeNamedType) .filter( nestedNamedStruct -> - nestedNamedStruct.getType().equals("tuple")) + nestedNamedStruct.getType().startsWith("tuple")) .forEach( nestedNamedType -> structMap.put( diff --git a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java index cad59aba2..8c3e924d5 100644 --- a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java +++ b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java @@ -218,6 +218,20 @@ public void testSameInnerStructNameCompareJavaFile() throws Exception { compareJavaFile("SameInnerStructName", true); } + @Test + public void testArrayOfStructClassGeneration() throws Exception { + testCodeGeneration( + "arrayofstructclassgeneration", + "ArrayOfStructClassGeneration", + JAVA_TYPES_ARG, + false); + } + + @Test + public void testArrayOfStructClassGenerationCompareJavaFile() throws Exception { + compareJavaFile("ArrayOfStructClassGeneration", true); + } + private void compareJavaFile(String inputFileName, boolean useBin) throws Exception { String contract = inputFileName.toLowerCase(); String packagePath = diff --git a/codegen/src/test/resources/solidity/arrayofstructclassgeneration/ArrayOfStructClassGeneration.sol b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/ArrayOfStructClassGeneration.sol new file mode 100644 index 000000000..6b7afad9f --- /dev/null +++ b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/ArrayOfStructClassGeneration.sol @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + + +contract ArrayOfStructClassGeneration { + + struct Foo { + bool dummy; + } + + constructor() { + + } + function test(Foo[2] calldata foos) external { + revert(); + } +} diff --git a/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.abi b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.abi new file mode 100644 index 000000000..b611702d6 --- /dev/null +++ b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.abi @@ -0,0 +1 @@ +[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"components":[{"internalType":"bool","name":"dummy","type":"bool"}],"internalType":"struct ArrayOfStructClassGeneration.Foo[2]","name":"foos","type":"tuple[2]"}],"name":"test","outputs":[],"stateMutability":"nonpayable","type":"function"}] \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.bin b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.bin new file mode 100644 index 000000000..8a52f9811 --- /dev/null +++ b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/ArrayOfStructClassGeneration.bin @@ -0,0 +1 @@ +608060405234801561000f575f80fd5b5060c88061001c5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c80631ae18a6414602a575b5f80fd5b60406004803603810190603c9190606c565b6042565b005b5f80fd5b5f80fd5b5f80fd5b5f819050826020600202820111156066576065604a565b5b92915050565b5f60408284031215607e57607d6046565b5b5f608984828501604e565b9150509291505056fea26469706673582212207623489a002fb52ee39ece715271c1ed1515fd033f97ca89ccbfbab8a2f493ab64736f6c63430008140033 \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/java/ArrayOfStructClassGeneration.java b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/java/ArrayOfStructClassGeneration.java new file mode 100644 index 000000000..44b0de307 --- /dev/null +++ b/codegen/src/test/resources/solidity/arrayofstructclassgeneration/build/java/ArrayOfStructClassGeneration.java @@ -0,0 +1,138 @@ +package org.web3j.unittests.java; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.web3j.abi.TypeReference; +import org.web3j.abi.datatypes.Bool; +import org.web3j.abi.datatypes.Function; +import org.web3j.abi.datatypes.StaticStruct; +import org.web3j.abi.datatypes.Type; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.RemoteCall; +import org.web3j.protocol.core.RemoteFunctionCall; +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.tx.Contract; +import org.web3j.tx.TransactionManager; +import org.web3j.tx.gas.ContractGasProvider; + +/** + *

Auto generated code. + *

Do not modify! + *

Please use the web3j command line tools, + * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the + * codegen module to update. + * + *

Generated with web3j version none. + */ +@SuppressWarnings("rawtypes") +public class ArrayOfStructClassGeneration extends Contract { + public static final String BINARY = "608060405234801561000f575f80fd5b5060c88061001c5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c80631ae18a6414602a575b5f80fd5b60406004803603810190603c9190606c565b6042565b005b5f80fd5b5f80fd5b5f80fd5b5f819050826020600202820111156066576065604a565b5b92915050565b5f60408284031215607e57607d6046565b5b5f608984828501604e565b9150509291505056fea26469706673582212207623489a002fb52ee39ece715271c1ed1515fd033f97ca89ccbfbab8a2f493ab64736f6c63430008140033"; + + private static String librariesLinkedBinary; + + public static final String FUNC_TEST = "test"; + + @Deprecated + protected ArrayOfStructClassGeneration(String contractAddress, Web3j web3j, + Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + protected ArrayOfStructClassGeneration(String contractAddress, Web3j web3j, + Credentials credentials, ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, credentials, contractGasProvider); + } + + @Deprecated + protected ArrayOfStructClassGeneration(String contractAddress, Web3j web3j, + TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + protected ArrayOfStructClassGeneration(String contractAddress, Web3j web3j, + TransactionManager transactionManager, ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider); + } + + public RemoteFunctionCall test(List foos) { + final Function function = new Function( + FUNC_TEST, + Arrays.asList(new org.web3j.abi.datatypes.generated.StaticArray2(Foo.class, foos)), + Collections.>emptyList()); + return executeRemoteCallTransaction(function); + } + + @Deprecated + public static ArrayOfStructClassGeneration load(String contractAddress, Web3j web3j, + Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + return new ArrayOfStructClassGeneration(contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + @Deprecated + public static ArrayOfStructClassGeneration load(String contractAddress, Web3j web3j, + TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + return new ArrayOfStructClassGeneration(contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + public static ArrayOfStructClassGeneration load(String contractAddress, Web3j web3j, + Credentials credentials, ContractGasProvider contractGasProvider) { + return new ArrayOfStructClassGeneration(contractAddress, web3j, credentials, contractGasProvider); + } + + public static ArrayOfStructClassGeneration load(String contractAddress, Web3j web3j, + TransactionManager transactionManager, ContractGasProvider contractGasProvider) { + return new ArrayOfStructClassGeneration(contractAddress, web3j, transactionManager, contractGasProvider); + } + + public static RemoteCall deploy(Web3j web3j, + Credentials credentials, ContractGasProvider contractGasProvider) { + return deployRemoteCall(ArrayOfStructClassGeneration.class, web3j, credentials, contractGasProvider, getDeploymentBinary(), ""); + } + + public static RemoteCall deploy(Web3j web3j, + TransactionManager transactionManager, ContractGasProvider contractGasProvider) { + return deployRemoteCall(ArrayOfStructClassGeneration.class, web3j, transactionManager, contractGasProvider, getDeploymentBinary(), ""); + } + + @Deprecated + public static RemoteCall deploy(Web3j web3j, + Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + return deployRemoteCall(ArrayOfStructClassGeneration.class, web3j, credentials, gasPrice, gasLimit, getDeploymentBinary(), ""); + } + + @Deprecated + public static RemoteCall deploy(Web3j web3j, + TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + return deployRemoteCall(ArrayOfStructClassGeneration.class, web3j, transactionManager, gasPrice, gasLimit, getDeploymentBinary(), ""); + } + + public static void linkLibraries(List references) { + librariesLinkedBinary = linkBinaryWithReferences(BINARY, references); + } + + private static String getDeploymentBinary() { + if (librariesLinkedBinary != null) { + return librariesLinkedBinary; + } else { + return BINARY; + } + } + + public static class Foo extends StaticStruct { + public Boolean dummy; + + public Foo(Boolean dummy) { + super(new org.web3j.abi.datatypes.Bool(dummy)); + this.dummy = dummy; + } + + public Foo(Bool dummy) { + super(dummy); + this.dummy = dummy.getValue(); + } + } +} +