Skip to content

Commit

Permalink
Make getABI_* calls static (#2079)
Browse files Browse the repository at this point in the history
Signed-off-by: tonykwok1992 <[email protected]>
  • Loading branch information
tonykwok1992 authored Jul 22, 2024
1 parent 45540b9 commit 8d9e8ee
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ List<MethodSpec> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -215,7 +226,7 @@ public void testSameInnerStructName() throws Exception {

@Test
public void testSameInnerStructNameCompareJavaFile() throws Exception {
compareJavaFile("SameInnerStructName", true);
compareJavaFile("SameInnerStructName", true, false);
}

@Test
Expand All @@ -229,7 +240,7 @@ public void testArrayOfStructClassGeneration() throws Exception {

@Test
public void testArrayOfStructClassGenerationCompareJavaFile() throws Exception {
compareJavaFile("ArrayOfStructClassGeneration", true);
compareJavaFile("ArrayOfStructClassGeneration", true, false);
}

@Test
Expand All @@ -240,7 +251,7 @@ public void testArrayOfStructAndStructGeneration() throws Exception {

@Test
public void testArrayOfStructAndStructCompareJavaFile() throws Exception {
compareJavaFile("ArrayOfStructAndStruct", true);
compareJavaFile("ArrayOfStructAndStruct", true, false);
}

@Test
Expand All @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions codegen/src/test/resources/solidity/abifuncs/AbiFuncs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pragma solidity >= 0.8.7;

contract AbiFuncs {


function hi(uint256 x) public {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"hi","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561000f575f80fd5b5060d38061001c5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c80634cf1b41d14602a575b5f80fd5b60406004803603810190603c91906077565b6042565b005b50565b5f80fd5b5f819050919050565b6059816049565b81146062575f80fd5b50565b5f813590506071816052565b92915050565b5f6020828403121560895760886045565b5b5f6094848285016065565b9150509291505056fea2646970667358221220e788c40f4c54add124e8c2662f9bc92e122a1ea722846a0a855e1ffb68cbdac564736f6c63430008140033
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/main/codegen">codegen module</a> to update.
*
* <p>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<TransactionReceipt> hi(BigInteger x) {
final Function function = new Function(
FUNC_HI,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(x)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public static String getABI_hi(BigInteger x) {
final Function function = new Function(
FUNC_HI,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(x)),
Collections.<TypeReference<?>>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);
}
}

0 comments on commit 8d9e8ee

Please sign in to comment.