@@ -36,6 +36,7 @@ good place to start:
36
36
- `Writing a contract <https://ethereum-homestead.readthedocs.io/en/latest/contracts-and-transactions/contracts.html#writing-a-contract >`_
37
37
in the Ethereum Homestead Guide
38
38
39
+ .. _compiling-solidity :
39
40
40
41
Compiling Solidity source code
41
42
------------------------------
@@ -86,10 +87,16 @@ in the Homestead documentation.
86
87
87
88
88
89
Deploying and interacting with smart contracts
89
- -------------------------------------
90
+ ----------------------------------------------
90
91
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.
93
100
94
101
95
102
Smart contract examples
@@ -103,6 +110,8 @@ contracts in the project directory
103
110
`src/integration-test/java/org/web3j/protocol/scenarios <https://github.com/web3j/web3j/tree/master/src/integration-test/java/org/web3j/protocol/scenarios >`_.
104
111
105
112
113
+ .. _eip :
114
+
106
115
EIP-20 Ethereum token standard smart contract
107
116
---------------------------------------------
108
117
@@ -116,9 +125,17 @@ However, there is an implementation provided in
116
125
which has been taken from Consensys' implementation on
117
126
`GitHub <https://github.com/ConsenSys/Tokens >`_.
118
127
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
120
138
`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.
122
139
123
140
124
141
.. _smart-contract-wrappers :
@@ -128,18 +145,93 @@ Solidity smart contract wrappers
128
145
129
146
web3j supports the auto-generation of smart contract function wrappers in Java from Solidity ABI files.
130
147
131
- This can be achieved by running:
132
-
133
148
.. code-block :: bash
134
149
135
150
org.web3j.codegen.SolidityFunctionWrapperGenerator /path/to/< smart-contract> .abi -o /path/to/src/dir/java -p com.your.organisation.name
136
151
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
+
139
233
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
+ --------
143
236
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 `.
0 commit comments