13
13
import org .web3j .abi .datatypes .Function ;
14
14
import org .web3j .abi .datatypes .Type ;
15
15
import org .web3j .crypto .Credentials ;
16
- import org .web3j .crypto .TransactionEncoder ;
17
16
import org .web3j .protocol .Web3j ;
18
17
import org .web3j .protocol .core .DefaultBlockParameterName ;
19
18
import org .web3j .protocol .core .methods .request .RawTransaction ;
20
19
import org .web3j .protocol .core .methods .request .Transaction ;
21
20
import org .web3j .protocol .core .methods .response .*;
22
21
import org .web3j .protocol .exceptions .TransactionTimeoutException ;
23
- import org .web3j .utils .Numeric ;
24
22
25
23
26
24
/**
27
25
* Solidity contract type abstraction for interacting with smart contracts via native Java types.
28
26
*/
29
- public abstract class Contract {
27
+ public abstract class Contract extends ManagedTransaction {
30
28
31
29
private static final BigInteger GAS_PRICE = BigInteger .valueOf (50_000_000_000L );
32
30
private static final BigInteger GAS_LIMIT = BigInteger .valueOf (2_000_000 );
33
31
34
- private static final int SLEEP_DURATION = 15000 ;
35
- private static final int ATTEMPTS = 40 ;
36
-
37
32
private String contractAddress ;
38
- private Web3j web3j ;
39
- private Credentials credentials ;
40
33
41
34
protected Contract (String contractAddress , Web3j web3j , Credentials credentials ) {
35
+ super (web3j , credentials );
42
36
this .contractAddress = contractAddress ;
43
- this .web3j = web3j ;
44
- this .credentials = credentials ;
45
37
}
46
38
47
39
public String getContractAddress () {
@@ -138,21 +130,6 @@ protected TransactionReceipt executeTransaction(
138
130
return signAndSend (rawTransaction );
139
131
}
140
132
141
- private TransactionReceipt signAndSend (RawTransaction rawTransaction )
142
- throws InterruptedException , ExecutionException , TransactionTimeoutException {
143
- byte [] signedMessage = TransactionEncoder .signMessage (rawTransaction , credentials );
144
- String hexValue = Numeric .toHexString (signedMessage );
145
-
146
- // This might be a good candidate for using functional composition with CompletableFutures
147
- EthSendTransaction transactionResponse = web3j .ethSendRawTransaction (hexValue )
148
- .sendAsync ().get ();
149
-
150
- String transactionHash = transactionResponse .getTransactionHash ();
151
-
152
- return waitForTransactionReceipt (transactionHash );
153
- }
154
-
155
-
156
133
/**
157
134
* Execute the provided function as a transaction asynchronously.
158
135
*
@@ -173,47 +150,6 @@ protected Future<TransactionReceipt> executeTransactionAsync(Function function)
173
150
return result ;
174
151
}
175
152
176
- private BigInteger getNonce (String address ) throws InterruptedException , ExecutionException {
177
- EthGetTransactionCount ethGetTransactionCount = web3j .ethGetTransactionCount (
178
- address , DefaultBlockParameterName .LATEST ).sendAsync ().get ();
179
-
180
- return ethGetTransactionCount .getTransactionCount ();
181
- }
182
-
183
- private TransactionReceipt waitForTransactionReceipt (
184
- String transactionHash ) throws InterruptedException , ExecutionException ,
185
- TransactionTimeoutException {
186
-
187
- return getTransactionReceipt (transactionHash , SLEEP_DURATION , ATTEMPTS );
188
- }
189
-
190
- private TransactionReceipt getTransactionReceipt (
191
- String transactionHash , int sleepDuration , int attempts )
192
- throws InterruptedException , ExecutionException , TransactionTimeoutException {
193
-
194
- Optional <TransactionReceipt > receiptOptional =
195
- sendTransactionReceiptRequest (transactionHash );
196
- for (int i = 0 ; i < attempts ; i ++) {
197
- if (!receiptOptional .isPresent ()) {
198
- Thread .sleep (sleepDuration );
199
- receiptOptional = sendTransactionReceiptRequest (transactionHash );
200
- } else {
201
- return receiptOptional .get ();
202
- }
203
- }
204
-
205
- throw new TransactionTimeoutException ("Transaction receipt was not generated after " +
206
- ((sleepDuration * attempts ) / 1000 + " seconds" ));
207
- }
208
-
209
- private Optional <TransactionReceipt > sendTransactionReceiptRequest (
210
- String transactionHash ) throws InterruptedException , ExecutionException {
211
- EthGetTransactionReceipt transactionReceipt =
212
- web3j .ethGetTransactionReceipt (transactionHash ).sendAsync ().get ();
213
-
214
- return transactionReceipt .getTransactionReceipt ();
215
- }
216
-
217
153
protected EventValues extractEventParameters (
218
154
Event event , TransactionReceipt transactionReceipt ) {
219
155
0 commit comments