Skip to content

Commit

Permalink
1. Added support for transactions and event processing in generated s…
Browse files Browse the repository at this point in the history
…mart contract code.

2. Pulled TransactionReceipt into it's own class.
3. Removed parameterized type from Event implementation.
4. Bumped version.
  • Loading branch information
conor10 committed Nov 1, 2016
1 parent 44e1e26 commit 586a0e6
Show file tree
Hide file tree
Showing 36 changed files with 1,309 additions and 412 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Maven
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>
</dependency>
Gradle
Expand Down Expand Up @@ -126,10 +126,10 @@ This can be achieved by running:

.. code-block:: bash
$ org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name
$ org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name
See `FunctionWrappersIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java>`_
See `HumanStandardTokenGeneratedIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenGeneratedIT.java>`_
for an example of using a generated smart contract Java wrapper.


Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'idea'
apply plugin: 'io.codearte.nexus-staging'

group 'org.web3j'
version '0.6.0'
version '0.7.0'

sourceCompatibility = 1.8

Expand Down
3 changes: 2 additions & 1 deletion docs/source/filters.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Filters
=======

Please refer to `EventFilterIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/EventFilterIT.java>`_ for an example of how to do this until this section is written.
Please refer to `EventFilterIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/EventFilterIT.java>`_
for an example of how to do this until this section is written.
4 changes: 2 additions & 2 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Maven
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>
</dependency>
Gradle
------

.. code-block:: groovy
compile ('org.web3j:core:0.6.0')
compile ('org.web3j:core:0.7.0')
Start a client
Expand Down
120 changes: 106 additions & 14 deletions docs/source/smart_contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ good place to start:
- `Writing a contract <https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#writing-a-contract>`_
in the Ethereum Homestead Guide

.. _compiling-solidity:

Compiling Solidity source code
------------------------------
Expand Down Expand Up @@ -86,10 +87,16 @@ in the Homestead documentation.


Deploying and interacting with smart contracts
-------------------------------------
----------------------------------------------

Please refer to the sections :ref:`creation-of-smart-contract`, :ref:`transacting-with-contract`
and :ref:`querying-state` for details.
If you want to avoid the underlying implementation detail for working with smart contracts, web3j
provides :ref:`smart-contract-wrappers` which enable you to interact directly with all of a smart
contract's methods via a generated wrapper object.

Alternatively, if you wish to send regular transactions or have more control over your
interactions with your smart contracts, please refer to the sections
:ref:`creation-of-smart-contract`, :ref:`transacting-with-contract` and :ref:`querying-state`
for details.


Smart contract examples
Expand All @@ -103,6 +110,8 @@ contracts in the project directory
`src/integration-test/java/org/web3j/protocol/scenarios <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios>`_.


.. _eip:

EIP-20 Ethereum token standard smart contract
---------------------------------------------

Expand All @@ -116,9 +125,17 @@ However, there is an implementation provided in
which has been taken from Consensys' implementation on
`GitHub <https://github.com/ConsenSys/Tokens>`_.

The integration test
There are two integration tests that have been written to fully demonstrate the functionality of
this token smart contract.

`HumanStandardTokenGeneratedIT <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenGeneratedIT.java>`_
uses the generated
`HumanStandardTokenGenerated <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/generated/HumanStandardTokenGenerated.java>`_
:ref:`smart contract wrapper <smart-contract-wrappers>` to demonstrate this.

Alternatively, if you do not wish to use a smart contract wrapper and would like to work directly
with the JSON-RPC calls, please refer to
`HumanStandardTokenIT <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenIT.java>`_.
has been been written to fully demonstrate the functionality of this token standard smart contract.


.. _smart-contract-wrappers:
Expand All @@ -128,18 +145,93 @@ Solidity smart contract wrappers

web3j supports the auto-generation of smart contract function wrappers in Java from Solidity ABI files.

This can be achieved by running:

.. code-block:: bash
org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name
See `FunctionWrappersIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java>`_
for an example of using a generated smart contract Java wrapper.
Where the *bin* and *abi* are obtained as per :ref:`compiling-solidity`.

The smart contract wrappers support all common operations for working with smart contracts:

- :ref:`construction-and-deployment`
- :ref:`invoking-transactions`
- :ref:`constant-methods`

