Java Code Examples for org.web3j.protocol.core.methods.response.AbiDefinition#NamedType

The following examples show how to use org.web3j.protocol.core.methods.response.AbiDefinition#NamedType . 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 web3j with Apache License 2.0 6 votes vote down vote up
private String buildStructConstructorParameterDefinition(
        final List<AbiDefinition.NamedType> components, final boolean useNativeJavaTypes)
        throws ClassNotFoundException {
    final StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < components.size(); i++) {
        final AbiDefinition.NamedType component = components.get(i);
        stringBuilder.append(i > 0 ? "," : "");
        stringBuilder.append(
                (!component.getType().equals("tuple") && useNativeJavaTypes
                        ? "new " + buildTypeName(component.getType(), false) + "("
                        : ""));
        stringBuilder.append(
                (component.getType().equals("tuple")
                        ? component.getName()
                        : useNativeJavaTypes
                                ? component.getName() + ")"
                                : component.getName()));
    }
    return stringBuilder.toString();
}
 
Example 2
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 6 votes vote down vote up
private java.util.Collection<? extends AbiDefinition.NamedType> extractNested(
        final AbiDefinition.NamedType namedType) {
    if (namedType.getComponents().size() == 0) {
        return new ArrayList<>();
    } else {
        List<AbiDefinition.NamedType> nestedStructs = new ArrayList<>();
        namedType
                .getComponents()
                .forEach(
                        nestedNamedStruct -> {
                            nestedStructs.add(nestedNamedStruct);
                            nestedStructs.addAll(extractNested(nestedNamedStruct));
                        });
        return nestedStructs;
    }
}
 
Example 3
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 6 votes vote down vote up
private List<ParameterSpec> buildParameterTypes(
        List<AbiDefinition.NamedType> namedTypes, boolean primitives)
        throws ClassNotFoundException {

    List<ParameterSpec> result = new ArrayList<>(namedTypes.size());
    for (int i = 0; i < namedTypes.size(); i++) {
        AbiDefinition.NamedType namedType = namedTypes.get(i);

        String name = createValidParamName(namedType.getName(), i);
        String type = namedTypes.get(i).getType();

        if (type.startsWith("tuple")) {
            result.add(
                    ParameterSpec.builder(
                                    structClassNameMap.get(namedType.structIdentifier()), name)
                            .build());
        } else {
            result.add(ParameterSpec.builder(buildTypeName(type, primitives), name).build());
        }
    }
    return result;
}
 
Example 4
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static List<ParameterSpec> buildParameterTypes(List<AbiDefinition.NamedType> namedTypes) {
    List<ParameterSpec> result = new ArrayList<>(namedTypes.size());
    for (int i = 0; i < namedTypes.size(); i++) {
        AbiDefinition.NamedType namedType = namedTypes.get(i);

        String name = createValidParamName(namedType.getName(), i);
        String type = namedTypes.get(i).getType();

        result.add(ParameterSpec.builder(buildTypeName(type), name).build());
    }
    return result;
}
 
Example 5
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static List<TypeName> buildTypeNames(List<AbiDefinition.NamedType> namedTypes) {
    List<TypeName> result = new ArrayList<>(namedTypes.size());
    for (AbiDefinition.NamedType namedType : namedTypes) {
        result.add(buildTypeName(namedType.getType()));
    }
    return result;
}
 
Example 6
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 7
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
String addParameters(
        MethodSpec.Builder methodBuilder, List<AbiDefinition.NamedType> namedTypes) {

    List<ParameterSpec> inputParameterTypes = buildParameterTypes(namedTypes);

    List<ParameterSpec> nativeInputParameterTypes =
            new ArrayList<>(inputParameterTypes.size());
    for (ParameterSpec parameterSpec : inputParameterTypes) {
        TypeName typeName = getWrapperType(parameterSpec.type);
        nativeInputParameterTypes.add(
                ParameterSpec.builder(typeName, parameterSpec.name).build());
    }

    methodBuilder.addParameters(nativeInputParameterTypes);

    if (useNativeJavaTypes) {
        return Collection.join(
                inputParameterTypes,
                ", \n",
                // this results in fully qualified names being generated
                this::createMappedParameterTypes);
    } else {
        return Collection.join(
                inputParameterTypes,
                ", ",
                parameterSpec -> parameterSpec.name);
    }
}
 
