Java Code Examples for org.web3j.protocol.core.methods.response.AbiDefinition#getName()

The following examples show how to use org.web3j.protocol.core.methods.response.AbiDefinition#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
Iterable<FieldSpec> buildFuncNameConstants(List<AbiDefinition> functionDefinitions) {
    List<FieldSpec> fields = new ArrayList<>();
    Set<String> fieldNames = new HashSet<>();
    fieldNames.add(Contract.FUNC_DEPLOY);

    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getType().equals("function")) {
            String funcName = functionDefinition.getName();

            if (!fieldNames.contains(funcName)) {
                FieldSpec field = FieldSpec.builder(String.class,
                        funcNameToConst(funcName),
                        Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
                        .initializer("$S", funcName)
                        .build();
                fields.add(field);
                fieldNames.add(funcName);
            }
        }
    }
    return fields;
}
 
Example 2
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildFunction(
        AbiDefinition functionDefinition) throws ClassNotFoundException {
    String functionName = functionDefinition.getName();

    MethodSpec.Builder methodBuilder =
            MethodSpec.methodBuilder(functionName)
                    .addModifiers(Modifier.PUBLIC);

    String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());

    List<TypeName> outputParameterTypes = buildTypeNames(functionDefinition.getOutputs());
    if (functionDefinition.isConstant()) {
        buildConstantFunction(
                functionDefinition, methodBuilder, outputParameterTypes, inputParams);
    } else {
        buildTransactionFunction(
                functionDefinition, methodBuilder, inputParams);
    }

    return methodBuilder.build();
}
 
Example 3
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
MethodSpec buildFunction(
        AbiDefinition functionDefinition) throws ClassNotFoundException {
    String functionName = functionDefinition.getName();

    MethodSpec.Builder methodBuilder =
            MethodSpec.methodBuilder(functionName)
                    .addModifiers(Modifier.PUBLIC);

    String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());

    List<TypeName> outputParameterTypes = buildTypeNames(functionDefinition.getOutputs());
    if (functionDefinition.isConstant()) {
        buildConstantFunction(
                functionDefinition, methodBuilder, outputParameterTypes, inputParams);
    } else {
        buildTransactionFunction(
                functionDefinition, methodBuilder, inputParams);
    }

    return methodBuilder.build();
}
 
Example 4
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private void buildTransactionFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        String inputParams) throws ClassNotFoundException {

    if (functionDefinition.hasOutputs()) {
        //CHECKSTYLE:OFF
        reporter.report(String.format(
                "Definition of the function %s returns a value but is not defined as a view function. "
                        + "Please ensure it contains the view modifier if you want to read the return value",
                functionDefinition.getName()));
        //CHECKSTYLE:ON
    }

    if (functionDefinition.isPayable()) {
        methodBuilder.addParameter(BigInteger.class, WEI_VALUE);
    }

    String functionName = functionDefinition.getName();

    methodBuilder.returns(buildRemoteCall(TypeName.get(TransactionReceipt.class)));

    methodBuilder.addStatement("final $T function = new $T(\n$N, \n$T.<$T>asList($L), \n$T"
                    + ".<$T<?>>emptyList())",
            Function.class, Function.class, funcNameToConst(functionName),
            Arrays.class, Type.class, inputParams, Collections.class,
            TypeReference.class);
    if (functionDefinition.isPayable()) {
        methodBuilder.addStatement(
                "return executeRemoteCallTransaction(function, $N)", WEI_VALUE);
    } else {
        methodBuilder.addStatement("return executeRemoteCallTransaction(function)");
    }
}
 
