From 8d9e8ee7840ff06a5f468c16ccf4f3f095a7f0e2 Mon Sep 17 00:00:00 2001 From: tonykwok1992 Date: Mon, 22 Jul 2024 20:14:08 +0800 Subject: [PATCH] Make getABI_* calls static (#2079) Signed-off-by: tonykwok1992 --- .../codegen/SolidityFunctionWrapper.java | 2 +- .../SolidityFunctionWrapperGeneratorTest.java | 31 +++++-- .../resources/solidity/abifuncs/AbiFuncs.sol | 10 ++ .../solidity/abifuncs/build/AbiFuncs.abi | 1 + .../solidity/abifuncs/build/AbiFuncs.bin | 1 + .../abifuncs/build/java/AbiFuncs.java | 91 +++++++++++++++++++ 6 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 codegen/src/test/resources/solidity/abifuncs/AbiFuncs.sol create mode 100644 codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.abi create mode 100644 codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.bin create mode 100644 codegen/src/test/resources/solidity/abifuncs/build/java/AbiFuncs.java diff --git a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index 0559fca03..1db0b3380 100644 --- a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -1506,7 +1506,7 @@ List buildFunctions( // Create function that returns the ABI encoding of the Solidity function call. if (abiFuncs) { functionName = "getABI_" + functionName; - methodBuilder = MethodSpec.methodBuilder(functionName).addModifiers(Modifier.PUBLIC); + methodBuilder = MethodSpec.methodBuilder(functionName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC); addParameters(methodBuilder, functionDefinition.getInputs()); buildAbiFunction(functionDefinition, methodBuilder, inputParams, useUpperCase); results.add(methodBuilder.build()); diff --git a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java index ddbefe684..7b8d15ab2 100644 --- a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java +++ b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java @@ -60,6 +60,17 @@ public void testGetFileNoExtension() { assertEquals("file", getFileNameNoExtension("file.txt")); } + @Test + public void testAbiFuncsGeneration() throws Exception { + testCodeGeneration(emptyList(),"abifuncs", "AbiFuncs", JAVA_TYPES_ARG, true, false, true); + testCodeGeneration(emptyList(),"abifuncs", "AbiFuncs", SOLIDITY_TYPES_ARG, true, false, true); + } + + @Test + public void testAbiFuncsCompareJavaFileTest() throws Exception { + compareJavaFile("AbiFuncs", false, true); + } + @Test public void testGreeterGeneration() throws Exception { testCodeGenerationJvmTypes("greeter", "Greeter"); @@ -143,12 +154,12 @@ public void testStructOnlyInArray() throws Exception { @Test public void testStructOnlyInArrayCompareJavaFile() throws Exception { - compareJavaFile("OnlyInArrayStruct", false); + compareJavaFile("OnlyInArrayStruct", false, false); } @Test public void testArraysInStructCompareJavaFileTest() throws Exception { - compareJavaFile("ArraysInStruct", false); + compareJavaFile("ArraysInStruct", false, false); } @Test @@ -199,12 +210,12 @@ public void testEventParametersNoNamed() throws Exception { @Test public void testEventParametersNoNamedCompareJavaFile() throws Exception { - compareJavaFile("EventParameters", false); + compareJavaFile("EventParameters", false, false); } @Test public void testDeployMethodGenerated() throws Exception { - compareJavaFile("MetaCoin", true); + compareJavaFile("MetaCoin", true, false); } @Test @@ -215,7 +226,7 @@ public void testSameInnerStructName() throws Exception { @Test public void testSameInnerStructNameCompareJavaFile() throws Exception { - compareJavaFile("SameInnerStructName", true); + compareJavaFile("SameInnerStructName", true, false); } @Test @@ -229,7 +240,7 @@ public void testArrayOfStructClassGeneration() throws Exception { @Test public void testArrayOfStructClassGenerationCompareJavaFile() throws Exception { - compareJavaFile("ArrayOfStructClassGeneration", true); + compareJavaFile("ArrayOfStructClassGeneration", true, false); } @Test @@ -240,7 +251,7 @@ public void testArrayOfStructAndStructGeneration() throws Exception { @Test public void testArrayOfStructAndStructCompareJavaFile() throws Exception { - compareJavaFile("ArrayOfStructAndStruct", true); + compareJavaFile("ArrayOfStructAndStruct", true, false); } @Test @@ -251,14 +262,14 @@ public void testStaticArrayOfStructsInStructGeneration() throws Exception { @Test public void testStaticArrayOfStructsInStructGenerationCompareJavaFile() throws Exception { - compareJavaFile("StaticArrayOfStructsInStruct", true); + compareJavaFile("StaticArrayOfStructsInStruct", true, false); } - private void compareJavaFile(String inputFileName, boolean useBin) throws Exception { + private void compareJavaFile(String inputFileName, boolean useBin, boolean abiFuncs) throws Exception { String contract = inputFileName.toLowerCase(); String packagePath = generateCode( - emptyList(), contract, inputFileName, JAVA_TYPES_ARG, useBin, false, false); + emptyList(), contract, inputFileName, JAVA_TYPES_ARG, useBin, false, abiFuncs); File fileActual = new File(tempDirPath, packagePath + "/" + inputFileName + ".java"); File fileExpected = new File( diff --git a/codegen/src/test/resources/solidity/abifuncs/AbiFuncs.sol b/codegen/src/test/resources/solidity/abifuncs/AbiFuncs.sol new file mode 100644 index 000000000..ca224e784 --- /dev/null +++ b/codegen/src/test/resources/solidity/abifuncs/AbiFuncs.sol @@ -0,0 +1,10 @@ +pragma solidity >= 0.8.7; + +contract AbiFuncs { + + + function hi(uint256 x) public { + + } + +} \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.abi b/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.abi new file mode 100644 index 000000000..03bfee70e --- /dev/null +++ b/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.abi @@ -0,0 +1 @@ +[{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"hi","outputs":[],"stateMutability":"nonpayable","type":"function"}] \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.bin b/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.bin new file mode 100644 index 000000000..53a3eabb1 --- /dev/null +++ b/codegen/src/test/resources/solidity/abifuncs/build/AbiFuncs.bin @@ -0,0 +1 @@ +608060405234801561000f575f80fd5b5060d38061001c5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c80634cf1b41d14602a575b5f80fd5b60406004803603810190603c91906077565b6042565b005b50565b5f80fd5b5f819050919050565b6059816049565b81146062575f80fd5b50565b5f813590506071816052565b92915050565b5f6020828403121560895760886045565b5b5f6094848285016065565b9150509291505056fea2646970667358221220e788c40f4c54add124e8c2662f9bc92e122a1ea722846a0a855e1ffb68cbdac564736f6c63430008140033 \ No newline at end of file diff --git a/codegen/src/test/resources/solidity/abifuncs/build/java/AbiFuncs.java b/codegen/src/test/resources/solidity/abifuncs/build/java/AbiFuncs.java new file mode 100644 index 000000000..b2e8120b7 --- /dev/null +++ b/codegen/src/test/resources/solidity/abifuncs/build/java/AbiFuncs.java @@ -0,0 +1,91 @@ +package org.web3j.unittests.java; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import org.web3j.abi.TypeReference; +import org.web3j.abi.datatypes.Function; +import org.web3j.abi.datatypes.Type; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +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 AbiFuncs extends Contract { + public static final String BINARY = "Bin file was not provided"; + + public static final String FUNC_HI = "hi"; + + @Deprecated + protected AbiFuncs(String contractAddress, Web3j web3j, Credentials credentials, + BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + protected AbiFuncs(String contractAddress, Web3j web3j, Credentials credentials, + ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, credentials, contractGasProvider); + } + + @Deprecated + protected AbiFuncs(String contractAddress, Web3j web3j, TransactionManager transactionManager, + BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + protected AbiFuncs(String contractAddress, Web3j web3j, TransactionManager transactionManager, + ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider); + } + + public RemoteFunctionCall hi(BigInteger x) { + final Function function = new Function( + FUNC_HI, + Arrays.asList(new org.web3j.abi.datatypes.generated.Uint256(x)), + Collections.>emptyList()); + return executeRemoteCallTransaction(function); + } + + public static String getABI_hi(BigInteger x) { + final Function function = new Function( + FUNC_HI, + Arrays.asList(new org.web3j.abi.datatypes.generated.Uint256(x)), + Collections.>emptyList()); + return org.web3j.abi.FunctionEncoder.encode(function); + } + + @Deprecated + public static AbiFuncs load(String contractAddress, Web3j web3j, Credentials credentials, + BigInteger gasPrice, BigInteger gasLimit) { + return new AbiFuncs(contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + @Deprecated + public static AbiFuncs load(String contractAddress, Web3j web3j, + TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + return new AbiFuncs(contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + public static AbiFuncs load(String contractAddress, Web3j web3j, Credentials credentials, + ContractGasProvider contractGasProvider) { + return new AbiFuncs(contractAddress, web3j, credentials, contractGasProvider); + } + + public static AbiFuncs load(String contractAddress, Web3j web3j, + TransactionManager transactionManager, ContractGasProvider contractGasProvider) { + return new AbiFuncs(contractAddress, web3j, transactionManager, contractGasProvider); + } +}