Search in sources :

Example 1 with AbiDefinition

use of org.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildFunctionDefinitions.

private static List<MethodSpec> buildFunctionDefinitions(String className, TypeSpec.Builder classBuilder, List<AbiDefinition> functionDefinitions) throws ClassNotFoundException {
    List<MethodSpec> methodSpecs = new ArrayList<>();
    boolean constructor = false;
    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getType().equals("function")) {
            methodSpecs.add(buildFunction(functionDefinition));
            // 添加带有callback的方法
            if (!functionDefinition.isConstant()) {
                methodSpecs.add(buildFunctionWithCallback(functionDefinition));
            }
        } else if (functionDefinition.getType().equals("event")) {
            buildEventFunctions(functionDefinition, classBuilder);
        } else if (functionDefinition.getType().equals("constructor")) {
            constructor = true;
            methodSpecs.add(buildDeploy(className, functionDefinition, Credentials.class, CREDENTIALS));
            methodSpecs.add(buildDeploy(className, functionDefinition, TransactionManager.class, TRANSACTION_MANAGER));
        }
    }
    // constructor will not be specified in ABI file if its empty
    if (!constructor) {
        MethodSpec.Builder credentialsMethodBuilder = getDeployMethodSpec(className, Credentials.class, CREDENTIALS);
        methodSpecs.add(buildDeployAsyncNoParams(credentialsMethodBuilder, className, CREDENTIALS));
        MethodSpec.Builder transactionManagerMethodBuilder = getDeployMethodSpec(className, TransactionManager.class, TRANSACTION_MANAGER);
        methodSpecs.add(buildDeployAsyncNoParams(transactionManagerMethodBuilder, className, TRANSACTION_MANAGER));
    }
    return methodSpecs;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) AbiDefinition(org.bcos.web3j.protocol.core.methods.response.AbiDefinition) TransactionManager(org.bcos.web3j.tx.TransactionManager) ArrayList(java.util.ArrayList) Credentials(org.bcos.web3j.crypto.Credentials)

Example 2 with AbiDefinition

use of org.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method loadContractDefinition.

private List<AbiDefinition> loadContractDefinition(String abi) throws IOException {
    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    AbiDefinition[] abiDefinition = objectMapper.readValue(abi, AbiDefinition[].class);
    return Arrays.asList(abiDefinition);
}
Also used : AbiDefinition(org.bcos.web3j.protocol.core.methods.response.AbiDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with AbiDefinition

use of org.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapperGenerator method generate.

private void generate() throws IOException, ClassNotFoundException {
    File binaryFile = new File(binaryFileLocation);
    if (!binaryFile.exists()) {
        exitError("Invalid input binary file specified: " + binaryFileLocation);
    }
    byte[] bytes = Files.readBytes(new File(binaryFile.toURI()));
    String binary = new String(bytes);
    File absFile = new File(absFileLocation);
    if (!absFile.exists() || !absFile.canRead()) {
        exitError("Invalid input ABI file specified: " + absFileLocation);
    }
    String fileName = absFile.getName();
    String contractName = getFileNameNoExtension(fileName);
    bytes = Files.readBytes(new File(absFile.toURI()));
    String abi = new String(bytes);
    List<AbiDefinition> functionDefinitions = loadContractDefinition(absFile);
    if (functionDefinitions.isEmpty()) {
        exitError("Unable to parse input ABI file");
    } else {
        String className = Strings.capitaliseFirstLetter(contractName);
        System.out.printf("Generating " + basePackageName + "." + className + " ... ");
        new SolidityFunctionWrapper().generateJavaFiles(contractName, binary, abi, destinationDirLocation.toString(), basePackageName);
        System.out.println("File written to " + destinationDirLocation.toString() + "\n");
    }
}
Also used : AbiDefinition(org.bcos.web3j.protocol.core.methods.response.AbiDefinition) File(java.io.File)

Example 4 with AbiDefinition

use of org.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapperGenerator method loadContractDefinition.

private static List<AbiDefinition> loadContractDefinition(File absFile) throws IOException {
    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    AbiDefinition[] abiDefinition = objectMapper.readValue(absFile, AbiDefinition[].class);
    return Arrays.asList(abiDefinition);
}
Also used : AbiDefinition(org.bcos.web3j.protocol.core.methods.response.AbiDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AbiDefinition (org.bcos.web3j.protocol.core.methods.response.AbiDefinition)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 MethodSpec (com.squareup.javapoet.MethodSpec)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Credentials (org.bcos.web3j.crypto.Credentials)1 TransactionManager (org.bcos.web3j.tx.TransactionManager)1