Example 5
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
List<MethodSpec> buildEventFunctions(
        AbiDefinition functionDefinition,
        TypeSpec.Builder classBuilder) throws ClassNotFoundException {
    String functionName = functionDefinition.getName();
    List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs();
    String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";

    List<NamedTypeName> parameters = new ArrayList<>();
    List<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {
        NamedTypeName parameter = new NamedTypeName(
                namedType.getName(),
                buildTypeName(namedType.getType()),
                namedType.isIndexed()
        );
        if (namedType.isIndexed()) {
            indexedParameters.add(parameter);
        } else {
            nonIndexedParameters.add(parameter);
        }
        parameters.add(parameter);
    }

    classBuilder.addField(createEventDefinition(functionName, parameters));

    classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters,
            nonIndexedParameters));

    List<MethodSpec> methods = new ArrayList<>();
    methods.add(buildEventTransactionReceiptFunction(responseClassName,
            functionName, indexedParameters, nonIndexedParameters));

    methods.add(buildEventObservableFunction(responseClassName, functionName,
            indexedParameters, nonIndexedParameters));
    methods.add(buildDefaultEventObservableFunction(responseClassName,
            functionName));
    return methods;
}
 
Example 6
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void buildTransactionFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        String inputParams) throws ClassNotFoundException {

    if (functionDefinition.hasOutputs()) {
        //CHECKSTYLE:OFF
        reporter.report(String.format(
                "Definition of the function %s returns a value but is not defined as a view function. "
                        + "Please ensure it contains the view modifier if you want to read the return value",
                functionDefinition.getName()));
        //CHECKSTYLE:ON
    }

    if (functionDefinition.isPayable()) {
        methodBuilder.addParameter(BigInteger.class, WEI_VALUE);
    }

    String functionName = functionDefinition.getName();

    methodBuilder.returns(buildRemoteCall(TypeName.get(TransactionReceipt.class)));

    methodBuilder.addStatement("final $T function = new $T(\n$S, \n$T.<$T>asList($L), \n$T"
                    + ".<$T<?>>emptyList())",
            Function.class, Function.class, functionName,
            Arrays.class, Type.class, inputParams, Collections.class,
            TypeReference.class);
    if (functionDefinition.isPayable()) {
        methodBuilder.addStatement(
                "return executeRemoteCallTransaction(function, $N)", WEI_VALUE);
    } else {
        methodBuilder.addStatement("return executeRemoteCallTransaction(function)");
    }
}
 
Example 7
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
private Set<String> getDuplicateFunctionNames(List<AbiDefinition> functionDefinitions) {
    Set<String> duplicateNames = new HashSet<>();
    Set<String> functionNames = new HashSet<>();
    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getName() != null
                && TYPE_FUNCTION.equals(functionDefinition.getType())) {
            String functionName = funcNameToConst(functionDefinition.getName(), true);
            if (!functionNames.add(functionName)) {
                duplicateNames.add(functionName);
            }
        }
    }
    return duplicateNames;
}
 
Example 8
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
Iterable<FieldSpec> buildFuncNameConstants(List<AbiDefinition> functionDefinitions) {
    List<FieldSpec> fields = new ArrayList<>();
    Set<String> fieldNames = new HashSet<>();
    fieldNames.add(Contract.FUNC_DEPLOY);
    Set<String> duplicateFunctionNames = getDuplicateFunctionNames(functionDefinitions);
    if (!duplicateFunctionNames.isEmpty()) {
        System.out.println(
                "\nWarning: Duplicate field(s) found: "
                        + duplicateFunctionNames
                        + ". Please don't use names which will be the same in uppercase.");
    }
    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getType().equals(TYPE_FUNCTION)) {
            String funcName = functionDefinition.getName();

            if (!fieldNames.contains(funcName)) {
                boolean useUpperCase =
                        !duplicateFunctionNames.contains(funcNameToConst(funcName, true));
                FieldSpec field =
                        FieldSpec.builder(
                                        String.class,
                                        funcNameToConst(funcName, useUpperCase),
                                        Modifier.PUBLIC,
                                        Modifier.STATIC,
                                        Modifier.FINAL)
                                .initializer("$S", funcName)
                                .build();
                fields.add(field);
                fieldNames.add(funcName);
            }
        }
    }
    return fields;
}
 
