All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.web3j.openapi.codegen.web3jCodegenStuff.SolidityFunctionWrapperGenerator.kt Maven / Gradle / Ivy

There is a newer version: 4.8.4
Show newest version
/*
 * Copyright 2020 Web3 Labs Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.web3j.openapi.codegen.web3jCodegenStuff

import com.fasterxml.jackson.databind.ObjectMapper
import org.web3j.codegen.Console.exitError
import org.web3j.codegen.SolidityFunctionWrapper
import org.web3j.tx.Contract
import java.io.File
import java.io.IOException

class SolidityFunctionWrapperGenerator(
    val binFile: File? = null,
    val abiFile: File? = null,
    val destinationDir: File?,
    var contractName: String? = null,
    override val basePackageName: String?,
    override val useJavaNativeTypes: Boolean = false,
    override val useJavaPrimitiveTypes: Boolean = false,
    val generateSendTxForCalls: Boolean = false,
    override val contractClass: Class = Contract::class.java,
    val addressLength: Int
) : FunctionWrapperGenerator(
    contractClass,
    destinationDir,
    basePackageName,
    useJavaNativeTypes,
    useJavaPrimitiveTypes) {

    private val COMMAND_SOLIDITY = "solidity"
    private val COMMAND_GENERATE = "generate"
    val COMMAND_PREFIX = "$COMMAND_SOLIDITY $COMMAND_GENERATE"
    private val solidityTypes = false
    val destinationFileDir: File? = destinationDir
    val packageName: String? = basePackageName
    private val primitiveTypes = useJavaPrimitiveTypes

    /*
    Class contractClass
     * Usage: solidity generate [-hV] [-jt] [-st] -a= [-b=]
     * -o= -p=
     * -h, --help                 Show this help message and exit.
     * -V, --version              Print version information and exit.
     * -a, --abiFile=    abi file with contract definition.
     * -b, --binFile=    bin file with contract compiled code in order to
     * generate deploy methods.
     * -o, --outputDir=
     * destination base directory.
     * -p, --package=
     * base package name.
     * -jt, --javaTypes       use native java types.
     * Default: true
     * -st, --solidityTypes   use solidity types.
     */

    @Throws(IOException::class)
    fun loadContractDefinition(absFile: File?): List {
        val objectMapper: ObjectMapper =
            org.web3j.protocol.ObjectMapperFactory.getObjectMapper()
        val abiDefinition: Array =
            objectMapper.readValue(
                absFile,
                Array::class.java
            )
        return listOf(*abiDefinition)
    }

    @Throws(IOException::class, ClassNotFoundException::class)
    override fun generate() {
        var binary: String? = Contract.BIN_NOT_PROVIDED
        if (binFile != null) {
            val bytes: ByteArray = org.web3j.utils.Files.readBytes(binFile)
            binary = String(bytes)
        }
        val functionDefinitions: List =
            loadContractDefinition(abiFile)
        if (functionDefinitions.isEmpty()) {
            exitError("Unable to parse input ABI file")
        } else {
            val className: String = org.web3j.utils.Strings.capitaliseFirstLetter(contractName)
            print("Generating " + basePackageName.toString() + "." + className + " ... ")
            SolidityFunctionWrapper(
                useJavaNativeTypes,
                useJavaPrimitiveTypes,
                generateSendTxForCalls,
                addressLength
            )
                .generateJavaFiles(
                    contractClass,
                    contractName,
                    binary,
                    functionDefinitions,
                    destinationDirLocation.toString(),
                    basePackageName,
                    null
                )
            println(
                """
                    File written to $destinationDirLocation

                    """.trimIndent()
            )
        }
    }

    fun run() {
        try {
            val useJavaTypes: Boolean = !solidityTypes
            if (contractName.isNullOrEmpty()) {
                contractName = getFileNameNoExtension(abiFile!!.name)
            }
            SolidityFunctionWrapperGenerator(
                binFile,
                abiFile,
                destinationFileDir,
                contractName,
                packageName,
                useJavaTypes,
                primitiveTypes,
                addressLength = addressLength
            )
                .generate()
        } catch (e: Exception) {
            exitError(e)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy