org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition Java Examples

The following examples show how to use org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition. 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: TransService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
public static AbiDefinition getFunctionAbiDefinition(String functionName, String contractAbi) {
    if(functionName == null) {
        throw new FrontException(IN_FUNCTION_ERROR);
    }
    List<AbiDefinition> abiDefinitionList = JsonUtils.toJavaObjectList(contractAbi, AbiDefinition.class);
    if (abiDefinitionList == null) {
        throw new FrontException(ConstantCode.FAIL_PARSE_JSON);
    }
    AbiDefinition result = null;
    for (AbiDefinition abiDefinition : abiDefinitionList) {
        if (abiDefinition == null) {
            throw new FrontException(IN_FUNCTION_ERROR);
        }
        if (ConstantProperties.TYPE_FUNCTION.equals(abiDefinition.getType())
                && functionName.equals(abiDefinition.getName())) {
            result = abiDefinition;
            break;
        }
    }
    return result;
}
 
Example #2
Source File: ContractAbiUtil.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * @param contractAbi
 * @return
 */
public static List<AbiDefinition> getEventAbiDefinitions(String contractAbi) {

    List<AbiDefinition> result = new ArrayList<>();
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        AbiDefinition[] abiDefinitions =
                objectMapper.readValue(contractAbi, AbiDefinition[].class);

        for (AbiDefinition abiDefinition : abiDefinitions) {
            if (TYPE_EVENT.equals(abiDefinition.getType())) {
                result.add(abiDefinition);
            }
        }
    } catch (JsonProcessingException e) {
        logger.warn(" invalid json, abi: {}, e: {} ", contractAbi, e);
    }
    return result;
}
 
Example #3
Source File: ContractAbiUtil.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * @param contractAbi
 * @return
 */
public static List<AbiDefinition> getFuncAbiDefinition(String contractAbi) {
    List<AbiDefinition> result = new ArrayList<>();
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        AbiDefinition[] abiDefinitions =
                objectMapper.readValue(contractAbi, AbiDefinition[].class);

        for (AbiDefinition abiDefinition : abiDefinitions) {
            if (TYPE_FUNCTION.equals(abiDefinition.getType())
                    || TYPE_CONSTRUCTOR.equals(abiDefinition.getType())) {
                result.add(abiDefinition);
            }
        }
    } catch (JsonProcessingException e) {
        logger.warn(" invalid json, abi: {}, e: {} ", contractAbi, e);
    }
    return result;
}
 
Example #4
Source File: ContractAbiUtil.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * @param contractAbi
 * @return
 */
public static AbiDefinition getConstructorAbiDefinition(String contractAbi) {
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        AbiDefinition[] abiDefinitions =
                objectMapper.readValue(contractAbi, AbiDefinition[].class);

        for (AbiDefinition abiDefinition : abiDefinitions) {
            if (TYPE_CONSTRUCTOR.equals(abiDefinition.getType())) {
                return abiDefinition;
            }
        }
    } catch (JsonProcessingException e) {
        logger.warn(" invalid  json, abi: {}, e: {} ", contractAbi, e);
    }
    return null;
}
 
Example #5
Source File: TypeReferenceUtils.java    From WeBASE-Codegen-Monkey with Apache License 2.0 6 votes vote down vote up
public static TypeReference<?> getTypeRef(String solType) throws BaseException {
    AbiDefinition.NamedType.Type type = new AbiDefinition.NamedType.Type(solType);
    // nested array , not support now.
    if (type.getDepth() > 1) {
        throw new BaseException(201202, String.format("type:%s unsupported array decoding", type.getName()));
    }

    TypeReference<?> typeReference = null;
    if (type.dynamicArray()) {
        typeReference = DynamicArrayReference.create(type.getBaseName(), false);
    } else if (type.staticArray()) {
        typeReference = StaticArrayReference.create(type.getBaseName(), type.getDimensions(), false);
    } else {
        typeReference = TypeReference.create(ContractTypeUtil.getType(solType), false);
    }
    return typeReference;
}
 