Example 8
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
String addParameters(
        MethodSpec.Builder methodBuilder, List<AbiDefinition.NamedType> namedTypes) {

    List<ParameterSpec> inputParameterTypes = buildParameterTypes(namedTypes);

    List<ParameterSpec> nativeInputParameterTypes =
            new ArrayList<>(inputParameterTypes.size());
    for (ParameterSpec parameterSpec : inputParameterTypes) {
        TypeName typeName = getWrapperType(parameterSpec.type);
        nativeInputParameterTypes.add(
                ParameterSpec.builder(typeName, parameterSpec.name).build());
    }

    methodBuilder.addParameters(nativeInputParameterTypes);

    if (useNativeJavaTypes) {
        return Collection.join(
                inputParameterTypes,
                ", \n",
                // this results in fully qualified names being generated
                this::createMappedParameterTypes);
    } else {
        return Collection.join(
                inputParameterTypes,
                ", ",
                parameterSpec -> parameterSpec.name);
    }
}
 
Example 9
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static List<ParameterSpec> buildParameterTypes(List<AbiDefinition.NamedType> namedTypes) {
    List<ParameterSpec> result = new ArrayList<>(namedTypes.size());
    for (int i = 0; i < namedTypes.size(); i++) {
        AbiDefinition.NamedType namedType = namedTypes.get(i);

        String name = createValidParamName(namedType.getName(), i);
        String type = namedTypes.get(i).getType();

        result.add(ParameterSpec.builder(buildTypeName(type), name).build());
    }
    return result;
}
 
Example 10
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static List<TypeName> buildTypeNames(List<AbiDefinition.NamedType> namedTypes) {
    List<TypeName> result = new ArrayList<>(namedTypes.size());
    for (AbiDefinition.NamedType namedType : namedTypes) {
        result.add(buildTypeName(namedType.getType()));
    }
    return result;
}
 
Example 11
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
private List<TypeName> buildTypeNames(
        List<AbiDefinition.NamedType> namedTypes, boolean primitives)
        throws ClassNotFoundException {

    List<TypeName> result = new ArrayList<>(namedTypes.size());
    for (AbiDefinition.NamedType namedType : namedTypes) {
        if (namedType.getType().equals("tuple")) {
            result.add(structClassNameMap.get(namedType.structIdentifier()));
        } else {
            result.add(buildTypeName(namedType.getType(), primitives));
        }
    }
    return result;
}
 
Example 12
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
@NotNull
private List<AbiDefinition.NamedType> extractStructs(
        final List<AbiDefinition> functionDefinitions) {
    final HashMap<Integer, AbiDefinition.NamedType> structMap = new LinkedHashMap<>();
    functionDefinitions.stream()
            .flatMap(
                    definition -> {
                        List<AbiDefinition.NamedType> parameters = new ArrayList<>();
                        parameters.addAll(definition.getInputs());
                        parameters.addAll(definition.getOutputs());
                        return parameters.stream()
                                .filter(namedType -> namedType.getType().equals("tuple"));
                    })
            .forEach(
                    namedType -> {
                        structMap.put(namedType.structIdentifier(), namedType);
                        extractNested(namedType).stream()
                                .filter(
                                        nestedNamedStruct ->
                                                nestedNamedStruct.getType().equals("tuple"))
                                .forEach(
                                        nestedNamedType ->
                                                structMap.put(
                                                        nestedNamedType.structIdentifier(),
                                                        nestedNamedType));
                    });

    return structMap.values().stream()
            .sorted(Comparator.comparingInt(AbiDefinition.NamedType::nestedness))
            .collect(Collectors.toList());
}
 