Example 9
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private void buildConstantFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        List<TypeName> outputParameterTypes,
        String inputParams) throws ClassNotFoundException {

    String functionName = functionDefinition.getName();

    if (outputParameterTypes.isEmpty()) {
        methodBuilder.addStatement("throw new RuntimeException"
                + "(\"cannot call constant function with void return type\")");
    } else if (outputParameterTypes.size() == 1) {

        TypeName typeName = outputParameterTypes.get(0);
        TypeName nativeReturnTypeName;
        if (useNativeJavaTypes) {
            nativeReturnTypeName = getWrapperRawType(typeName);
        } else {
            nativeReturnTypeName = getWrapperType(typeName);
        }
        methodBuilder.returns(buildRemoteCall(nativeReturnTypeName));

        methodBuilder.addStatement("final $T function = "
                        + "new $T($N, \n$T.<$T>asList($L), "
                        + "\n$T.<$T<?>>asList(new $T<$T>() {}))",
                Function.class, Function.class, funcNameToConst(functionName),
                Arrays.class, Type.class, inputParams,
                Arrays.class, TypeReference.class,
                TypeReference.class, typeName);

        if (useNativeJavaTypes) {
            if (nativeReturnTypeName.equals(ClassName.get(List.class))) {
                // We return list. So all the list elements should
                // also be converted to native types
                TypeName listType = ParameterizedTypeName.get(List.class, Type.class);

                CodeBlock.Builder callCode = CodeBlock.builder();
                callCode.addStatement(
                        "$T result = "
                                + "($T) executeCallSingleValueReturn(function, $T.class)",
                        listType, listType, nativeReturnTypeName);
                callCode.addStatement("return convertToNative(result)");

                TypeSpec callableType = TypeSpec.anonymousClassBuilder("")
                        .addSuperinterface(ParameterizedTypeName.get(
                                ClassName.get(Callable.class), nativeReturnTypeName))
                        .addMethod(MethodSpec.methodBuilder("call")
                                .addAnnotation(Override.class)
                                .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
                                        .addMember("value", "$S", "unchecked")
                                        .build())
                                .addModifiers(Modifier.PUBLIC)
                                .addException(Exception.class)
                                .returns(nativeReturnTypeName)
                                .addCode(callCode.build())
                                .build())
                        .build();

                methodBuilder.addStatement("return new $T(\n$L)",
                        buildRemoteCall(nativeReturnTypeName), callableType);
            } else {
                methodBuilder.addStatement(
                        "return executeRemoteCallSingleValueReturn(function, $T.class)",
                        nativeReturnTypeName);
            }
        } else {
            methodBuilder.addStatement("return executeRemoteCallSingleValueReturn(function)");
        }
    } else {
        List<TypeName> returnTypes = buildReturnTypes(outputParameterTypes);

        ParameterizedTypeName parameterizedTupleType = ParameterizedTypeName.get(
                ClassName.get(
                        "org.web3j.tuples.generated",
                        "Tuple" + returnTypes.size()),
                returnTypes.toArray(
                        new TypeName[returnTypes.size()]));

        methodBuilder.returns(buildRemoteCall(parameterizedTupleType));

        buildVariableLengthReturnFunctionConstructor(
                methodBuilder, functionName, inputParams, outputParameterTypes);

        buildTupleResultContainer(methodBuilder, parameterizedTupleType, outputParameterTypes);
    }
}
 
Example 10
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
private void buildConstantFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        List<TypeName> outputParameterTypes,
        String inputParams) throws ClassNotFoundException {

    String functionName = functionDefinition.getName();

    if (outputParameterTypes.isEmpty()) {
        throw new RuntimeException("Only transactional methods should have void return types");
    } else if (outputParameterTypes.size() == 1) {

        TypeName typeName = outputParameterTypes.get(0);
        TypeName nativeReturnTypeName;
        if (useNativeJavaTypes) {
            nativeReturnTypeName = getWrapperRawType(typeName);
        } else {
            nativeReturnTypeName = getWrapperType(typeName);
        }
        methodBuilder.returns(buildRemoteCall(nativeReturnTypeName));

        methodBuilder.addStatement("final $T function = "
                        + "new $T($S, \n$T.<$T>asList($L), "
                        + "\n$T.<$T<?>>asList(new $T<$T>() {}))",
                Function.class, Function.class, functionName,
                Arrays.class, Type.class, inputParams,
                Arrays.class, TypeReference.class,
                TypeReference.class, typeName);

        if (useNativeJavaTypes) {
            if (nativeReturnTypeName.equals(ClassName.get(List.class))) {
                // We return list. So all the list elements should
                // also be converted to native types
                TypeName listType = ParameterizedTypeName.get(List.class, Type.class);

                CodeBlock.Builder callCode = CodeBlock.builder();
                callCode.addStatement(
                        "$T result = "
                        + "($T) executeCallSingleValueReturn(function, $T.class)",
                        listType, listType, nativeReturnTypeName);
                callCode.addStatement("return convertToNative(result)");

                TypeSpec callableType = TypeSpec.anonymousClassBuilder("")
                        .addSuperinterface(ParameterizedTypeName.get(
                                ClassName.get(Callable.class), nativeReturnTypeName))
                        .addMethod(MethodSpec.methodBuilder("call")
                                .addAnnotation(Override.class)
                                .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
                                        .addMember("value", "$S", "unchecked")
                                        .build())
                                .addModifiers(Modifier.PUBLIC)
                                .addException(Exception.class)
                                .returns(nativeReturnTypeName)
                                .addCode(callCode.build())
                                .build())
                        .build();

                methodBuilder.addStatement("return new $T(\n$L)",
                        buildRemoteCall(nativeReturnTypeName), callableType);
            } else {
                methodBuilder.addStatement(
                        "return executeRemoteCallSingleValueReturn(function, $T.class)",
                        nativeReturnTypeName);
            }
        } else {
            methodBuilder.addStatement("return executeRemoteCallSingleValueReturn(function)");
        }
    } else {
        List<TypeName> returnTypes = buildReturnTypes(outputParameterTypes);

        ParameterizedTypeName parameterizedTupleType = ParameterizedTypeName.get(
                ClassName.get(
                        "org.web3j.tuples.generated",
                        "Tuple" + returnTypes.size()),
                returnTypes.toArray(
                        new TypeName[returnTypes.size()]));

        methodBuilder.returns(buildRemoteCall(parameterizedTupleType));

        buildVariableLengthReturnFunctionConstructor(
                methodBuilder, functionName, inputParams, outputParameterTypes);

        buildTupleResultContainer(methodBuilder, parameterizedTupleType, outputParameterTypes);
    }
}
 
Example 11
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
void buildEventFunctions(
        AbiDefinition functionDefinition,
        TypeSpec.Builder classBuilder) throws ClassNotFoundException {

    String functionName = functionDefinition.getName();
    List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs();
    String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";

    List<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {

        if (namedType.isIndexed()) {
            indexedParameters.add(
                    new org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName(
                            namedType.getName(),
                            buildTypeName(namedType.getType())));
        } else {
            nonIndexedParameters.add(
                    new org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName(
                            namedType.getName(),
                            buildTypeName(namedType.getType())));
        }
    }

    classBuilder.addField(createEventDefinition(functionName,
            indexedParameters, nonIndexedParameters));

    classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters,
            nonIndexedParameters));

    classBuilder.addMethod(buildEventTransactionReceiptFunction(responseClassName,
            functionName, indexedParameters, nonIndexedParameters));

    classBuilder.addMethod(buildEventObservableFunction(responseClassName, functionName,
            indexedParameters, nonIndexedParameters));

    classBuilder.addMethod(buildDefaultEventObservableFunction(responseClassName,
            functionName));
}
 