Example #6
Source File: ContractAbiUtilTest.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
@Test
public void testDepolyContract() throws Exception {

    String contractName = "hello";
    String version = "2.0";
    List<AbiDefinition> abiList = ContractAbiUtil.loadContractDefinition(new File("src/test/resources/solidity/Ok.abi"));
    ContractAbiUtil.setContractWithAbi(contractName, version, abiList, false);

    String bytecodeBin = "608060405234801561001057600080fd5b5060016000800160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506402540be40060006001018190555060028060000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060006002600101819055506103bf806100c26000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806366c99139146100515780636d4ce63c1461007e575b600080fd5b34801561005d57600080fd5b5061007c600480360381019080803590602001909291905050506100a9565b005b34801561008a57600080fd5b506100936102e1565b6040518082815260200191505060405180910390f35b8060006001015410806100c757506002600101548160026001015401105b156100d1576102de565b8060006001015403600060010181905550806002600101600082825401925050819055507fc77b710b83d1dc3f3fafeccd08a6c469beb873b2f0975b50d1698e46b3ee5b4c816040518082815260200191505060405180910390a160046080604051908101604052806040805190810160405280600881526020017f323031373034313300000000000000000000000000000000000000000000000081525081526020016000800160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001600260000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001838152509080600181540180825580915050906001820390600052602060002090600402016000909192909190915060008201518160000190805190602001906102419291906102ee565b5060208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550606082015181600301555050505b50565b6000600260010154905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061032f57805160ff191683800117855561035d565b8280016001018555821561035d579182015b8281111561035c578251825591602001919060010190610341565b5b50905061036a919061036e565b5090565b61039091905b8082111561038c576000816000905550600101610374565b5090565b905600a165627a7a72305820044e82d74a0d492f9f764e8bbf8eeca940eee670c9ab7bba9861db1d522ab6400029";
    String encodedConstructor = ContractService.constructorEncodedByContractNameAndVersion(contractName, version, new ArrayList<>());

    CommonContract commonContract = null;
        commonContract = CommonContract.deploy(web3j, credentials, Constants.GAS_PRICE, Constants.GAS_LIMIT,
                Constants.INITIAL_WEI_VALUE, bytecodeBin, encodedConstructor).send();
    System.out.println(commonContract.getContractAddress());
    CnsService cnsService = new CnsService(web3j, credentials);
    String result =  cnsService.registerCns(contractName ,version,commonContract.getContractAddress(),"[{\"constant\":false,\"inputs\":[{\"name\":\"num\",\"type\":\"uint256\"}],\"name\":\"trans\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"num\",\"type\":\"uint256\"}],\"name\":\"TransEvent\",\"type\":\"event\"}]");
    System.out.println(result);
}
 
Example #7
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 #8
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
private TypeSpec.Builder createClassBuilder(
        String className, String binary, List<AbiDefinition> abi) {

    String javadoc = CODEGEN_WARNING + getWeb3jVersion();

    return TypeSpec.classBuilder(className)
            .addModifiers(Modifier.PUBLIC)
            .addJavadoc(javadoc)
            .superclass(Contract.class)
            .addAnnotation(
                    AnnotationSpec.builder(SuppressWarnings.class)
                            .addMember("value", "$S", "unchecked")
                            .build())
            .addField(createBinaryArrayDefinition(binary))
            .addField(createBinaryDefinition())
            .addField(createABIArrayDefinition())
            .addField(createABIDefinition())
            .addField(createTransactionDecoderDefinition());
}
 
Example #9
Source File: ContractService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * deploy locally, not through webase-sign
 */
public String deployLocally(ReqDeploy req) throws Exception {
    int groupId = req.getGroupId();
    String userAddress = req.getUser();
    // check deploy permission
    checkDeployPermission(groupId, userAddress);
    String contractName = req.getContractName();
    List<AbiDefinition> abiInfos = req.getAbiInfo();
    String bytecodeBin = req.getBytecodeBin();
    List<Object> params = req.getFuncParam();

    ContractAbiUtil.VersionEvent versionEvent =
            ContractAbiUtil.getVersionEventFromAbi(contractName, abiInfos);
    String encodedConstructor = constructorEncoded(contractName, versionEvent, params);

    // get privateKey
    Credentials credentials = keyStoreService.getCredentials(userAddress);
    // contract deploy
    String contractAddress =
            deployContract(groupId, bytecodeBin, encodedConstructor, credentials);

    log.info("success deployLocally. contractAddress:{}", contractAddress);
    return contractAddress;
}
 