Example 13
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
String addParameters(MethodSpec.Builder methodBuilder, List<AbiDefinition.NamedType> namedTypes)
        throws ClassNotFoundException {

    final List<ParameterSpec> inputParameterTypes =
            buildParameterTypes(namedTypes, useJavaPrimitiveTypes);

    final List<ParameterSpec> nativeInputParameterTypes =
            new ArrayList<>(inputParameterTypes.size());

    for (int i = 0; i < inputParameterTypes.size(); ++i) {
        final TypeName typeName;
        if (namedTypes.get(i).getType().equals("tuple")) {
            typeName = structClassNameMap.get(namedTypes.get(i).structIdentifier());
        } else {
            typeName = getWrapperType(inputParameterTypes.get(i).type);
        }
        nativeInputParameterTypes.add(
                ParameterSpec.builder(typeName, inputParameterTypes.get(i).name).build());
    }

    methodBuilder.addParameters(nativeInputParameterTypes);

    if (useNativeJavaTypes) {
        return Collection.join(
                inputParameterTypes,
                ", \n",
                // this results in fully qualified names being generated
                this::createMappedParameterTypes);
    } else {
        return Collection.join(inputParameterTypes, ", ", parameterSpec -> parameterSpec.name);
    }
}
 
Example 14
Source File: SolidityFunctionWrapperTest.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildEventConstantMultipleValueReturn() throws Exception {

    AbiDefinition.NamedType id = new AbiDefinition.NamedType("id", "string", true);
    AbiDefinition.NamedType fromAddress = new AbiDefinition.NamedType("from", "address");
    AbiDefinition.NamedType toAddress = new AbiDefinition.NamedType("to", "address");
    AbiDefinition.NamedType value = new AbiDefinition.NamedType("value", "uint256");
    AbiDefinition.NamedType message = new AbiDefinition.NamedType("message", "string");
    fromAddress.setIndexed(true);
    toAddress.setIndexed(true);

    AbiDefinition functionDefinition =
            new AbiDefinition(
                    false,
                    Arrays.asList(id, fromAddress, toAddress, value, message),
                    "Transfer",
                    new ArrayList<>(),
                    "event",
                    false);
    TypeSpec.Builder builder = TypeSpec.classBuilder("testClass");

    builder.addMethods(
            solidityFunctionWrapper.buildEventFunctions(functionDefinition, builder));

    String expected =
            "class testClass {\n"
                    + "  public static final org.web3j.abi.datatypes.Event TRANSFER_EVENT = new org.web3j.abi.datatypes.Event(\"Transfer\", \n"
                    + "      java.util.Arrays.<org.web3j.abi.TypeReference<?>>asList(new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.generated.Uint256>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>() {}));\n  ;\n\n"
                    + "  public java.util.List<TransferEventResponse> getTransferEvents(org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceipt) {\n"
                    + "    java.util.List<org.web3j.tx.Contract.EventValuesWithLog> valueList = extractEventParametersWithLog(TRANSFER_EVENT, transactionReceipt);\n"
                    + "    java.util.ArrayList<TransferEventResponse> responses = new java.util.ArrayList<TransferEventResponse>(valueList.size());\n"
                    + "    for (org.web3j.tx.Contract.EventValuesWithLog eventValues : valueList) {\n"
                    + "      TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "      typedResponse.log = eventValues.getLog();\n"
                    + "      typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "      typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "      typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "      responses.add(typedResponse);\n"
                    + "    }\n"
                    + "    return responses;\n"
                    + "  }\n"
                    + "\n"
                    + "  public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(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, TransferEventResponse>() {\n"
                    + "      @java.lang.Override\n"
                    + "      public TransferEventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
                    + "        org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
                    + "        TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "        typedResponse.log = log;\n"
                    + "        typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "        typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "        typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "        return typedResponse;\n"
                    + "      }\n"
                    + "    });\n"
                    + "  }\n"
                    + "\n"
                    + "  public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(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(TRANSFER_EVENT));\n"
                    + "    return transferEventFlowable(filter);\n"
                    + "  }\n"
                    + "\n"
                    + "  public static class TransferEventResponse extends org.web3j.protocol.core.methods.response.BaseEventResponse {\n"
                    + "    public byte[] id;\n"
                    + "\n"
                    + "    public java.lang.String from;\n"
                    + "\n"
                    + "    public java.lang.String to;\n"
                    + "\n"
                    + "    public java.math.BigInteger value;\n"
                    + "\n"
                    + "    public java.lang.String message;\n"
                    + "  }\n"
                    + "}\n";

    assertEquals(builder.build().toString(), (expected));
}
 
