|
5 | 5 | import java.io.Closeable; |
6 | 6 | import java.io.IOException; |
7 | 7 | import java.net.SocketTimeoutException; |
8 | | -import java.util.ArrayList; |
9 | | -import java.util.Collection; |
10 | | -import java.util.Collections; |
11 | | -import java.util.List; |
12 | | -import java.util.Optional; |
13 | | -import java.util.UUID; |
| 8 | +import java.util.*; |
14 | 9 | import java.util.concurrent.TimeUnit; |
15 | 10 | import okhttp3.HttpUrl; |
16 | 11 | import okhttp3.MediaType; |
|
36 | 31 | import org.stellar.sdk.requests.sorobanrpc.SendTransactionRequest; |
37 | 32 | import org.stellar.sdk.requests.sorobanrpc.SimulateTransactionRequest; |
38 | 33 | import org.stellar.sdk.requests.sorobanrpc.SorobanRpcRequest; |
39 | | -import org.stellar.sdk.responses.sorobanrpc.GetEventsResponse; |
40 | | -import org.stellar.sdk.responses.sorobanrpc.GetFeeStatsResponse; |
41 | | -import org.stellar.sdk.responses.sorobanrpc.GetHealthResponse; |
42 | | -import org.stellar.sdk.responses.sorobanrpc.GetLatestLedgerResponse; |
43 | | -import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse; |
44 | | -import org.stellar.sdk.responses.sorobanrpc.GetLedgersResponse; |
45 | | -import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse; |
46 | | -import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse; |
47 | | -import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse; |
48 | | -import org.stellar.sdk.responses.sorobanrpc.GetVersionInfoResponse; |
49 | | -import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse; |
50 | | -import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse; |
51 | | -import org.stellar.sdk.responses.sorobanrpc.SorobanRpcResponse; |
| 34 | +import org.stellar.sdk.responses.sorobanrpc.*; |
| 35 | +import org.stellar.sdk.scval.Scv; |
52 | 36 | import org.stellar.sdk.xdr.ContractDataDurability; |
53 | 37 | import org.stellar.sdk.xdr.LedgerEntry; |
54 | 38 | import org.stellar.sdk.xdr.LedgerEntryType; |
@@ -560,6 +544,52 @@ public SendTransactionResponse sendTransaction(Transaction transaction) { |
560 | 544 | "sendTransaction", params, new TypeToken<SorobanRpcResponse<SendTransactionResponse>>() {}); |
561 | 545 | } |
562 | 546 |
|
| 547 | + public GetSacBalanceResponse getSacBalance(String contractId, Asset asset, Network network) |
| 548 | + throws IOException { |
| 549 | + |
| 550 | + if (!StrKey.isValidContract(contractId)) { |
| 551 | + throw new IllegalArgumentException("expected contract ID, got " + contractId); |
| 552 | + } |
| 553 | + |
| 554 | + LedgerKey ledgerKey = |
| 555 | + LedgerKey.builder() |
| 556 | + .discriminant(LedgerEntryType.CONTRACT_DATA) |
| 557 | + .contractData( |
| 558 | + LedgerKey.LedgerKeyContractData.builder() |
| 559 | + .contract(Scv.toAddress(asset.getContractId(network)).getAddress()) |
| 560 | + .key( |
| 561 | + Scv.toVec( |
| 562 | + Arrays.asList(Scv.toSymbol("Balance"), Scv.toAddress(contractId)))) |
| 563 | + .durability(ContractDataDurability.PERSISTENT) |
| 564 | + .build()) |
| 565 | + .build(); |
| 566 | + |
| 567 | + GetLedgerEntriesResponse response = this.getLedgerEntries(Collections.singleton(ledgerKey)); |
| 568 | + |
| 569 | + List<GetLedgerEntriesResponse.LedgerEntryResult> entries = response.getEntries(); |
| 570 | + if (entries == null || entries.isEmpty()) { |
| 571 | + return null; |
| 572 | + } |
| 573 | + |
| 574 | + GetLedgerEntriesResponse.LedgerEntryResult entry = entries.get(0); |
| 575 | + LedgerEntry.LedgerEntryData ledgerEntryData = |
| 576 | + LedgerEntry.LedgerEntryData.fromXdrBase64(entry.getXdr()); |
| 577 | + LinkedHashMap<SCVal, SCVal> balanceMap = |
| 578 | + Scv.fromMap(ledgerEntryData.getContractData().getVal()); |
| 579 | + |
| 580 | + return GetSacBalanceResponse.builder() |
| 581 | + .latestLedger(response.getLatestLedger()) |
| 582 | + .balanceEntry( |
| 583 | + GetSacBalanceResponse.BalanceEntry.builder() |
| 584 | + .liveUntilLedgerSeq(entry.getLiveUntilLedger()) |
| 585 | + .lastModifiedLedgerSeq(entry.getLastModifiedLedger()) |
| 586 | + .amount(Scv.fromInt128(balanceMap.get(Scv.toSymbol("amount"))).toString()) |
| 587 | + .authorized(Scv.fromBoolean(balanceMap.get(Scv.toSymbol("authorized")))) |
| 588 | + .clawback(Scv.fromBoolean(balanceMap.get(Scv.toSymbol("clawback")))) |
| 589 | + .build()) |
| 590 | + .build(); |
| 591 | + } |
| 592 | + |
563 | 593 | public static Transaction assembleTransaction( |
564 | 594 | Transaction transaction, SimulateTransactionResponse simulateTransactionResponse) { |
565 | 595 | if (!transaction.isSorobanTransaction()) { |
|
0 commit comments