Skip to content

Commit 586a0e6

Browse files
committed
1. Added support for transactions and event processing in generated smart contract code.
2. Pulled TransactionReceipt into it's own class. 3. Removed parameterized type from Event implementation. 4. Bumped version.
1 parent 44e1e26 commit 586a0e6

36 files changed

+1309
-412
lines changed

README.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Maven
5151
<dependency>
5252
<groupId>org.web3j</groupId>
5353
<artifactId>core</artifactId>
54-
<version>0.6.0</version>
54+
<version>0.7.0</version>
5555
</dependency>
5656
5757
Gradle
@@ -126,10 +126,10 @@ This can be achieved by running:
126126

127127
.. code-block:: bash
128128
129-
$ org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name
129+
$ 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
130130
131131
132-
See `FunctionWrappersIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java>`_
132+
See `HumanStandardTokenGeneratedIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenGeneratedIT.java>`_
133133
for an example of using a generated smart contract Java wrapper.
134134

135135

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ apply plugin: 'idea'
2323
apply plugin: 'io.codearte.nexus-staging'
2424

2525
group 'org.web3j'
26-
version '0.6.0'
26+
version '0.7.0'
2727

2828
sourceCompatibility = 1.8
2929

docs/source/filters.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Filters
22
=======
33

4-
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+
Please refer to `EventFilterIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/EventFilterIT.java>`_
5+
for an example of how to do this until this section is written.

docs/source/getting_started.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Maven
1111
<dependency>
1212
<groupId>org.web3j</groupId>
1313
<artifactId>core</artifactId>
14-
<version>0.6.0</version>
14+
<version>0.7.0</version>
1515
</dependency>
1616
1717
Gradle
1818
------
1919

2020
.. code-block:: groovy
2121
22-
compile ('org.web3j:core:0.6.0')
22+
compile ('org.web3j:core:0.7.0')
2323
2424
2525
Start a client

docs/source/smart_contracts.rst

+106-14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ good place to start:
3636
- `Writing a contract <https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#writing-a-contract>`_
3737
in the Ethereum Homestead Guide
3838

39+
.. _compiling-solidity:
3940

4041
Compiling Solidity source code
4142
------------------------------
@@ -86,10 +87,16 @@ in the Homestead documentation.
8687

8788

8889
Deploying and interacting with smart contracts
89-
-------------------------------------
90+
----------------------------------------------
9091

91-
Please refer to the sections :ref:`creation-of-smart-contract`, :ref:`transacting-with-contract`
92-
and :ref:`querying-state` for details.
92+
If you want to avoid the underlying implementation detail for working with smart contracts, web3j
93+
provides :ref:`smart-contract-wrappers` which enable you to interact directly with all of a smart
94+
contract's methods via a generated wrapper object.
95+
96+
Alternatively, if you wish to send regular transactions or have more control over your
97+
interactions with your smart contracts, please refer to the sections
98+
:ref:`creation-of-smart-contract`, :ref:`transacting-with-contract` and :ref:`querying-state`
99+
for details.
93100

94101

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

105112

113+
.. _eip:
114+
106115
EIP-20 Ethereum token standard smart contract
107116
---------------------------------------------
108117

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

119-
The integration test
128+
There are two integration tests that have been written to fully demonstrate the functionality of
129+
this token smart contract.
130+
131+
`HumanStandardTokenGeneratedIT <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenGeneratedIT.java>`_
132+
uses the generated
133+
`HumanStandardTokenGenerated <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/generated/HumanStandardTokenGenerated.java>`_
134+
:ref:`smart contract wrapper <smart-contract-wrappers>` to demonstrate this.
135+
136+
Alternatively, if you do not wish to use a smart contract wrapper and would like to work directly
137+
with the JSON-RPC calls, please refer to
120138
`HumanStandardTokenIT <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios/HumanStandardTokenIT.java>`_.
121-
has been been written to fully demonstrate the functionality of this token standard smart contract.
122139

123140

124141
.. _smart-contract-wrappers:
@@ -128,18 +145,93 @@ Solidity smart contract wrappers
128145

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