Example 15
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
NamedTypeName(AbiDefinition.NamedType namedType, TypeName typeName) {
    this.namedType = namedType;
    this.typeName = typeName;
}
 
Example 16
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;
}
 
Example 17
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
private List<TypeSpec> buildStructTypes(final List<AbiDefinition> functionDefinitions)
        throws ClassNotFoundException {
    final List<AbiDefinition.NamedType> orderedKeys = extractStructs(functionDefinitions);
    int structCounter = 0;
    final List<TypeSpec> structs = new ArrayList<>();
    for (final AbiDefinition.NamedType namedType : orderedKeys) {
        final String internalType = namedType.getInternalType();
        final String structName;
        if (internalType == null || internalType.isEmpty()) {
            structName = "Struct" + structCounter;
        } else {
            structName = internalType.substring(internalType.lastIndexOf(".") + 1);
        }

        final TypeSpec.Builder builder =
                TypeSpec.classBuilder(structName)
                        .addModifiers(Modifier.PUBLIC, Modifier.STATIC);

        final MethodSpec.Builder constructorBuilder =
                MethodSpec.constructorBuilder()
                        .addModifiers(Modifier.PUBLIC)
                        .addStatement(
                                "super("
                                        + buildStructConstructorParameterDefinition(
                                                namedType.getComponents(), useNativeJavaTypes)
                                        + ")");

        final MethodSpec.Builder nativeConstructorBuilder =
                MethodSpec.constructorBuilder()
                        .addModifiers(Modifier.PUBLIC)
                        .addStatement(
                                "super("
                                        + buildStructConstructorParameterDefinition(
                                                namedType.getComponents(), false)
                                        + ")");

        for (AbiDefinition.NamedType component : namedType.getComponents()) {
            if (component.getType().equals("tuple")) {
                final ClassName typeName = structClassNameMap.get(component.structIdentifier());
                builder.addField(typeName, component.getName(), Modifier.PUBLIC);
                constructorBuilder.addParameter(typeName, component.getName());
                nativeConstructorBuilder.addParameter(typeName, component.getName());

            } else {
                final TypeName nativeTypeName =
                        buildTypeName(component.getType(), useJavaPrimitiveTypes);
                final TypeName wrappedTypeName = getWrapperType(nativeTypeName);
                builder.addField(wrappedTypeName, component.getName(), Modifier.PUBLIC);
                constructorBuilder.addParameter(wrappedTypeName, component.getName());
                nativeConstructorBuilder.addParameter(nativeTypeName, component.getName());
            }
            constructorBuilder.addStatement(
                    "this." + component.getName() + " = " + component.getName());
            nativeConstructorBuilder.addStatement(
                    "this."
                            + component.getName()
                            + " = "
                            + component.getName()
                            + (useNativeJavaTypes
                                            && structClassNameMap.keySet().stream()
                                                    .noneMatch(
                                                            i ->
                                                                    i
                                                                            == component
                                                                                    .structIdentifier())
                                    ? ".getValue()"
                                    : ""));
        }

        builder.superclass(namedType.isDynamic() ? DynamicStruct.class : StaticStruct.class);
        builder.addMethod(constructorBuilder.build());
        if (useNativeJavaTypes
                && !namedType.getComponents().isEmpty()
                && namedType.getComponents().stream()
                        .anyMatch(
                                component ->
                                        structClassNameMap.keySet().stream()
                                                .noneMatch(
                                                        i ->
                                                                i
                                                                        == component
                                                                                .structIdentifier()))) {
            builder.addMethod(nativeConstructorBuilder.build());
        }
        structClassNameMap.put(namedType.structIdentifier(), ClassName.get("", structName));
        structs.add(builder.build());
        structCounter++;
    }
    return structs;
}
 
