Web3 Ethereum
Purpose¶
A helper layer over the Ethereum blockchain API: web3j
Methods¶
Binding name: p6.web3ethereum
decodeEventData¶
Decode the data returned by a web3j event using the list of solidity types (expressed as strings).
Syntax
List<Tuple2> p6.webethereum.decodeEventData(String data, String... solidityParamTypes)
Example
def lstData = p6.web3ethereum.decodeEventData( pipeline.get("logData"), "uint256")
println "Decoded log event data: " + lstData
decodeLogTopics¶
Decode the topics returned by a web3j event using the list of solidity types (expressed as strings).
Syntax
List<Tuple2> p6.webethereum.decodeLogTopics(String topicsData, String... solidityParamTypes)
Info
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
Example
def lstData = p6.web3ethereum.decodeLogTopics( pipeline.get("logTopics"), "uint256")
println 'Decoded log event topics: ' + lstData
generateNewCredential¶
Generate new credentials using the supplied password and returning the credential as a JSON formatted String.
Syntax
String p6.webethereum.generateNewCredential(String password, boolean useFullCrypt)
Tip
The generated credentials can be ‘lite’ or ‘full’ based on useFullCrypt
.
Example
println p6.webethereum.generateNewCredential("mypassword", true)
getCredentials (login / 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.
Syntax
Credentials p6.webethereum.getCredentials(String userEmail, String propertyName, String password)
Info
The JSON can be a simple text User property or attached file.
Example
def credentials = p6.web3ethereum.getCredentials("user@server.com", "eth_credentials.json", "mypassword")
getCredentials (json)¶
Load a Credentials
object using the supplied JSON string and password.
Syntax
Credentials p6.webethereum.getCredentials(String jsonSource, String password)
Example
def credentials = p6.web3ethereum.getCredentials(json, "mypassword")
noOpTransactionManager¶
Creates a Web3j RawTransactionManager
which submits transactions and returns immediately with an empty TransactionReceipt
.
Syntax
TransactionManager p6.webethereum.noOpTransactionManager(Web3j web3j, Credentials credentials)
Example
def web3j = p6.web3ethereum.build("http://127.0.0.1:8545")
def credentials = p6.web3ethereum.getCredentials(json, "mypassword")
def manager = p6.webethereum.noOpTransactionManager(web3j, credentials)
pollingTransactionManager¶
Creates a Web3j RawTransactionManager
which submits transactions and blocks the call until it gets the transaction confirmation.
Syntax
TransactionManager p6.webethereum.pollingTransactionManager(
Web3j web3j,
Credentials credentials,
long periodInMillis
[, int nbOfAttempts]
)
Info
It does so by polling the blockchain every periodInMillis
for a maximum number of nbOfAttempts
times.
Parameter: nbOfAttempts
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).
Example
def web3j = p6.web3ethereum.build("http://127.0.0.1:8545")
def credentials = p6.web3ethereum.getCredentials(json, "mypassword")
def manager = p6.webethereum.pollingTransactionManager(web3j, credentials, 60000)
pollingTransactionManager (with callback)¶
Creates a Web3j RawTransactionManager
which submits transactions and returns immediately with an empty TransactionReceipt
.
Syntax
TransactionManager p6.webethereum.pollingTransactionManager(
Web3j web3j,
Credentials credentials,
Callback callback,
long periodInMillis
[, int nbOfAttempts]
)
Info
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.
Parameter: nbOfAttempts
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).
Example
def web3j = p6.web3ethereum.build("http://127.0.0.1:8545")
def credentials = p6.web3ethereum.getCredentials(json, "mypassword")
def manager = p6.webethereum.pollingTransactionManager(web3j, credentials, callback, 60000)
queuingTransactionManager¶
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.
Syntax
TransactionManager p6.webethereum.queuingTransactionManager(
Web3j web3j,
Credentials credentials,
Callback callback,
long periodInMillis,
int nbOfAttempts
)
Example
def web3j = p6.web3ethereum.build("http://127.0.0.1:8545")
def credentials = p6.web3ethereum.getCredentials(json, "mypassword")
def manager = p6.webethereum.queuingTransactionManager(web3j, credentials, callback, 60000, 10)
build¶
Initialises a Web3j
instance. Allows IPC or HTTP URLs and an optional indication of Infura usage.
Syntax
org.web3j.protocol.Web3j p6.webethereum.build(String url, boolean infura)
build (with login/password)¶
Initialises a Web3j
instance when connection to an Ethereum client requires a username / password.
Syntax
org.web3j.protocol.Web3j p6.webethereum.build(String url, String username, String password)
Example
def web3j = p6.web3ethereum.build("http://172.13.0.10:8545", "username", "mypassword")
buildAdmin¶
Creates an admin client for Parity or Geth providing support for commands like personal_newAccount.
Syntax
org.web3j.protocol.admin.Admin p6.webethereum.buildAdmin(String clientAddress, boolean ... infura)
Example
def admin = p6.webethereum.buildAdmin("http://172.13.0.10:8545")
getNextNonce¶
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.
Syntax
BigInteger p6.webethereum.getNextNonce(Web3j web3j, String address)
Example
def nonce = p6.web3ethereum.getNextNonce(web3j, "0xeEf629969b8B48975658f6a328E950ae5Ab78b45")
waitForTransactionReceipt¶
Waits for a transaction receipt given a transaction hash and web3j instance. Forty attempts with fifteen seconds intervals will be made before failure.
Syntax
TransactionReceipt p6.webethereum.waitForTransactionReceipt(Web3j web3j, String transactionHash)
Example
def web3j = p6.web3ethereum.build("http://172.13.0.10:8545")
def ethSendTransaction = web3j.ethSendRawTransaction(Numeric.toHexString(signedMessage)).sendAsync().get()
def transactionHash = ethSendTransaction.getTransactionHash()
def transactionReceipt = p6.web3ethereum.waitForTransactionReceipt(web3j, transactionHash)
waitForTransactionReceipt (with sleep and attempts)¶
Waits for a transaction receipt given a transaction hash and web3j instance.
Syntax
TransactionReceipt p6.webethereum.waitForTransactionReceipt(
Web3j web3j,
String transactionHash,
int sleepDurationMillis,
int attempts
)
Parameter: attempts
The Number of attempts
and interval to wait (in milliseconds) is supplied.
Example
def web3j = p6.web3ethereum.build("http://172.13.0.10:8545")
def ethSendTransaction = web3j.ethSendRawTransaction(Numeric.toHexString(signedMessage)).sendAsync().get()
def transactionHash = ethSendTransaction.getTransactionHash()
def transactionReceipt = p6.web3ethereum.waitForTransactionReceipt(web3j, transactionHash, 60000, 10)
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.
Code
import com.amalto.platform6.solidity.model.*
Step 4: Load the contract and use it’s methods
Code
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:
Code
def msg = ondifloContract.getMessage("0102030405060708".getBytes()).send()
Info
For more details see: https://docs.web3j.io/smart_contracts.html#solidity-smart-contract-wrappers