131-
This can be achieved by running:
132-
133148
.. code-block:: bash
134149
135150
org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/<smart-contract>.abi -o /path/to/src/dir/java -p com.your.organisation.name
136151
137-
See `FunctionWrappersIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/FunctionWrappersIT.java>`_
138-
for an example of using a generated smart contract Java wrapper.
152+
Where the *bin* and *abi* are obtained as per :ref:`compiling-solidity`.
153+
154+
The smart contract wrappers support all common operations for working with smart contracts:
155+
156+
- :ref:`construction-and-deployment`
157+
- :ref:`invoking-transactions`
158+
- :ref:`constant-methods`
159+
160+
Any method calls that requires an underlying JSON-RPC call to take place will return a Future to
161+
avoid blocking.
162+
163+
164+
.. _construction-and-deployment:
165+
166+
Construction and deployment
167+
---------------------------
168+
169+
Construction and deployment of smart contracts happens with the *deploy* method::
170+
171+
YourSmartContract contract = YourSmartContract.deploy(
172+
<web3j>, <credentials>, <initialValue>,
173+
<param1>, ..., <paramN>) {
174+
175+
This will create a new instance of the smart contract on the Ethereum blockchain using the
176+
supplied credentials, and constructor parameter values.
177+
178+
It returns a new smart contract wrapper instance which contains the underlying address of the
179+
smart contract. If you wish to construct an instance of a smart contract wrapper with an existing
180+
smart contract, simply pass in it's address::
181+
182+
YourSmartContract contract = new YourSmartContract("0x...", web3j, credentials);
183+
184+
185+
.. _invoking-transactions:
186+
187+
Invoking transactions and events
188+
--------------------------------
189+
190+
All transactional smart contract methods are named identically to their Solidity methods, taking
191+
the same parameter values. Transactional calls do not return any values, regardless of the return
192+
type specified on the method. Hence, for all transactional methods the
193+
`Transaction Receipt <https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionreceipt>`_
194+
associated with the transaction is returned.::
195+
196+
TransactionReceipt transactionReceipt = contract.someMethod(
197+
new Type(...),
198+
...).get();
199+
200+
201+
The transaction receipt is useful for two reasons:
202+
203+
#. It provides details of the mined block that the transaction resides in
204+
#. `Solidity events <http://solidity.readthedocs.io/en/develop/contracts.html?highlight=events#events>`_ that
205+
are called will be logged as part of the transaction, which can then be extracted
206+
207+
Any events defined within a smart contract will be represented in the smart contract wrapper with
208+
a method named *process<Event Name>Event*, which takes the Transaction Receipt and from this
209+
extracts the indexed and non-indexed event parameters, which are returned decoded in an instance of
210+
the
211+
`EventValues <https://github.com/web3j/web3j/blob/master/src/main/java/org/web3j/abi/EventValues.java>`_
212+
object.::
213+
214+
EventValues eventValues = contract.processSomeEvent(transactionReceipt);
215+
216+
**Remember** that for any indexed array, bytes and string Solidity parameter
217+
types, a Keccak-256 hash of their values will be returned, see the
218+
`documentation <http://solidity.readthedocs.io/en/latest/contracts.html#events>`_
219+
for further information.
220+
221+
222+
.. _constant-methods:
223+
224+
Calling constant methods
225+
------------------------
226+
227+
Constant methods are those that read a value in a smart contract, and do not alter the state of
228+
the smart contract. These methods are available with the same method signature as the smart
229+
contract they were generated from, the only addition is that the call is wrapped in a Future.::
230+
231+
Type result = contract.someMethod(new Type(...), ...).get();
232+
139233

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

144-
For an example of how to call a smart contracts via a transaction, refer to
145-
`DeployContractIT <https://github.com/web3j/web3j/blob/master/src/integration-test/java/org/web3j/protocol/scenarios/DeployContractIT.java>`_.
237+
Please refer to :ref:`eip`.

docs/source/trouble.rst

+9
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ If there is no sign of it then the transaction has vanished into the ether (sorr
3434
cause of this is likely to be to do with the transaction's nonce either not being set, or
3535
being too low. Please refer to the section :ref:`nonce` for more information.
3636

37+
38+
I want to see details of the JSON-RPC requests and responses
39+
------------------------------------------------------------
40+
41+
Set the following system properties in your main class::
42+
43+
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
44+
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
45+
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
package org.web3j.generated;
22

3-
import java.lang.InterruptedException;
43
import java.lang.String;
5-
import java.util.concurrent.ExecutionException;
4+
import java.util.Collections;
5+
import java.util.concurrent.Future;
66
import org.web3j.abi.Contract;
7-
import org.web3j.abi.TypeReference;
87
import org.web3j.abi.datatypes.DynamicArray;
98
import org.web3j.abi.datatypes.Function;
109
import org.web3j.abi.datatypes.StaticArray;
1110
import org.web3j.abi.datatypes.generated.Uint256;
11+
import org.web3j.crypto.Credentials;
1212
import org.web3j.protocol.Web3j;
13+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
1314

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

24-
public StaticArray<Uint256> fixedReverse(StaticArray<Uint256> input) throws InterruptedException, ExecutionException {
25-
Function function = new Function<>("fixedReverse",
26-
java.util.Arrays.asList(input),
27-
java.util.Arrays.asList(new TypeReference<StaticArray<Uint256>>() {}));
28-
return executeSingleValueReturn(function);
29-
}
23+
public Arrays(String contractAddress, Web3j web3j, Credentials credentials) {
24+
super(contractAddress, web3j, credentials);
25+
}
3026

31-
public DynamicArray<Uint256> dynamicReverse(DynamicArray<Uint256> input) throws InterruptedException, ExecutionException {
32-
Function function = new Function<>("dynamicReverse",
33-
java.util.Arrays.asList(input),
34-
java.util.Arrays.asList(new TypeReference<DynamicArray<Uint256>>() {}));
35-
return executeSingleValueReturn(function);
36-
}
27+
public Future<TransactionReceipt> fixedReverse(StaticArray<Uint256> input) {
28+
Function function = new Function<>("fixedReverse", java.util.Arrays.asList(input), Collections.emptyList());
29+
return executeTransactionAsync(function);
30+
}
31+
32+
public Future<TransactionReceipt> dynamicReverse(DynamicArray<Uint256> input) {
33+
Function function = new Function<>("dynamicReverse", java.util.Arrays.asList(input), Collections.emptyList());
34+
return executeTransactionAsync(function);
35+
}
3736
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
package org.web3j.generated;
22

3-
import java.lang.InterruptedException;
43
import java.lang.String;
54
import java.util.Arrays;
6-
import java.util.concurrent.ExecutionException;
5+
import java.util.Collections;
6+
import java.util.concurrent.Future;
77
import org.web3j.abi.Contract;
8+
import org.web3j.abi.EventValues;
89
import org.web3j.abi.TypeReference;
10+
import org.web3j.abi.datatypes.Event;
911
import org.web3j.abi.datatypes.Function;
1012
import org.web3j.abi.datatypes.generated.Uint256;
13+
import org.web3j.crypto.Credentials;
1114
import org.web3j.protocol.Web3j;
15+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
1216

1317
/**
1418
* <p>Auto generated code.<br>
1519
* <strong>Do not modifiy!</strong><br>
1620
* Please use {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update.</p>
1721
*/
1822
public final class Fibonacci extends Contract {
19-
public Fibonacci(String contractAddress, Web3j web3j) {
20-
super(contractAddress, web3j);
21-
}
23+
private static final String BINARY = "606060405260d18060106000396000f3606060405260e060020a60003504633c7fdc708114602657806361047ff4146044575b6002565b34600257605160043560006063825b600081151560a75750600060a2565b3460025760516004356035565b60408051918252519081900360200190f35b604080518481526020810183905281519293507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed929081900390910190a15b919050565b816001141560b65750600160a2565b60c0600283036035565b60ca600184036035565b01905060a256";
2224

23-
public Uint256 fibonacciNotify(Uint256 number) throws InterruptedException, ExecutionException {
24-
Function function = new Function<>("fibonacciNotify",
25-
Arrays.asList(number),
26-
Arrays.asList(new TypeReference<Uint256>() {}));
27-
return executeSingleValueReturn(function);
28-
}
25+
public Fibonacci(String contractAddress, Web3j web3j, Credentials credentials) {
26+
super(contractAddress, web3j, credentials);
27+
}
2928

30-
public Uint256 fibonacci(Uint256 number) throws InterruptedException, ExecutionException {
31-
Function function = new Function<>("fibonacci",
32-
Arrays.asList(number),
33-
Arrays.asList(new TypeReference<Uint256>() {}));
34-
return executeSingleValueReturn(function);
35-
}
29+
public Future<TransactionReceipt> fibonacciNotify(Uint256 number) {
30+
Function function = new Function<>("fibonacciNotify", Arrays.asList(number), Collections.emptyList());
31+
return executeTransactionAsync(function);
32+
}
33+
34+
public Future<Uint256> fibonacci(Uint256 number) {
35+
Function function = new Function<>("fibonacci",
36+
Arrays.asList(number),
37+
Arrays.asList(new TypeReference<Uint256>() {}));
38+
return executeCallSingleValueReturnAsync(function);
39+
}
40+
41+
public EventValues processNotifyEvent(TransactionReceipt transactionReceipt) {
42+
Event event = new Event("Notify",
43+
Arrays.asList(),
44+
Arrays.asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));
45+
return extractEventParameters(event, transactionReceipt);
46+
}
3647
}

0 commit comments

Comments
 (0)