-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathServer.java
More file actions
693 lines (647 loc) · 29.7 KB
/
Server.java
File metadata and controls
693 lines (647 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package org.stellar.sdk;
import com.google.gson.reflect.TypeToken;
import java.io.Closeable;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import org.stellar.sdk.exception.*;
import org.stellar.sdk.http.IHttpClient;
import org.stellar.sdk.http.Jdk11HttpClient;
import org.stellar.sdk.http.PostRequest;
import org.stellar.sdk.http.StringResponse;
import org.stellar.sdk.http.sse.ISseClient;
import org.stellar.sdk.operations.*;
import org.stellar.sdk.requests.*;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.FeeStatsResponse;
import org.stellar.sdk.responses.SubmitTransactionAsyncResponse;
import org.stellar.sdk.responses.TransactionResponse;
import org.stellar.sdk.xdr.CryptoKeyType;
/** Main class used to connect to Horizon server. */
public class Server implements Closeable {
private final URI serverURI;
@Getter @Setter private IHttpClient httpClient;
/** submitHttpClient is used only for submitting transactions. The read timeout is longer. */
@Getter @Setter private IHttpClient submitHttpClient;
@Getter @Setter private ISseClient sseClient;
/**
* HORIZON_SUBMIT_TIMEOUT is a time in seconds after Horizon sends a timeout response after
* internal txsub timeout.
*/
private static final int HORIZON_SUBMIT_TIMEOUT = 60;
/**
* ACCOUNT_REQUIRES_MEMO_VALUE is the base64 encoding of "1". SEP 29 uses this value to define
* transaction memo requirements for incoming payments.
*/
private static final String ACCOUNT_REQUIRES_MEMO_VALUE = "MQ==";
/** ACCOUNT_REQUIRES_MEMO_KEY is the data name described in SEP 29. */
private static final String ACCOUNT_REQUIRES_MEMO_KEY = "config.memo_required";
/**
* Constructs a new Server object with default HTTP clients.
*
* @param uri The URI of the Horizon server.
*/
public Server(String uri) {
this(uri, normalHttpClient(), submitHttpClient());
}
private static IHttpClient normalHttpClient() {
return new Jdk11HttpClient.Builder()
.withDefaultHeader("X-Client-Name", "java-stellar-sdk")
.withDefaultHeader("X-Client-Version", Util.getSdkVersion())
.withConnectTimeout(10, ChronoUnit.SECONDS)
.withReadTimeout(30, ChronoUnit.SECONDS)
.withRetryOnConnectionFailure(true)
.build();
}
private static IHttpClient submitHttpClient() {
return new Jdk11HttpClient.Builder()
.withDefaultHeader("X-Client-Name", "java-stellar-sdk")
.withDefaultHeader("X-Client-Version", Util.getSdkVersion())
.withConnectTimeout(10, ChronoUnit.SECONDS)
.withReadTimeout(HORIZON_SUBMIT_TIMEOUT + 5, ChronoUnit.SECONDS)
.withRetryOnConnectionFailure(true)
.build();
}
/**
* Constructs a new Server object with custom HTTP clients.
*
* @param serverURI The URI of the Horizon server.
* @param httpClient The IHttpClient to use for general requests.
* @param submitHttpClient The IHttpClient to use for submitting transactions.
*/
public Server(String serverURI, IHttpClient httpClient, IHttpClient submitHttpClient) {
try {
this.serverURI = new URI(serverURI);
} catch (URISyntaxException e) {
throw new RuntimeException("Invalid URI: " + serverURI);
}
this.httpClient = httpClient;
this.submitHttpClient = submitHttpClient;
}
/**
* Fetches an account's most current state in the ledger, then creates and returns an {@link
* Account} object.
*
* @param address The address of the account to load, muxed accounts are supported.
* @return {@link Account} object
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionBuilderAccount loadAccount(String address) {
MuxedAccount muxedAccount = new MuxedAccount(address);
AccountResponse accountResponse = this.accounts().account(muxedAccount.getAccountId());
return new Account(address, accountResponse.getSequenceNumber());
}
/**
* @return {@link RootRequestBuilder} instance.
*/
public RootRequestBuilder root() {
return new RootRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link AccountsRequestBuilder} instance.
*/
public AccountsRequestBuilder accounts() {
return new AccountsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link AssetsRequestBuilder} instance.
*/
public AssetsRequestBuilder assets() {
return new AssetsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link ClaimableBalancesRequestBuilder} instance.
*/
public ClaimableBalancesRequestBuilder claimableBalances() {
return new ClaimableBalancesRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link EffectsRequestBuilder} instance.
*/
public EffectsRequestBuilder effects() {
return new EffectsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link LedgersRequestBuilder} instance.
*/
public LedgersRequestBuilder ledgers() {
return new LedgersRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link OffersRequestBuilder} instance.
*/
public OffersRequestBuilder offers() {
return new OffersRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link OperationsRequestBuilder} instance.
*/
public OperationsRequestBuilder operations() {
return new OperationsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link FeeStatsResponse} instance.
*/
public FeeStatsRequestBuilder feeStats() {
return new FeeStatsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link OrderBookRequestBuilder} instance.
*/
public OrderBookRequestBuilder orderBook() {
return new OrderBookRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link TradesRequestBuilder} instance.
*/
public TradesRequestBuilder trades() {
return new TradesRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link TradeAggregationsRequestBuilder} instance.
*/
public TradeAggregationsRequestBuilder tradeAggregations(
Asset baseAsset,
Asset counterAsset,
long startTime,
long endTime,
long resolution,
long offset) {
return new TradeAggregationsRequestBuilder(
httpClient,
sseClient,
serverURI,
baseAsset,
counterAsset,
startTime,
endTime,
resolution,
offset);
}
/**
* @return {@link StrictReceivePathsRequestBuilder} instance.
*/
public StrictReceivePathsRequestBuilder strictReceivePaths() {
return new StrictReceivePathsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link StrictSendPathsRequestBuilder} instance.
*/
public StrictSendPathsRequestBuilder strictSendPaths() {
return new StrictSendPathsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link PaymentsRequestBuilder} instance.
*/
public PaymentsRequestBuilder payments() {
return new PaymentsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link TransactionsRequestBuilder} instance.
*/
public TransactionsRequestBuilder transactions() {
return new TransactionsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* @return {@link LiquidityPoolsRequestBuilder} instance.
*/
public LiquidityPoolsRequestBuilder liquidityPools() {
return new LiquidityPoolsRequestBuilder(httpClient, sseClient, serverURI);
}
/**
* Submits a base64 encoded transaction envelope to the network
*
* @param transactionXdr base64 encoded transaction envelope to submit to the network
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionResponse submitTransactionXdr(String transactionXdr) {
final var transactionsURI = new UriBuilder(serverURI).addPathSegment("transactions").build();
final var form = Map.of("tx", transactionXdr);
final var post = PostRequest.formBody(transactionsURI, form);
TypeToken<TransactionResponse> type = new TypeToken<TransactionResponse>() {};
ResponseHandler<TransactionResponse> responseHandler = new ResponseHandler<>(type);
StringResponse response;
try {
response = this.submitHttpClient.post(post);
} catch (SocketTimeoutException e) {
throw new RequestTimeoutException(e);
} catch (IOException e) {
if (e.getMessage().contains("request timed out")) {
throw new RequestTimeoutException(e);
} else {
throw new ConnectionErrorException(e);
}
}
return responseHandler.handleResponse(response);
}
/**
* Submits a transaction to the network
*
* @param transaction transaction to submit to the network
* @param skipMemoRequiredCheck set to true to skip memoRequiredCheck
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionResponse submitTransaction(
Transaction transaction, boolean skipMemoRequiredCheck) {
if (!skipMemoRequiredCheck) {
checkMemoRequired(transaction);
}
return this.submitTransactionXdr(transaction.toEnvelopeXdrBase64());
}
/**
* Submits a fee bump transaction to the network
*
* @param transaction transaction to submit to the network
* @param skipMemoRequiredCheck set to true to skip memoRequiredCheck
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionResponse submitTransaction(
FeeBumpTransaction transaction, boolean skipMemoRequiredCheck) {
if (!skipMemoRequiredCheck) {
checkMemoRequired(transaction.getInnerTransaction());
}
return this.submitTransactionXdr(transaction.toEnvelopeXdrBase64());
}
/**
* Submits a transaction to the network
*
* <p>This function will always check if the destination account requires a memo in the
* transaction as defined in <a
* href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md"
* target="_blank">SEP-0029</a> If you want to skip this check, use {@link
* Server#submitTransaction(Transaction, boolean)}.
*
* @param transaction transaction to submit to the network.
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionResponse submitTransaction(Transaction transaction) {
return submitTransaction(transaction, false);
}
/**
* Submits a fee bump transaction to the network
*
* <p>This function will always check if the destination account requires a memo in the
* transaction as defined in <a
* href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md"
* target="_blank">SEP-0029</a> If you want to skip this check, use {@link
* Server#submitTransaction(Transaction, boolean)}.
*
* @param transaction transaction to submit to the network.
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
*/
public TransactionResponse submitTransaction(FeeBumpTransaction transaction) {
return submitTransaction(transaction, false);
}
/**
* Submits a base64 asynchronous transaction to the network. Unlike the synchronous version, which
* blocks and waits for the transaction to be ingested in Horizon, this endpoint relays the
* response from core directly back to the user.
*
* @param transactionXdr base64 encoded transaction envelope to submit to the network
* @return {@link SubmitTransactionAsyncResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/horizon/api-reference/submit-async-transaction">Submit
* a Transaction Asynchronously</a>
*/
public SubmitTransactionAsyncResponse submitTransactionXdrAsync(String transactionXdr) {
final var transactionsURI =
new UriBuilder(serverURI).addPathSegment("transactions_async").build();
final var form = Map.of("tx", transactionXdr);
final var post = PostRequest.formBody(transactionsURI, form);
TypeToken<SubmitTransactionAsyncResponse> type =
new TypeToken<SubmitTransactionAsyncResponse>() {};
ResponseHandler<SubmitTransactionAsyncResponse> responseHandler = new ResponseHandler<>(type);
StringResponse response;
try {
response = this.submitHttpClient.post(post);
} catch (SocketTimeoutException e) {
throw new RequestTimeoutException(e);
} catch (IOException e) {
throw new ConnectionErrorException(e);
}
return responseHandler.handleResponse(response, true);
}
/**
* Submits a base64 asynchronous transaction to the network. Unlike the synchronous version, which
* blocks and waits for the transaction to be ingested in Horizon, this endpoint relays the
* response from core directly back to the user.
*
* @param transaction transaction to submit to the network
* @param skipMemoRequiredCheck set to true to skip memoRequiredCheck
* @return {@link TransactionResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/horizon/api-reference/submit-async-transaction">Submit
* a Transaction Asynchronously</a>
*/
public SubmitTransactionAsyncResponse submitTransactionAsync(
Transaction transaction, boolean skipMemoRequiredCheck) {
if (!skipMemoRequiredCheck) {
checkMemoRequired(transaction);
}
return this.submitTransactionXdrAsync(transaction.toEnvelopeXdrBase64());
}
/**
* Submits a base64 asynchronous transaction to the network. Unlike the synchronous version, which
* blocks and waits for the transaction to be ingested in Horizon, this endpoint relays the
* response from core directly back to the user.
*
* @param transaction transaction to submit to the network
* @param skipMemoRequiredCheck set to true to skip memoRequiredCheck
* @return {@link SubmitTransactionAsyncResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/horizon/api-reference/submit-async-transaction">Submit
* a Transaction Asynchronously</a>
*/
public SubmitTransactionAsyncResponse submitTransactionAsync(
FeeBumpTransaction transaction, boolean skipMemoRequiredCheck) {
if (!skipMemoRequiredCheck) {
checkMemoRequired(transaction.getInnerTransaction());
}
return this.submitTransactionXdrAsync(transaction.toEnvelopeXdrBase64());
}
/**
* Submits a base64 asynchronous transaction to the network. Unlike the synchronous version, which
* blocks and waits for the transaction to be ingested in Horizon, this endpoint relays the
* response from core directly back to the user.
*
* <p>This function will always check if the destination account requires a memo in the
* transaction as defined in <a
* href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md"
* target="_blank">SEP-0029</a> If you want to skip this check, use {@link
* Server#submitTransactionAsync(Transaction, boolean)}.
*
* @param transaction transaction to submit to the network.
* @return {@link SubmitTransactionAsyncResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/horizon/api-reference/submit-async-transaction">Submit
* a Transaction Asynchronously</a>
*/
public SubmitTransactionAsyncResponse submitTransactionAsync(Transaction transaction) {
return submitTransactionAsync(transaction, false);
}
/**
* Submits a base64 asynchronous transaction to the network. Unlike the synchronous version, which
* blocks and waits for the transaction to be ingested in Horizon, this endpoint relays the
* response from core directly back to the user.
*
* <p>This function will always check if the destination account requires a memo in the
* transaction as defined in <a
* href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md"
* target="_blank">SEP-0029</a> If you want to skip this check, use {@link
* Server#submitTransactionAsync(Transaction, boolean)}.
*
* @param transaction transaction to submit to the network.
* @return {@link SubmitTransactionAsyncResponse}
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws org.stellar.sdk.exception.BadRequestException if the request fails due to a bad request
* (4xx)
* @throws org.stellar.sdk.exception.BadResponseException if the request fails due to a bad
* response from the server (5xx)
* @throws TooManyRequestsException if the request fails due to too many requests sent to the
* server
* @throws org.stellar.sdk.exception.RequestTimeoutException When Horizon returns a <code>Timeout
* </code> or connection timeout occurred
* @throws org.stellar.sdk.exception.UnknownResponseException if the server returns an unknown
* status code
* @throws org.stellar.sdk.exception.ConnectionErrorException When the request cannot be executed
* due to cancellation or connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/horizon/api-reference/submit-async-transaction">Submit
* a Transaction Asynchronously</a>
*/
public SubmitTransactionAsyncResponse submitTransactionAsync(FeeBumpTransaction transaction) {
return submitTransactionAsync(transaction, false);
}
private boolean hashMemoId(String muxedAccount) {
return StrKey.encodeToXDRMuxedAccount(muxedAccount).getDiscriminant()
== CryptoKeyType.KEY_TYPE_MUXED_ED25519;
}
/**
* checkMemoRequired implements a memo required check as defined in <a
* href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md"
* target="_blank">SEP-0029</a>
*
* @param transaction transaction to submit to the network.
* @throws AccountRequiresMemoException when a transaction is trying to submit an operation to an
* account which requires a memo.
*/
private void checkMemoRequired(Transaction transaction) {
if (!transaction.getMemo().equals(Memo.none())) {
return;
}
Set<String> destinations = new HashSet<>();
Operation[] operations = transaction.getOperations();
for (int i = 0; i < operations.length; i++) {
String destination;
Operation operation = operations[i];
if (operation instanceof PaymentOperation) {
destination = ((PaymentOperation) operation).getDestination();
} else if (operation instanceof PathPaymentStrictReceiveOperation) {
destination = ((PathPaymentStrictReceiveOperation) operation).getDestination();
} else if (operation instanceof PathPaymentStrictSendOperation) {
destination = ((PathPaymentStrictSendOperation) operation).getDestination();
} else if (operation instanceof AccountMergeOperation) {
destination = ((AccountMergeOperation) operation).getDestination();
} else {
continue;
}
if (destinations.contains(destination) || hashMemoId(destination)) {
continue;
}
destinations.add(destination);
AccountResponse.Data data;
try {
data = this.accounts().account(destination).getData();
} catch (BadRequestException e) {
if (e.getCode() == 404) {
continue;
}
throw e;
}
if (ACCOUNT_REQUIRES_MEMO_VALUE.equals(data.get(ACCOUNT_REQUIRES_MEMO_KEY))) {
throw new AccountRequiresMemoException(
"Destination account requires a memo in the transaction.", destination, i);
}
}
}
@Override
public void close() {
try {
this.httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
this.submitHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}