Skip to content

Web3 Ethereum

Purpose

A helper layer over the Ethereum blockchain API: web3j

Methods

Binding name: p6.web3ethereum


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: 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 jsonSource, String password)

Load a Credentials object using the supplied JSON string and password.


Method: TransactionManager noOpTransactionManager(Web3j web3j, Credentials credentials)

Creates a Web3j RawTransactionManager which submits transactions and returns immediately with an empty TransactionReceipt.


Method: TransactionManager pollingTransactionManager(Web3j web3j, Credentials credentials, long periodInMillis, int nbOfAttempts)

Creates a Web3j RawTransactionManager which submits transactions and blocks the call until it gets the transaction confirmation. It does so by polling the blockchain every periodInMillis for a maximum number of nbOfAttempts times. The nbOfAttempts parameter is optional and its default value is equal to TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH (40 in the current implementation of Web3j).


Method: TransactionManager pollingTransactionManager(Web3j web3j, Credentials credentials, Callback callback, long periodInMillis, int nbOfAttempts)

Creates a Web3j RawTransactionManager which submits transactions and returns immediately with an empty TransactionReceipt. However, unlike the NoOp TransactionManager, you can register a callback to be later notified when the transaction manager manages to fetch the transaction confirmation. It does so by polling the blockchain every periodInMillis for a maximum number of nbOfAttempts times. The nbOfAttempts parameter is optional and its default value is equal to TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH (40 in the current implementation of Web3j).


Method: TransactionManager queuingTransactionManager(Web3j web3j, Credentials credentials, Callback callback, long periodInMillis, int nbOfAttempts)

Similar to pollingTransactionManager() except that querying for transaction receipts happens on a single thread, which explains why a Callback is the only way to be notified of a transaction’s execution.


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.Web3j build(String url, String username, String password)

Initialises a Web3j instance when connection to an Ethereum client requires a username / password.


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

def web3j = p6.web3ethereum.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 = p6.web3ethereum.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 ${P6_DATA}/lib and restart platform6

Note

If you add the JAR file to the Bundle Resources Service it will be automatically added to the classpath when it is deployed. This means there is no need to restart p6core

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 = p6.web3ethereum.build("http://127.0.0.1:8545")
def credentials = p6.web3ethereum.getCredentials(json, "password")

def ondifloContract = Ondiflo.load("0x7959edd693B202C183eFFf4dDA6c7a9702a7a82D", web3j, p6.web3ethereum.defaultTransactionManager(web3j, credentials), p6.web3ethereum.DEFAULT_GAS_PRICE, p6.web3ethereum.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