This repo contains samples how to build applications using the blockchain via Ethereum Android.
This sample shows how to use the low level RPC commands to interact with an Ethereum node
The general approach is always the following:
- create a
WrappedRequest
with the desired RPC command and the corresponding parameters (see https://github.com/ethereum/wiki/wiki/JSON-RPC) - either use the synchronous
send
orsendAsync
method to retrieve aWrappedResponse
- check
WrappedResponse.isSuccess()
and useWrappedResponse.getErrorMessage()
in case it failed - if it succeeded
WrappedResponse.getResponse()
contains the response of the RPC call
This sample guides you through all steps to interact with a smart contract.
To interact with an existing contract you will need to have the ABI, if you want to deploy a new contract you also need its bytecode. You can use any compiler you want to achieve this. For the sake of simplicity we misuse Junit test functionality.
Check SimpleOwnedStorageTest.compile()
which print both bytecode and ABI to System.out
. These are used as constants in SimpleStorageActivity
.
Deploying a smart contract involves 2 steps:
ethereumAndroid.contracts().create(String contractBytecode, String contractAbi, Object... constructorParams)
This will create a hex encoded unsigned transaction String which already includes the right nonce for the users identity.
As this is a write operation, it will cost the user Ether and the user will need to approve the transaction by signing it and submitting it to the network
ethereumAndroid.submitTransaction(Activity parentActivity, int requestCode, String transactionString)
This will open an Activity where the user can approve the transaction. You will receive the outcome of the operation via
parentActivity.onActivityResult(int requestCode, int resultCode, Intent data)
In case resultCode == RESULT_OK
the result Intent will contain the transaction hash:
String transaction = data.getStringExtra("transaction");
It the result was not OK the Intent will contain the error message instead:
String error = data.getStringExtra("error");
Retrieve the transaction receipt which contains the address of the created contract.
WrappedRequest wrappedRequest = new WrappedRequest();
wrappedRequest.setCommand(RpcCommand.eth_getTransactionReceipt.toString());
wrappedRequest.setParameters(new Object[]{transaction});
The response will contain a map containing key-value-pairs of the transaction receipt, where you can read the contract address from.
HashMap<String, String> transactionObject = (HashMap<String, String>) response.getResponse();
String contractAddress = transactionObject.get("contractAddress");
In order to easily interact with an existing contract you need the contract ABI and a Java interface containing the operations you would like to use. This Java interface can contain a subset of the contract ABI, the method needs to be named exactly as the ABI function name and have the same input and output parameters.
For read operations (functions which are marked with constant) this is all you need to do.
Write operations are represented as PendingTransaction
which gives the ability to create the unsigned transaction and decode the result of the transaction once it is picked up by the network.
SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class);
String value = simpleOwnedStorage.get();
Create the transaction
SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class);
PendingTransaction<Void> pendingWrite = simpleOwnedStorage.set("a new value");
Submit the transaction
ethereumAndroid.submitTransaction(Activity parentActivity, int requestCode, pendingWrite.getUnsignedTransaction())
Check the activity result
parentActivity.onActivityResult(int requestCode, int resultCode, Intent data)
In the sample case instead of waiting for the transaction receipt you can also just read the value again and check if it has already changed.