Any method calls that requires an underlying JSON-RPC call to take place will return a Future to
avoid blocking.


.. _construction-and-deployment:

Construction and deployment
---------------------------

Construction and deployment of smart contracts happens with the *deploy* method::

YourSmartContract contract = YourSmartContract.deploy(
<web3j>, <credentials>, <initialValue>,
<param1>, ..., <paramN>) {

This will create a new instance of the smart contract on the Ethereum blockchain using the
supplied credentials, and constructor parameter values.

It returns a new smart contract wrapper instance which contains the underlying address of the
smart contract. If you wish to construct an instance of a smart contract wrapper with an existing
smart contract, simply pass in it's address::

YourSmartContract contract = new YourSmartContract("0x...", web3j, credentials);


.. _invoking-transactions:

Invoking transactions and events
--------------------------------

All transactional smart contract methods are named identically to their Solidity methods, taking
the same parameter values. Transactional calls do not return any values, regardless of the return
type specified on the method. Hence, for all transactional methods the
`Transaction Receipt <https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionreceipt>`_
associated with the transaction is returned.::

TransactionReceipt transactionReceipt = contract.someMethod(
new Type(...),
...).get();


The transaction receipt is useful for two reasons:

#. It provides details of the mined block that the transaction resides in
#. `Solidity events <http://solidity.readthedocs.io/en/develop/contracts.html?highlight=events#events>`_ that
are called will be logged as part of the transaction, which can then be extracted

Any events defined within a smart contract will be represented in the smart contract wrapper with
a method named *process<Event Name>Event*, which takes the Transaction Receipt and from this
extracts the indexed and non-indexed event parameters, which are returned decoded in an instance of
the
`EventValues <https://github.com/web3j/web3j/blob/master/src/main/java/org/web3j/abi/EventValues.java>`_
object.::

EventValues eventValues = contract.processSomeEvent(transactionReceipt);

**Remember** that for any indexed array, bytes and string Solidity parameter
types, a Keccak-256 hash of their values will be returned, see the
`documentation <http://solidity.readthedocs.io/en/latest/contracts.html#events>`_
for further information.


.. _constant-methods:

Calling constant methods
------------------------

Constant methods are those that read a value in a smart contract, and do not alter the state of
the smart contract. These methods are available with the same method signature as the smart
contract they were generated from, the only addition is that the call is wrapped in a Future.::

Type result = contract.someMethod(new Type(...), ...).get();


**Note:** at present the wrappers invoke smart contracts via
`EthCall <https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call>`_,
so a transaction does not take place. Transaction support is imminent.
Examples
--------

For an example of how to call a smart contracts via a transaction, refer to
`DeployContractIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/DeployContractIT.java>`_.
Please refer to :ref:`eip`.
9 changes: 9 additions & 0 deletions docs/source/trouble.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ If there is no sign of it then the transaction has vanished into the ether (sorr
cause of this is likely to be to do with the transaction's nonce either not being set, or
being too low. Please refer to the section :ref:`nonce` for more information.


I want to see details of the JSON-RPC requests and responses
------------------------------------------------------------

Set the following system properties in your main class::

System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
35 changes: 17 additions & 18 deletions src/integration-test/java/org/web3j/generated/Arrays.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package org.web3j.generated;

import java.lang.InterruptedException;
import java.lang.String;
import java.util.concurrent.ExecutionException;
import java.util.Collections;
import java.util.concurrent.Future;
import org.web3j.abi.Contract;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.StaticArray;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

/**
* <p>Auto generated code.<br>
* <strong>Do not modifiy!</strong><br>
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update.</p>
*/
public final class Arrays extends Contract {
public Arrays(String contractAddress, Web3j web3j) {
super(contractAddress, web3j);
}
private static final String BINARY = "60606040526101f5806100126000396000f3606060405260e060020a6000350463b96f54d18114610029578063beda363b146100bc575b610002565b34610002576040805161014081810190925261018191600491610144918390600a90839083908082843750909550505050505061014060405190810160405280600a905b600081526020019060019003908161006d5750600a905060005b818110156101ee5783816001018303600a8110156100025760200201518382600a811015610002576020020152600101610087565b3461000257604080516020600480358082013583810280860185019096528085526101a49592946024949093928501928291850190849080828437509496505050505050506040805160208101825260008082528351925191929182908059106101235750595b90808252806020026020018201604052801561013a575b509250600090505b818110156101ee578381600101830381518110156100025790602001906020020151838281518110156100025760209081029091010152600101610142565b60405180826101408083818460006004602df15090500191505060405180910390f35b60405180806020018281038252838181518152602001915080519060200190602002808383829060006004602084601f0104600302600f01f1509050019250505060405180910390f35b505091905056";

public StaticArray<Uint256> fixedReverse(StaticArray<Uint256> input) throws InterruptedException, ExecutionException {
Function function = new Function<>("fixedReverse",
java.util.Arrays.asList(input),
java.util.Arrays.asList(new TypeReference<StaticArray<Uint256>>() {}));
return executeSingleValueReturn(function);
}
public Arrays(String contractAddress, Web3j web3j, Credentials credentials) {
super(contractAddress, web3j, credentials);
}

public DynamicArray<Uint256> dynamicReverse(DynamicArray<Uint256> input) throws InterruptedException, ExecutionException {
Function function = new Function<>("dynamicReverse",
java.util.Arrays.asList(input),
java.util.Arrays.asList(new TypeReference<DynamicArray<Uint256>>() {}));
return executeSingleValueReturn(function);
}
public Future<TransactionReceipt> fixedReverse(StaticArray<Uint256> input) {
Function function = new Function<>("fixedReverse", java.util.Arrays.asList(input), Collections.emptyList());
return executeTransactionAsync(function);
}

public Future<TransactionReceipt> dynamicReverse(DynamicArray<Uint256> input) {
Function function = new Function<>("dynamicReverse", java.util.Arrays.asList(input), Collections.emptyList());
return executeTransactionAsync(function);
}
}
45 changes: 28 additions & 17 deletions src/integration-test/java/org/web3j/generated/Fibonacci.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
package org.web3j.generated;

import java.lang.InterruptedException;
import java.lang.String;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.Collections;
import java.util.concurrent.Future;
import org.web3j.abi.Contract;
import org.web3j.abi.EventValues;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

/**
* <p>Auto generated code.<br>
* <strong>Do not modifiy!</strong><br>
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update.</p>
*/
public final class Fibonacci extends Contract {
public Fibonacci(String contractAddress, Web3j web3j) {
super(contractAddress, web3j);
}
private static final String BINARY = "606060405260d18060106000396000f3606060405260e060020a60003504633c7fdc708114602657806361047ff4146044575b6002565b34600257605160043560006063825b600081151560a75750600060a2565b3460025760516004356035565b60408051918252519081900360200190f35b604080518481526020810183905281519293507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed929081900390910190a15b919050565b816001141560b65750600160a2565b60c0600283036035565b60ca600184036035565b01905060a256";

public Uint256 fibonacciNotify(Uint256 number) throws InterruptedException, ExecutionException {
Function function = new Function<>("fibonacciNotify",
Arrays.asList(number),
Arrays.asList(new TypeReference<Uint256>() {}));
return executeSingleValueReturn(function);
}
public Fibonacci(String contractAddress, Web3j web3j, Credentials credentials) {
super(contractAddress, web3j, credentials);
}

public Uint256 fibonacci(Uint256 number) throws InterruptedException, ExecutionException {
Function function = new Function<>("fibonacci",
Arrays.asList(number),
Arrays.asList(new TypeReference<Uint256>() {}));
return executeSingleValueReturn(function);
}
public Future<TransactionReceipt> fibonacciNotify(Uint256 number) {
Function function = new Function<>("fibonacciNotify", Arrays.asList(number), Collections.emptyList());
return executeTransactionAsync(function);
}

public Future<Uint256> fibonacci(Uint256 number) {
Function function = new Function<>("fibonacci",
Arrays.asList(number),
Arrays.asList(new TypeReference<Uint256>() {}));
return executeCallSingleValueReturnAsync(function);
}

public EventValues processNotifyEvent(TransactionReceipt transactionReceipt) {
Event event = new Event("Notify",
Arrays.asList(),
Arrays.asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
return extractEventParameters(event, transactionReceipt);
}
}
Loading

0 comments on commit 586a0e6

Please sign in to comment.