Ethereum RPC

Purpose

A helper layer over the Ethereum blockchain API: web3j

Methods

Binding name: ethereumrpc


Method: List<Tuple2> decodeEventData(String data, String... solidityParamTypes)

Decode the data returned by a web3j event using the list of solidity types (expressed as strings).


Method: List<Tuple2> decodeLogTopics(String topicsData, String... solidityParamTypes)

Decode the topics returned by a web3j event using the list of solidity types (expressed as strings). Note: Topic data contains a maximum of four comma separated values. The first value is ignored as it’s “the hash of the signature of the event”

See: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges


Method: String generateNewCredential(String password, boolean useFullCrypt)

Generate new credentials using the supplied password and returning the credential as a JSON formatted String. The generated credentials can be ‘lite’ or ‘full’ based on useFullCrypt


Method: String addNewCredentials(String tableName, Map keys, String fieldName, String password, boolean useFullScrypt)

Generate new credentials using the supplied password and store the resulting JSON credentials in the table specified (using keys map and filename) The generated credentials can be ‘lite’ or ‘full’ based on useFullCrypt


Method: Credentials getCredentials(String userEmail, String propertyName, String password)

Load a credentials object using a JSON string attached as a User property and password. The given propertyName is used to select the User property value. The JSON can be a simple text User property or attached file


Method: Credentials getCredentials(String tableName, Map keys, String fieldName, String password)

Load a credentials object using the JSON representation found in the given table (using supplied keys and fieldname)


Method: Credentials getCredentials(String jsonSource, String password)

Load a credentials object using the supplied JSON string and password


Method: TransactionManager defaultTransactionManager(Web3j web3j, Credentials credentials)

Helper to create a rawTransactionManager


Method: org.web3j.protocol.Web3j build(String url, boolean... infura)

Initialises a web3j instance. Allows IPC or HTTP URLs and an optional indication of Infura usage.


Method: org.web3j.protocol.admin.Admin buildAdmin(String clientAddress, boolean ... infura)

Creates an admin client for Parity or Geth providing support for commands like personal_newAccount


Method: BigInteger getNextNonce(Web3j web3j, String address)

The nonce is an increasing numeric value which is used to uniquely identify transactions. This method returns the next nonce value to use for the given address.


Method: TransactionReceipt waitForTransactionReceipt(Web3j web3j, String transactionHash)

Waits for a transaction receipt given a transaction hash and web3j instance. Forty attempts with fifteen seconds intervals will be made before failure.


Method: TransactionReceipt waitForTransactionReceipt(Web3j web3j, String transactionHash, int sleepDurationMillis, int attempts)

Waits for a transaction receipt given a transaction hash and web3j instance. The Number of attempts and interval to wait (in milliseconds) is supplied.


Examples

def nonce = ethereumrpc.getNextNonce(web3j, "0xeEf629969b8B48975658f6a328E950ae5Ab78b45")
def lstData = ethereumrpc.decodeEventData( pipeline.get("logData"), "uint256")
println "Decoded log event data: " + lstData
def lstData = ethereumrpc.decodeEventData( pipeline.get("logTopics"), "uint256")
println 'Decoded log event topics: ' + lstData
def json = ethereumrpc.addNewCredentials("wallet", ["id": "simontemple"], "credential", "password", true)
println json
def credentials = ethereumrpc.getCredentials("simon.temple@amalto.com", "eth_credentials.json", "mypassword")
// Example of sending a transaction and using .waitForTransactionReceipt()

def web3j = ethereumrpc.build("http://172.13.0.10:8545")
assert null != web3j

EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(Numeric.toHexString(signedMessage)).sendAsync().get()
assert false == ethSendTransaction.hasError(): ethSendTransaction.getError().getMessage()

def transactionHash = ethSendTransaction.getTransactionHash()

TransactionReceipt transactionReceipt = ethereumrpc.waitForTransactionReceipt(web3j, transactionHash)
EthTransaction transaction = web3j.ethGetTransactionByHash(transactionReceipt.getTransactionHash()).send()

Binding Smart Contracts

This section explains how to use a smart contract written in the Solidity Contract-Oriented Programming Language from within a script.

Step 1: Create a web3j java class representing each contract

Please refer to https://github.com/amalto/solidity-jar-builder

Step 2: Make the Web3j class available to platform6

Copy the JAR file created in step one to ${B2BOX_DATA}/lib and restart platform6

Step 3: Import the Class into your Script

Simply adding the import statement to you script will make you ‘contract’ class available.

import com.amalto.platform6.solidity.model.*

Step 4: Load the contract and use it’s methods

def web3j = ethereumrpc.build("http://127.0.0.1:8545")
def credentials = ethereumrpc.getCredentials(json, "password")

def ondifloContract = Ondiflo.load("0x7959edd693B202C183eFFf4dDA6c7a9702a7a82D", web3j, ethereumrpc.defaultTransactionManager(web3j, credentials), ethereumrpc.DEFAULT_GAS_PRICE, ethereumrpc.DEFAULT_GAS_LIMIT)

Once you have a reference to a loaded contract you can call it’s methods:

def msg = ondifloContract.getMessage("0102030405060708".getBytes()).send()

For more details see: https://docs.web3j.io/smart_contracts.html#solidity-smart-contract-wrappers