Example 12
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
List<MethodSpec> buildFunctions(AbiDefinition functionDefinition, boolean useUpperCase)
        throws ClassNotFoundException {

    List<MethodSpec> results = new ArrayList<>(2);
    String functionName = functionDefinition.getName();

    String stateMutability = functionDefinition.getStateMutability();
    boolean pureOrView = "pure".equals(stateMutability) || "view".equals(stateMutability);
    boolean isFunctionDefinitionConstant = functionDefinition.isConstant() || pureOrView;

    if (generateSendTxForCalls) {
        final String funcNamePrefix;
        if (isFunctionDefinitionConstant) {
            funcNamePrefix = "call";
        } else {
            funcNamePrefix = "send";
        }
        // Prefix function name to avoid naming collision
        functionName = funcNamePrefix + "_" + functionName;
    } else {
        // If the solidity function name is a reserved word
        // in the current java version prepend it with "_"
        if (!SourceVersion.isName(functionName)) {
            functionName = "_" + functionName;
        }
    }

    MethodSpec.Builder methodBuilder =
            MethodSpec.methodBuilder(functionName).addModifiers(Modifier.PUBLIC);

    final String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());
    final List<TypeName> outputParameterTypes =
            buildTypeNames(functionDefinition.getOutputs(), useJavaPrimitiveTypes);

    if (isFunctionDefinitionConstant) {
        // Avoid generating runtime exception call
        if (functionDefinition.hasOutputs()) {
            buildConstantFunction(
                    functionDefinition,
                    methodBuilder,
                    outputParameterTypes,
                    inputParams,
                    useUpperCase);

            results.add(methodBuilder.build());
        }
        if (generateSendTxForCalls) {
            AbiDefinition sendFuncDefinition = new AbiDefinition(functionDefinition);
            sendFuncDefinition.setConstant(false);
            results.addAll(buildFunctions(sendFuncDefinition));
        }
    }

    if (!isFunctionDefinitionConstant) {
        buildTransactionFunction(functionDefinition, methodBuilder, inputParams, useUpperCase);
        results.add(methodBuilder.build());
    }

    return results;
}
 
Example 13
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
private void buildTransactionFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        String inputParams,
        boolean useUpperCase)
        throws ClassNotFoundException {

    if (functionDefinition.hasOutputs()) {
        reporter.report(
                String.format(
                        "Definition of the function %s returns a value but is not defined as a view function. "
                                + "Please ensure it contains the view modifier if you want to read the return value",
                        functionDefinition.getName()));
    }

    if (functionDefinition.isPayable()) {
        methodBuilder.addParameter(BigInteger.class, WEI_VALUE);
    }

    String functionName = functionDefinition.getName();

    methodBuilder.returns(buildRemoteFunctionCall(TypeName.get(TransactionReceipt.class)));

    methodBuilder.addStatement(
            "final $T function = new $T(\n$N, \n$T.<$T>asList($L), \n$T"
                    + ".<$T<?>>emptyList())",
            Function.class,
            Function.class,
            funcNameToConst(functionName, useUpperCase),
            Arrays.class,
            Type.class,
            inputParams,
            Collections.class,
            TypeReference.class);
    if (functionDefinition.isPayable()) {
        methodBuilder.addStatement(
                "return executeRemoteCallTransaction(function, $N)", WEI_VALUE);
    } else {
        methodBuilder.addStatement("return executeRemoteCallTransaction(function)");
    }
}
 
Example 14
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
List<MethodSpec> buildEventFunctions(
        AbiDefinition functionDefinition, TypeSpec.Builder classBuilder)
        throws ClassNotFoundException {
    String functionName = functionDefinition.getName();
    List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs();
    String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";

    List<NamedTypeName> parameters = new ArrayList<>();
    List<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {
        final TypeName typeName;
        if (namedType.getType().equals("tuple")) {
            typeName = structClassNameMap.get(namedType.structIdentifier());
        } else {
            typeName = buildTypeName(namedType.getType(), useJavaPrimitiveTypes);
        }
        NamedTypeName parameter = new NamedTypeName(namedType, typeName);
        if (namedType.isIndexed()) {
            indexedParameters.add(parameter);
        } else {
            nonIndexedParameters.add(parameter);
        }
        parameters.add(parameter);
    }

    classBuilder.addField(createEventDefinition(functionName, parameters));

    classBuilder.addType(
            buildEventResponseObject(
                    responseClassName, indexedParameters, nonIndexedParameters));

    List<MethodSpec> methods = new ArrayList<>();
    methods.add(
            buildEventTransactionReceiptFunction(
                    responseClassName, functionName, indexedParameters, nonIndexedParameters));

    methods.add(
            buildEventFlowableFunction(
                    responseClassName, functionName, indexedParameters, nonIndexedParameters));
    methods.add(buildDefaultEventFlowableFunction(responseClassName, functionName));
    return methods;
}