Example 18
Source File: SolidityFunctionWrapperTest.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testBuildEventConstantMultipleValueReturn() throws Exception {

    AbiDefinition.NamedType id = new AbiDefinition.NamedType("id", "string", true);
    AbiDefinition.NamedType fromAddress = new AbiDefinition.NamedType("from", "address");
    AbiDefinition.NamedType toAddress = new AbiDefinition.NamedType("to", "address");
    AbiDefinition.NamedType value = new AbiDefinition.NamedType("value", "uint256");
    AbiDefinition.NamedType message = new AbiDefinition.NamedType("message", "string");
    fromAddress.setIndexed(true);
    toAddress.setIndexed(true);

    AbiDefinition functionDefinition = new AbiDefinition(
            false,
            Arrays.asList(id, fromAddress, toAddress, value, message),
            "Transfer",
            new ArrayList<>(),
            "event",
            false);
    TypeSpec.Builder builder = TypeSpec.classBuilder("testClass");

    solidityFunctionWrapper.buildEventFunctions(functionDefinition, builder);

    //CHECKSTYLE:OFF
    String expected =
            "class testClass {\n"
                    + "  public static final org.web3j.abi.datatypes.Event TRANSFER_EVENT = new org.web3j.abi.datatypes.Event(\"Transfer\", \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.Address>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>() {}),\n" +
                    "      java.util.Arrays.<org.web3j.abi.TypeReference<?>>asList(new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.generated.Uint256>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>() {}));\n  ;\n\n"
                    + "  public java.util.List<TransferEventResponse> getTransferEvents(org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceipt) {\n"
                    + "    java.util.List<org.web3j.tx.Contract.EventValuesWithLog> valueList = extractEventParametersWithLog(TRANSFER_EVENT, transactionReceipt);\n"
                    + "    java.util.ArrayList<TransferEventResponse> responses = new java.util.ArrayList<TransferEventResponse>(valueList.size());\n"
                    + "    for (org.web3j.tx.Contract.EventValuesWithLog eventValues : valueList) {\n"
                    + "      TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "      typedResponse.log = eventValues.getLog();\n"
                    + "      typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "      typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "      typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "      responses.add(typedResponse);\n"
                    + "    }\n"
                    + "    return responses;\n"
                    + "  }\n"
                    + "\n"
                    + "  public rx.Observable<TransferEventResponse> transferEventObservable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
                    + "    return web3j.ethLogObservable(filter).map(new rx.functions.Func1<org.web3j.protocol.core.methods.response.Log, TransferEventResponse>() {\n"
                    + "      @java.lang.Override\n"
                    + "      public TransferEventResponse call(org.web3j.protocol.core.methods.response.Log log) {\n"
                    + "        org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
                    + "        TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "        typedResponse.log = log;\n"
                    + "        typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "        typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "        typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "        return typedResponse;\n"
                    + "      }\n"
                    + "    });\n"
                    + "  }\n"
                    + "\n"
                    + "  public rx.Observable<TransferEventResponse> transferEventObservable(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(TRANSFER_EVENT));\n"
                    + "    return transferEventObservable(filter);\n"
                    + "  }\n"
                    + "\n"
                    + "  public static class TransferEventResponse {\n"
                    + "    public org.web3j.protocol.core.methods.response.Log log;\n"
                    + "\n"
                    + "    public byte[] id;\n"
                    + "\n"
                    + "    public java.lang.String from;\n"
                    + "\n"
                    + "    public java.lang.String to;\n"
                    + "\n"
                    + "    public java.math.BigInteger value;\n"
                    + "\n"
                    + "    public java.lang.String message;\n"
                    + "  }\n"
                    + "}\n";
    //CHECKSTYLE:ON

    assertThat(builder.build().toString(), is(expected));
}
 