Example #10
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 #11
Source File: BasicTest.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
     * test abi bin in helloWorld
     * deploy through common contract
     * @throws Exception
     */
    @Test
    public void testDepolyContract() throws Exception {

        String contractName = "HelloWorld";
        String version = "2.0";
        List<AbiDefinition> abiList = ContractAbiUtil.loadContractDefinition(new File("src/test/resources/solidity/HelloWorldGM.abi"));
        ContractAbiUtil.setContractWithAbi(contractName, version, abiList, false);
        String bytecodeBin = "608060405234801561001057600080fd5b506040805190810160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b6102d7806101166000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063299f7f9d146100515780633590b49f146100e1575b600080fd5b34801561005d57600080fd5b5061006661014a565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a657808201518184015260208101905061008b565b50505050905090810190601f1680156100d35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100ed57600080fd5b50610148600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101ec565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b5050505050905090565b8060009080519060200190610202929190610206565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061024757805160ff1916838001178555610275565b82800160010185558215610275579182015b82811115610274578251825591602001919060010190610259565b5b5090506102829190610286565b5090565b6102a891905b808211156102a457600081600090555060010161028c565b5090565b905600a165627a7a72305820149885ffb94e77aa5ef86c3e347ff2d32491bd694173e82b03b08f67da4cebc00029";
        String encodedConstructor = ContractService.constructorEncodedByContractNameAndVersion(contractName, version, new ArrayList<>());

        CommonContract commonContract = null;
        commonContract = CommonContract.deploy(web3j, credentials, Constants.GAS_PRICE, Constants.GAS_LIMIT,
                Constants.INITIAL_WEI_VALUE, bytecodeBin, encodedConstructor).send();
        System.out.println(commonContract.getContractAddress());

//        CnsService cnsService = new CnsService(web3j, credentials);
//        String result =  cnsService.registerCns(contractName ,version,commonContract.getContractAddress(),"[{\"constant\":false,\"inputs\":[{\"name\":\"num\",\"type\":\"uint256\"}],\"name\":\"trans\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"num\",\"type\":\"uint256\"}],\"name\":\"TransEvent\",\"type\":\"event\"}]");
//        System.out.println(result);
    }
 
Example #12
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
private 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(
                namedTypes,
                ", \n",
                // this results in fully qualified names being generated
                this::createMappedParameterTypes);
    } else {
        return Collection.join(inputParameterTypes, ", ", parameterSpec -> parameterSpec.name);
    }
}
 
Example #13
Source File: TransService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * checkAndSaveAbiFromDb.
 *
 * @param req request
 */
public boolean checkAndSaveAbiFromDb(ContractOfTrans req) throws Exception {
    Contract contract = contractRepository.findByGroupIdAndContractPathAndContractName(
            req.getGroupId(), req.getContractPath(), req.getContractName());
    log.info("checkAndSaveAbiFromDb contract:{}", contract);
    if (Objects.isNull(contract)) {
        log.info("checkAndSaveAbiFromDb contract is null");
        return false;
    }
    // save abi
    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    List<AbiDefinition> abiDefinitionList =
            objectMapper.readValue(contract.getContractAbi(), objectMapper.getTypeFactory()
                    .constructCollectionType(List.class, AbiDefinition.class));
    ContractAbiUtil.setFunctionFromAbi(req.getContractName(), req.getContractPath(),
            abiDefinitionList, new ArrayList<>());
    return true;
}
 
Example #14
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 #15
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
private String addParameters(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());
        }

        if (useNativeJavaTypes) {
            return Collection.join(
                    namedTypes,
                    ", \n",
                    // this results in fully qualified names being generated
                    this::createMappedParameterTypes);
        } else {
            return Collection.join(inputParameterTypes, ", ", parameterSpec -> parameterSpec.name);
        }
    }
 
Example #16
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 #17
Source File: Web3Tools.java    From WeBASE-Node-Manager with Apache License 2.0 6 votes vote down vote up
/**
 * get AbiDefinition by Function name
 * @param funName
 * @param contractAbi
 * @return
 */
