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

The following examples show how to use org.fisco.bcos.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: MonitorService.java    From WeBASE-Node-Manager with Apache License 2.0 6 votes vote down vote up
/**
 * get interface name.
 */
private String getInterfaceName(String methodId, String contractAbi) {
    if (StringUtils.isAnyBlank(methodId, contractAbi)) {
        log.warn("fail getInterfaceName. methodId:{} contractAbi:{}", methodId, contractAbi);
        return null;
    }

    String interfaceName = null;
    try {
        List<AbiDefinition> abiList = Web3Tools.loadContractDefinition(contractAbi);
        for (AbiDefinition abiDefinition : abiList) {
            if ("function".equals(abiDefinition.getType())) {
                // support guomi sm3
                String buildMethodId = Web3Tools.buildMethodId(abiDefinition);
                if (methodId.equals(buildMethodId)) {
                    interfaceName = abiDefinition.getName();
                    break;
                }
            }
        }
    } catch (Exception ex) {
        log.error("fail getInterfaceName", ex);
    }
    return interfaceName;
}
 
Example 2
Source File: AbiUtil.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * receiptParse.
 * 
 * @param receipt info
 * @param abiList info
 * @return
 */
public static Object receiptParse(TransactionReceipt receipt, List<AbiDefinition> abiList)
    throws FrontException {
    Map<String, Object> resultMap = new HashMap<>();
    List<Log> logList = receipt.getLogs();
    for (AbiDefinition abiDefinition : abiList) {
        String eventName = abiDefinition.getName();
        List<String> funcInputTypes = getFuncInputType(abiDefinition);
        List<TypeReference<?>> finalOutputs = outputFormat(funcInputTypes);
        Event event = new Event(eventName,finalOutputs);
        Object result = null;
        for (Log logInfo : logList) {
            EventValues eventValues = Contract.staticExtractEventParameters(event, logInfo);
            if (eventValues != null) {
                result = callResultParse(funcInputTypes, eventValues.getNonIndexedValues());
                break;
            }
        }
        if (result != null) {
            resultMap.put(eventName, result);
        }
    }
    return resultMap;
}
 
Example 3
Source File: ContractAbiUtil.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
/**
 * receiptParse.
 * 
 * @param receipt info
 * @param abiList info
 * @return
 */
public static Object receiptParse(TransactionReceipt receipt, List<AbiDefinition> abiList)
        throws BaseException {
    Map<String, Object> resultMap = new HashMap<>();
    List<Log> logList = receipt.getLogs();
    for (AbiDefinition abiDefinition : abiList) {
        String eventName = abiDefinition.getName();
        List<String> funcInputTypes = getFuncInputType(abiDefinition);
        List<TypeReference<?>> finalOutputs = outputFormat(funcInputTypes);
        Event event = new Event(eventName, finalOutputs);
        Object result = null;
        for (Log logInfo : logList) {
            EventValues eventValues = Contract.staticExtractEventParameters(event, logInfo);
            if (eventValues != null) {
                result = callResultParse(funcInputTypes, eventValues.getNonIndexedValues());
                break;
            }
        }
        if (result != null) {
            resultMap.put(eventName, result);
        }
    }
    return resultMap;
}
 