Example 19
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 20
Source File: SolidityFunctionWrapperTest.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildEventConstantMultipleValueReturn() throws Exception {

    AbiDefinition.NamedType id = new AbiDefinition.NamedType("id", "string", true);
    AbiDefinition.NamedType fromAddress = new AbiDefinition.NamedType("from", "address");
    AbiDefinition.NamedType toAddress = new AbiDefinition.NamedType("to", "address");
    AbiDefinition.NamedType value = new AbiDefinition.NamedType("value", "uint256");
    AbiDefinition.NamedType message = new AbiDefinition.NamedType("message", "string");
    fromAddress.setIndexed(true);
    toAddress.setIndexed(true);

    AbiDefinition functionDefinition = new AbiDefinition(
            false,
            Arrays.asList(id, fromAddress, toAddress, value, message),
            "Transfer",
            new ArrayList<>(),
            "event",
            false);
    TypeSpec.Builder builder = TypeSpec.classBuilder("testClass");

    builder.addMethods(
            solidityFunctionWrapper.buildEventFunctions(functionDefinition, builder));

    //CHECKSTYLE:OFF
    String expected =
            "class testClass {\n"
                    + "  public static final org.web3j.abi.datatypes.Event TRANSFER_EVENT = new org.web3j.abi.datatypes.Event(\"Transfer\", \n"
                    + "      java.util.Arrays.<org.web3j.abi.TypeReference<?>>asList(new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Address>(true) {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.generated.Uint256>() {}, new org.web3j.abi.TypeReference<org.web3j.abi.datatypes.Utf8String>() {}));\n  ;\n\n"
                    + "  public java.util.List<TransferEventResponse> getTransferEvents(org.web3j.protocol.core.methods.response.TransactionReceipt transactionReceipt) {\n"
                    + "    java.util.List<org.web3j.tx.Contract.EventValuesWithLog> valueList = extractEventParametersWithLog(TRANSFER_EVENT, transactionReceipt);\n"
                    + "    java.util.ArrayList<TransferEventResponse> responses = new java.util.ArrayList<TransferEventResponse>(valueList.size());\n"
                    + "    for (org.web3j.tx.Contract.EventValuesWithLog eventValues : valueList) {\n"
                    + "      TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "      typedResponse.log = eventValues.getLog();\n"
                    + "      typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "      typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "      typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "      typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "      responses.add(typedResponse);\n"
                    + "    }\n"
                    + "    return responses;\n"
                    + "  }\n"
                    + "\n"
                    + "  public rx.Observable<TransferEventResponse> transferEventObservable" +
                    "(org.web3j.protocol.core.methods.request.PlatonFilter filter) {\n"
                    + "    return web3j.platonLogObservable(filter).map(new rx.functions.Func1<org.web3j.protocol.core.methods.response.Log, TransferEventResponse>() {\n"
                    + "      @java.lang.Override\n"
                    + "      public TransferEventResponse call(org.web3j.protocol.core.methods.response.Log log) {\n"
                    + "        org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
                    + "        TransferEventResponse typedResponse = new TransferEventResponse();\n"
                    + "        typedResponse.log = log;\n"
                    + "        typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
                    + "        typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
                    + "        typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
                    + "        typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
                    + "        return typedResponse;\n"
                    + "      }\n"
                    + "    });\n"
                    + "  }\n"
                    + "\n"
                    + "  public rx.Observable<TransferEventResponse> transferEventObservable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
                    + "    org.web3j.protocol.core.methods.request.PlatonFilter filter = new org.web3j.protocol.core.methods.request.PlatonFilter(startBlock, endBlock, getContractAddress());\n"
                    + "    filter.addSingleTopic(org.web3j.abi.EventEncoder.encode(TRANSFER_EVENT));\n"
                    + "    return transferEventObservable(filter);\n"
                    + "  }\n"
                    + "\n"
                    + "  public static class TransferEventResponse {\n"
                    + "    public org.web3j.protocol.core.methods.response.Log log;\n"
                    + "\n"
                    + "    public byte[] id;\n"
                    + "\n"
                    + "    public java.lang.String from;\n"
                    + "\n"
                    + "    public java.lang.String to;\n"
                    + "\n"
                    + "    public java.math.BigInteger value;\n"
                    + "\n"
                    + "    public java.lang.String message;\n"
                    + "  }\n"
                    + "}\n";
    //CHECKSTYLE:ON

    assertThat(builder.build().toString(), is(expected));
}