public static AbiDefinition getAbiDefinition(String funName, String contractAbi) {
    if (StringUtils.isBlank(contractAbi)) {
        throw new NodeMgrException(ConstantCode.CONTRACT_ABI_EMPTY);
    }
    List<AbiDefinition> abiList = JsonTools.toJavaObjectList(contractAbi, AbiDefinition.class);
    if (abiList == null) {
        throw new NodeMgrException(ConstantCode.FAIL_PARSE_JSON);
    }
    AbiDefinition result = null;
    for (AbiDefinition abiDefinition : abiList) {
        if (ConstantProperties.TYPE_FUNCTION.equals(abiDefinition.getType())
                && funName.equals(abiDefinition.getName())) {
            result = abiDefinition;
            break;
        }
    }
    return result;
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
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 #23
Source File: ContractAbiUtil.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * get function abi info.
 * 
 * @param name name
 * @param contractAbi contractAbi
 * @return
 */
public static AbiDefinition getAbiDefinition(String name, String contractAbi) {
    List<AbiDefinition> abiArr = JsonUtils.toJavaObjectList(contractAbi, AbiDefinition.class);
    AbiDefinition result = null;
    for (AbiDefinition abiDefinition : abiArr) {
        if (ConstantProperties.TYPE_FUNCTION.equals(abiDefinition.getType())
            && name.equals(abiDefinition.getName())) {
            result = abiDefinition;
            break;
        }
    }
    return result;
}
 
Example #24
Source File: TypeTest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void typeTest0() {
    AbiDefinition.NamedType.Type type = new AbiDefinition.NamedType.Type("int");

    assertThat(type.getBaseName(), is("int"));
    assertThat(type.getName(), is("int"));
    assertThat(type.getDepth(), is(0));
    assertThat(type.arrayType(), is(false));
    assertThat(type.dynamicArray(), is(false));
    assertThat(type.staticArray(), is(false));
    assertThat(type.getDimensions(), is(0));
}
 
Example #25
Source File: TransactionDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public TransactionDecoder(String abi, String bin) {
    this.abi = abi;
    this.bin = bin;
    methodIDMap = new HashMap<String, AbiDefinition>();
    List<AbiDefinition> funcAbiDefinitionList = ContractAbiUtil.getFuncAbiDefinition(abi);
    for (AbiDefinition abiDefinition : funcAbiDefinitionList) {
        String methodSign = decodeMethodSign(abiDefinition);
        String methodID = FunctionEncoder.buildMethodId(methodSign);
        methodIDMap.put(methodID, abiDefinition);
    }
}
 
Example #26
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 #27
Source File: ContractAbiUtil.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * getFuncOutputType.
 * 
 * @param abiDefinition abiDefinition
 * @return
 */
public static List<String> getFuncOutputType(AbiDefinition abiDefinition) {
    List<String> outputList = new ArrayList<>();
    List<NamedType> outputs = abiDefinition.getOutputs();
    for (NamedType output : outputs) {
        outputList.add(output.getType());
    }
    return outputList;
}
 
Example #28
Source File: ContractAbiUtil.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * getFuncInputType.
 * 
 * @param abiDefinition abiDefinition
 * @return
 */
public static List<String> getFuncInputType(AbiDefinition abiDefinition) {
    List<String> inputList = new ArrayList<>();
    if (abiDefinition != null) {
        List<NamedType> inputs = abiDefinition.getInputs();
        for (NamedType input : inputs) {
            inputList.add(input.getType());
        }
    }
    return inputList;
}
 
Example #29
Source File: ContractAbiUtil.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * get event abi info.
 * 
 * @param contractAbi contractAbi
 * @return
 */
public static List<AbiDefinition> getEventAbiDefinitions(String contractAbi) {
    List<AbiDefinition> abiArr = JsonUtils.toJavaObjectList(contractAbi, AbiDefinition.class);
    List<AbiDefinition> result = new ArrayList<>();
    for (AbiDefinition abiDefinition : abiArr) {
        if (ConstantProperties.TYPE_EVENT.equals(abiDefinition.getType())) {
            result.add(abiDefinition);
        }
    }
    return result;
}
 
Example #30
Source File: TransactionDecoderTest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private static String decodeMethodSign(AbiDefinition abiDefinition) {
    List<NamedType> inputTypes = abiDefinition.getInputs();
    StringBuilder methodSign = new StringBuilder();
    methodSign.append(abiDefinition.getName());
    methodSign.append("(");
    String params =
            inputTypes.stream().map(NamedType::getType).collect(Collectors.joining(","));
    methodSign.append(params);
    methodSign.append(")");
    return methodSign.toString();
}