Skip to content

Ethereum Smart Contract Guide

Ashish Shukla edited this page Aug 31, 2018 · 4 revisions

These are the Smart Contract features supported by driver :

  • DEPLOY
  • CALL

Note: DEPLOY and CALL query need java wrapper class of the smart contract.

Note: CALL query also needs address where smart contract is deployed on the blockchain.

Note: Any smart contract interaction required keystore path and password passed as property while making connection.

Create Java Wrapper Class

To create the Java Wrapper class you need to follow these steps

1. CREATE ABI AND BINARY

To generate the wrapper code, compile your smart contract:

$ solc --bin --abi --optimize --overwrite -o <outputDirectory> <solFileName>

2. CREATE JAVA WRAPPER

Then generate the wrapper code using web3j’s Command Line Tools: (Version 3.4.0 or above)

$ web3j solidity generate <inputDirectory>/<filename>.bin <inputDirectory>/<filename>.abi -o <DEV_HOME>/src/main/java -p <packageName>

DEPLOY

Now you can deploy your smart contract.

The query syntax for smart contract deployment is as below-

DEPLOY SMARTCONTRACT '<classpath_java_wrapper>'(<parameterList>) AND WITHASYNC true

Note: "AND WITHASYNC true" is optional, if true, will return the CompletableFuture else it will return object value which contains address of smart contract.

Note: <classpath_wrapper_class> is the classpath of the wrapper class, and this classpath should present in your project.

Example:

String url = "jdbc:blkchn:ethereum://127.0.0.1:8545";
String driverClass = "com.impetus.eth.jdbc.EthDriver";
String query = "DEPLOY SMARTCONTRACT '<classpath_java_wrapper>'(<parameterList>)";
Class.forName(driverClass);
Properties prop = new Properties();
prop.put(DriverConstants.KEYSTORE_PATH,"/home/<path>");
prop.put(DriverConstants.KEYSTORE_PASSWORD, "<password>");
Connection conn = DriverManager.getConnection(url, prop);
Statement stmt = conn.createStatement();
boolean retBool = stmt.execute(query);
if(retBool) {
    ResultSet ret = stmt.getResultSet();
    ret.next();
    String return_value = ret.getString(1);
    LOGGER.info("Smart contract has deployed at address :: "+return_value);
}

CALL

Query syntax for calling any smart contract function as below-

CALL <funtionName> (<parameterList>) USE SMARTCONTRACT '<classpath_java_wrapper>' WITH ADDRESS '<smartcontract_address_in_BlockChain>' AND WITHASYNC true

Note: "AND WITHASYNC true" is optional, if true, will return the CompletableFuture else it will return object value.

Example:

String query = "CALL <functionName> (<parameterList>) USE SMARTCONTRACT '<classpath_java_wrapper>' WITH ADDRESS '<smartcontract_address_in_BlockChain>' AND WITHASYNC true";
Class.forName(driverClass);
Properties prop = new Properties();
prop.put(DriverConstants.KEYSTORE_PATH,"/home/<path>");
prop.put(DriverConstants.KEYSTORE_PASSWORD, "<password>");
Connection conn = DriverManager.getConnection(url, prop);
Statement stmt = conn.createStatement();
boolean retBool = stmt.execute(query);
if(retBool) {
ResultSet ret = stmt.getResultSet();
ret.next();
CompletableFuture return_value = (CompletableFuture) ret.getObject(1);
while (true) if (return_value.isDone()) {
        	LOGGER.info("Return Value :: " + return_value.get());
        	break;
    	} else {
        	LOGGER.info("Waiting future to complete");
        	Thread.sleep(1000);
    	}
}

Parameter List

Comma separated values of arguments.

Supported types

DataType Example
String 'Jhon', 'Voter1', ''
Integer 123, 1994, 0
Biginteger 123, 192345, 0
Boolean true, false
Bytes HEX('0x123'), HEX('0xa2'), HEX('')
List ['Jhon','Tom'], [true, false], [1,2,3], <'INTEGER'>[]

Note: Empty list must contain the type of the list, passed as string in angular braces.

Note: List should contain same type of data values.