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

The following examples show how to use org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition#isConstant() . 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 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 2
Source File: MethodParser.java    From WeBASE-Codegen-Monkey with Apache License 2.0 5 votes vote down vote up
public List<MethodMetaInfo> parseToInfoList(Class<?> clazz) {
    AbiDefinition[] abiDefinitions = getContractAbiList(clazz);
    if (ArrayUtil.isEmpty(abiDefinitions)) {
        return null;
    }
    List<MethodMetaInfo> lists = Lists.newArrayList();
    for (AbiDefinition abiDefinition : abiDefinitions) {
        String abiType = abiDefinition.getType();
        if (abiType.equals(AbiTypeConstants.ABI_EVENT_TYPE) || abiDefinition.isConstant()) {
            continue;
        }
        List<NamedType> inputs = abiDefinition.getInputs();
        if (CollectionUtils.isEmpty(inputs) || StringUtils.isEmpty(inputs.get(0).getName())) {
            continue;
        }
        List<NamedType> outputs = abiDefinition.getOutputs();
        MethodMetaInfo method = new MethodMetaInfo();
        method.setType("method").setContractName(clazz.getSimpleName());
        log.debug("method name : {}", abiDefinition.getName());
        if (abiType.equals(AbiTypeConstants.ABI_CONSTRUCTOR_TYPE)) {
            method.setName(clazz.getSimpleName());
        } else {
            method.setName(abiDefinition.getName());
        }
        String generatedFlag = PropertiesUtils.getGlobalProperty(ParserConstants.MONITOR, method.getContractName(),
                method.getName(), "generated", "on");
        if (generatedFlag != null && generatedFlag.equalsIgnoreCase("off")) {
            continue;
        }
        int shardingNO = Integer.parseInt(PropertiesUtils.getGlobalProperty(ParserConstants.SYSTEM,
                method.getContractName(), method.getName(), ParserConstants.SHARDINGNO, "1"));
        method.setShardingNO(shardingNO).setList(getFieldList(method, inputs))
                .setOutputList(getOutputList(method, outputs));
        lists.add(method);
    }
    return lists;
}
 
Example 3
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private List<MethodSpec> buildFunctionDefinitions(
        String className,
        TypeSpec.Builder classBuilder,
        List<AbiDefinition> functionDefinitions)
        throws ClassNotFoundException {

    List<MethodSpec> methodSpecs = new ArrayList<>();
    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getType().equals("function")) {
            MethodSpec ms = buildFunction(functionDefinition);
            methodSpecs.add(ms);

            if (!functionDefinition.isConstant()) {
                MethodSpec msCallback = buildFunctionWithCallback(functionDefinition);
                methodSpecs.add(msCallback);

                MethodSpec msSeq = buildFunctionSeq(functionDefinition);
                methodSpecs.add(msSeq);

                boolean isOverLoad =
                        isOverLoadFunction(functionDefinition.getName(), functionDefinitions);
                if (!functionDefinition.getInputs().isEmpty()) {
                    MethodSpec inputDecoder =
                            buildFunctionWithInputDecoder(functionDefinition, isOverLoad);
                    methodSpecs.add(inputDecoder);
                }

                if (!functionDefinition.getOutputs().isEmpty()) {
                    MethodSpec outputDecoder =
                            buildFunctionWithOutputDecoder(functionDefinition, isOverLoad);
                    methodSpecs.add(outputDecoder);
                }
            }
        } else if (functionDefinition.getType().equals("event")) {
            methodSpecs.addAll(buildEventFunctions(functionDefinition, classBuilder));
        }
    }

    return methodSpecs;
}
 
Example 4
Source File: ContractService.java    From WeBASE-Node-Manager with Apache License 2.0 4 votes vote down vote up
/**
 * send transaction.
 */
public Object sendTransaction(TransactionInputParam param) throws NodeMgrException {
    log.debug("start sendTransaction. param:{}", JsonTools.toJSONString(param));

    if (Objects.isNull(param)) {
        log.info("fail sendTransaction. request param is null");
        throw new NodeMgrException(ConstantCode.INVALID_PARAM_INFO);
    }

    // check contractId
    String contractAbiStr = "";
    if (param.getContractId() != null) {
        // get abi by contract
        TbContract contract = verifyContractIdExist(param.getContractId(), param.getGroupId());
        contractAbiStr = contract.getContractAbi();
        //send abi to front
        sendAbi(param.getGroupId(), param.getContractId(), param.getContractAddress());
        //check contract deploy
        verifyContractDeploy(param.getContractId(), param.getGroupId());
    } else {
        // send tx by abi
        // get from db and it's deployed
        AbiInfo abiInfo = abiService.getAbiByGroupIdAndAddress(param.getGroupId(), param.getContractAddress());
        contractAbiStr = abiInfo.getContractAbi();
    }

    // if constant, signUserId is useless
    AbiDefinition funcAbi = Web3Tools.getAbiDefinition(param.getFuncName(), contractAbiStr);
    String signUserId = "empty";
    if (!funcAbi.isConstant()) {
        signUserId = userService.getSignUserIdByAddress(param.getGroupId(), param.getUser());
    }

    //send transaction
    TransactionParam transParam = new TransactionParam();
    BeanUtils.copyProperties(param, transParam);
    transParam.setSignUserId(signUserId);
    Object frontRsp = frontRestTools
        .postForEntity(param.getGroupId(), FrontRestTools.URI_SEND_TRANSACTION_WITH_SIGN, transParam,
            Object.class);
    log.debug("end sendTransaction. frontRsp:{}", JsonTools.toJSONString(frontRsp));
    return frontRsp;
}
 
Example 5
Source File: TransService.java    From WeBASE-Transaction with Apache License 2.0 4 votes vote down vote up
/**
 * transaction query.
 * 
 * @param req parameter
 * @return
 */
public ResponseEntity call(ReqTransCallInfo req) throws BaseException {
    int groupId = req.getGroupId();
    String uuidDeploy = req.getUuidDeploy();
    String contractAddress = req.getContractAddress();
    List<Object> abiList = req.getContractAbi();
    String funcName = req.getFuncName();
    List<Object> params = req.getFuncParam();
    try {
        // check groupId
        if (!checkGroupId(groupId)) {
            log.warn("call fail. groupId:{} has not been configured", groupId);
            throw new BaseException(ConstantCode.GROUPID_NOT_CONFIGURED);
        }
        // check request style
        if (StringUtils.isBlank(uuidDeploy)
                && (StringUtils.isBlank(contractAddress) || abiList.isEmpty())) {
            throw new BaseException(ConstantCode.ADDRESS_ABI_EMPTY);
        }
        // check if contract has been deployed
        if (StringUtils.isBlank(contractAddress)) {
            contractAddress = contractMapper.selectContractAddress(groupId, uuidDeploy);
        }
        if (StringUtils.isBlank(contractAddress)) {
            log.warn("save fail. contract has not been deployed");
            throw new BaseException(ConstantCode.CONTRACT_NOT_DEPLOED);
        }
        // check contractAbi
        String contractAbi = "";
        if (abiList.isEmpty()) {
            contractAbi = contractMapper.selectContractAbi(groupId, uuidDeploy);
            if (StringUtils.isBlank(contractAbi)) {
                log.warn("save fail. uuidDeploy:{} abi is not exists", uuidDeploy);
                throw new BaseException(ConstantCode.CONTRACT_ABI_EMPTY);
            }
        } else {
            contractAbi = JsonUtils.toJSONString(abiList);
        }
        // check function
        AbiDefinition abiDefinition = ContractAbiUtil.getAbiDefinition(funcName, contractAbi);
        if (abiDefinition == null) {
            log.warn("call fail. func:{} is not exists", funcName);
            throw new BaseException(ConstantCode.FUNCTION_NOT_EXISTS);
        }
        if (!abiDefinition.isConstant()) {
            log.warn("call fail. func:{} is not constant", funcName);
            throw new BaseException(ConstantCode.FUNCTION_MUST_CONSTANT);
        }
        // check function parameter
        List<String> funcInputTypes = ContractAbiUtil.getFuncInputType(abiDefinition);
        if (funcInputTypes.size() != params.size()) {
            log.warn("call fail. funcInputTypes:{}, params:{}", funcInputTypes, params);
            throw new BaseException(ConstantCode.IN_FUNCPARAM_ERROR);
        }
        // check input format
        List<Type> finalInputs = ContractAbiUtil.inputFormat(funcInputTypes, params);
        // check output format
        List<String> funOutputTypes = ContractAbiUtil.getFuncOutputType(abiDefinition);
        List<TypeReference<?>> finalOutputs = ContractAbiUtil.outputFormat(funOutputTypes);
        // encode function
        Function function = new Function(funcName, finalInputs, finalOutputs);
        String encodedFunction = FunctionEncoder.encode(function);
        String callOutput = web3jMap.get(groupId)
                .call(Transaction.createEthCallTransaction(keyStoreService.getRandomAddress(),
                        contractAddress, encodedFunction), DefaultBlockParameterName.LATEST)
                .send().getValue().getOutput();
        List<Type> typeList =
                FunctionReturnDecoder.decode(callOutput, function.getOutputParameters());
        ResponseEntity response = new ResponseEntity(ConstantCode.RET_SUCCEED);
        if (typeList.size() > 0) {
            response.setData(ContractAbiUtil.callResultParse(funOutputTypes, typeList));
        } else {
            response.setData(typeList);
        }
        return response;
    } catch (IOException e) {
        log.error("call funcName:{} Exception:{}", funcName, e);
        throw new BaseException(ConstantCode.TRANSACTION_QUERY_FAILED);
    }
}
 
Example 6
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;
}