Example 4
Source File: SolidityFunctionWrapper.java    From web3sdk 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 5
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
private MethodSpec buildFunction(AbiDefinition functionDefinition)
        throws ClassNotFoundException {
    String functionName = functionDefinition.getName();

    if (!SourceVersion.isName(functionName)) {
        functionName = "_" + functionName;
    }

    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 6
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
MethodSpec buildFunctionSeq(AbiDefinition functionDefinition) throws ClassNotFoundException {
    String functionName = functionDefinition.getName();
    functionName += "Seq";

    if (!SourceVersion.isName(functionName)) {
        functionName = "_" + functionName;
    }

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

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

    buildTransactionFunctionSeq(functionDefinition, methodBuilder, inputParams);

    return methodBuilder.build();
}
 
Example 7
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public static String getInputOutputFunctionName(
        AbiDefinition functionDefinition, boolean input, boolean isOverLoad) {
    if (!isOverLoad) {
        return functionDefinition.getName();
    }

    List<NamedType> nameTypes =
            (input ? functionDefinition.getInputs() : functionDefinition.getOutputs());

    String name = functionDefinition.getName();
    for (int i = 0; i < nameTypes.size(); i++) {
        AbiDefinition.NamedType.Type type =
                new AbiDefinition.NamedType.Type(nameTypes.get(i).getType());
        List<Integer> depths = type.getDepthArray();
        name += Strings.capitaliseFirstLetter(type.getBaseName());
        for (int j = 0; j < depths.size(); j++) {
            name += "Array";
            if (0 != depths.get(j)) {
                name += String.valueOf(depths.get(j));
            }
        }
    }

    return name;
}
 
Example 8
Source File: TransactionDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * @param input
 * @return
 * @throws BaseException
 * @throws TransactionException
 */
public InputAndOutputResult decodeInputReturnObject(String input)
        throws BaseException, TransactionException {

    String updatedInput = addHexPrefixToString(input);

    // select abi
    AbiDefinition abiDefinition = selectAbiDefinition(updatedInput);

    // decode input
    List<NamedType> inputTypes = abiDefinition.getInputs();
    List<TypeReference<?>> inputTypeReferences = ContractAbiUtil.paramFormat(inputTypes);
    Function function = new Function(abiDefinition.getName(), null, inputTypeReferences);
    List<Type> resultType =
            FunctionReturnDecoder.decode(
                    updatedInput.substring(10), function.getOutputParameters());

    // set result to java bean
    List<ResultEntity> resultList = new ArrayList<ResultEntity>();
    for (int i = 0; i < inputTypes.size(); i++) {
        resultList.add(
                new ResultEntity(
                        inputTypes.get(i).getName(),
                        inputTypes.get(i).getType(),
                        resultType.get(i)));
    }
    String methodSign = decodeMethodSign(abiDefinition);

    return new InputAndOutputResult(
            methodSign, FunctionEncoder.buildMethodId(methodSign), resultList);
}
 
Example 9
Source File: TransactionDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * @param input
 * @param output
 * @return
 * @throws TransactionException
 * @throws BaseException
 */
public InputAndOutputResult decodeOutputReturnObject(String input, String output)
        throws TransactionException, BaseException {

    String updatedInput = addHexPrefixToString(input);
    String updatedOutput = addHexPrefixToString(output);

    // select abi
    AbiDefinition abiDefinition = selectAbiDefinition(updatedInput);
    // decode output
    List<NamedType> outputTypes = abiDefinition.getOutputs();
    List<TypeReference<?>> outputTypeReference = ContractAbiUtil.paramFormat(outputTypes);
    Function function = new Function(abiDefinition.getName(), null, outputTypeReference);
    List<Type> resultType =
            FunctionReturnDecoder.decode(updatedOutput, function.getOutputParameters());

    // set result to java bean
    List<ResultEntity> resultList = new ArrayList<>();
    for (int i = 0; i < outputTypes.size(); i++) {
        resultList.add(
                new ResultEntity(
                        outputTypes.get(i).getName(),
                        outputTypes.get(i).getType(),
                        resultType.get(i)));
    }
    String methodSign = decodeMethodSign(abiDefinition);

    return new InputAndOutputResult(
            methodSign, FunctionEncoder.buildMethodId(methodSign), resultList);
}
 
Example 10
Source File: ContractAbiUtil.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * @param log
 * @param abiDefinition
 * @return
 * @throws BaseException
 */
public static EventValues decodeEvent(Log log, AbiDefinition abiDefinition)
        throws BaseException {

    List<TypeReference<?>> finalOutputs = paramFormat(abiDefinition.getInputs());
    Event event = new Event(abiDefinition.getName(), finalOutputs);

    EventValues eventValues = Contract.staticExtractEventParameters(event, log);
    return eventValues;
}
 
Example 11
Source File: SolidityFunctionWrapper.java    From web3sdk 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 12
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private void buildTransactionFunctionWithCallback(
        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);
    }
    // methodBuilder.addParameter(TransactionSucCallback.class, "callback");

    String functionName = functionDefinition.getName();

    methodBuilder.returns(TypeName.VOID);

    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);
    methodBuilder.addStatement("asyncExecuteTransaction(function, callback)");
}
 
Example 13
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private void buildTransactionFunctionSeq(
        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();

    TypeName returnType = TypeName.get(String.class);
    methodBuilder.returns(returnType);

    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);

    methodBuilder.addStatement("return createTransactionSeq(function)");
}
 
Example 14
Source File: ContractParser.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
/**
 * Parsing single class object of contract java code file, and storage contract info into ContractMethodInfo object,
 * firstly, remove event function, query function and functions that param's null, compute methodId and save method
 * info into ContractMethodInfo object.
 * 
 * @param clazz: class object of contract java code file.
 * @return ContractMethodInfo
 */
public ContractMethodInfo parse(Class<?> clazz) {
    AbiDefinition[] abiDefinitions = MethodUtils.getContractAbiList(clazz);
    if (abiDefinitions == null || abiDefinitions.length == 0) {
        return null;
    }
    String className = clazz.getSimpleName();
    String binary = MethodUtils.getContractBinary(clazz);
    ContractMethodInfo contractMethodInfo = new ContractMethodInfo();
    contractMethodInfo.setContractName(className);
    contractMethodInfo.setContractBinary(binary);

    List<MethodMetaInfo> methodIdList = Lists.newArrayList();
    contractMethodInfo.setMethodMetaInfos(methodIdList);

    for (AbiDefinition abiDefinition : abiDefinitions) {
        String abiType = abiDefinition.getType();
        // remove event function and query function
        if (abiType.equals(AbiTypeConstants.ABI_EVENT_TYPE) || abiDefinition.isConstant()) {
            continue;
        }
        // remove functions that input'params is null
        List<NamedType> inputs = abiDefinition.getInputs();
        if (inputs == null || inputs.isEmpty()) {
            continue;
        }
        String methodName = abiDefinition.getName();
        if (abiType.equals(AbiTypeConstants.ABI_CONSTRUCTOR_TYPE)) {
            methodName = clazz.getSimpleName();
        }
        // compute method id by method name and method input's params.
        String methodId = FunctionEncoder.buildMethodId(MethodUtils.buildMethodSignature(methodName, inputs));
        log.debug("methodId {} , methodName {}", methodId, methodName);
        MethodMetaInfo metaInfo = new MethodMetaInfo();
        metaInfo.setMethodId(methodId);
        methodName = className + StringUtils.capitalize(methodName);
        metaInfo.setMethodName(methodName);
        metaInfo.setFieldsList(inputs);
        metaInfo.setOutputFieldsList(abiDefinition.getOutputs());
        methodIdList.add(metaInfo);
    }
    return contractMethodInfo;
}
 
Example 15
Source File: SolidityFunctionWrapper.java    From web3sdk 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) {
        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(buildRegisterEventLogPushFunction(functionName));
    methods.add(buildDefaultRegisterEventLogPushFunction(functionName));

    return methods;
}