Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 49 additions & 19 deletions src/main/java/org/stellar/sdk/SorobanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using wildcard imports. I know both have their advantages, but this has been discussed in other PRs before. Since we have a standard now, let's adhere to it.
https://github.com/lightsail-network/java-stellar-sdk/blob/master/CONTRIBUTING.md#code-style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually from the auto formatter, which is google-java-formatter. What do you think we let the formatter handle this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think google-java-formatter does this; are you using IDEA? IDEA does this by default.
https://www.baeldung.com/intellij-disable-wildcard-import

import java.util.concurrent.TimeUnit;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
Expand All @@ -36,19 +31,8 @@
import org.stellar.sdk.requests.sorobanrpc.SendTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SimulateTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SorobanRpcRequest;
import org.stellar.sdk.responses.sorobanrpc.GetEventsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetFeeStatsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetHealthResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLatestLedgerResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLedgersResponse;
import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetVersionInfoResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SorobanRpcResponse;
import org.stellar.sdk.responses.sorobanrpc.*;
import org.stellar.sdk.scval.Scv;
import org.stellar.sdk.xdr.ContractDataDurability;
import org.stellar.sdk.xdr.LedgerEntry;
import org.stellar.sdk.xdr.LedgerEntryType;
Expand Down Expand Up @@ -560,6 +544,52 @@ public SendTransactionResponse sendTransaction(Transaction transaction) {
"sendTransaction", params, new TypeToken<SorobanRpcResponse<SendTransactionResponse>>() {});
}

public GetSacBalanceResponse getSacBalance(String contractId, Asset asset, Network network)
throws IOException {

if (!StrKey.isValidContract(contractId)) {
throw new IllegalArgumentException("expected contract ID, got " + contractId);
}

LedgerKey ledgerKey =
LedgerKey.builder()
.discriminant(LedgerEntryType.CONTRACT_DATA)
.contractData(
LedgerKey.LedgerKeyContractData.builder()
.contract(Scv.toAddress(asset.getContractId(network)).getAddress())
.key(
Scv.toVec(
Arrays.asList(Scv.toSymbol("Balance"), Scv.toAddress(contractId))))
.durability(ContractDataDurability.PERSISTENT)
.build())
.build();

GetLedgerEntriesResponse response = this.getLedgerEntries(Collections.singleton(ledgerKey));

List<GetLedgerEntriesResponse.LedgerEntryResult> entries = response.getEntries();
if (entries == null || entries.isEmpty()) {
return null;
}

GetLedgerEntriesResponse.LedgerEntryResult entry = entries.get(0);
LedgerEntry.LedgerEntryData ledgerEntryData =
LedgerEntry.LedgerEntryData.fromXdrBase64(entry.getXdr());
LinkedHashMap<SCVal, SCVal> balanceMap =
Scv.fromMap(ledgerEntryData.getContractData().getVal());

return GetSacBalanceResponse.builder()
.latestLedger(response.getLatestLedger())
.balanceEntry(
GetSacBalanceResponse.BalanceEntry.builder()
.liveUntilLedgerSeq(entry.getLiveUntilLedger())
.lastModifiedLedgerSeq(entry.getLastModifiedLedger())
.amount(Scv.fromInt128(balanceMap.get(Scv.toSymbol("amount"))).toString())
.authorized(Scv.fromBoolean(balanceMap.get(Scv.toSymbol("authorized"))))
.clawback(Scv.fromBoolean(balanceMap.get(Scv.toSymbol("clawback"))))
.build())
.build();
}

public static Transaction assembleTransaction(
Transaction transaction, SimulateTransactionResponse simulateTransactionResponse) {
if (!transaction.isSorobanTransaction()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.stellar.sdk.responses.sorobanrpc;

import lombok.Builder;
import lombok.Value;

@Builder
@Value
public class GetSacBalanceResponse {
long latestLedger;
BalanceEntry balanceEntry;

@Value
@Builder
public static class BalanceEntry {
Long liveUntilLedgerSeq;
Long lastModifiedLedgerSeq;
String amount;
Boolean authorized;
Boolean clawback;
}
}
60 changes: 48 additions & 12 deletions src/test/java/org/stellar/sdk/SorobanServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,7 @@
import org.stellar.sdk.requests.sorobanrpc.SendTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SimulateTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SorobanRpcRequest;
import org.stellar.sdk.responses.sorobanrpc.GetEventsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetFeeStatsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetHealthResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLatestLedgerResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
import org.stellar.sdk.responses.sorobanrpc.GetLedgersResponse;
import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetVersionInfoResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.*;
import org.stellar.sdk.xdr.ContractDataDurability;
import org.stellar.sdk.xdr.ContractExecutable;
import org.stellar.sdk.xdr.ContractExecutableType;
Expand Down Expand Up @@ -1354,6 +1343,53 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest)
mockWebServer.close();
}

@Test
public void testGetSacBalance() throws IOException {
String filePath = "src/test/resources/soroban_server/get_sac_balance.json";
String json = new String(Files.readAllBytes(Paths.get(filePath)));
MockWebServer mockWebServer = new MockWebServer();
Dispatcher dispatcher =
new Dispatcher() {
@NotNull
@Override
public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) {
SorobanRpcRequest<GetLedgerEntriesRequest> sorobanRpcRequest =
gson.fromJson(
recordedRequest.getBody().readUtf8(),
new TypeToken<SorobanRpcRequest<GetLedgerEntriesRequest>>() {}.getType());
if ("POST".equals(recordedRequest.getMethod())
&& sorobanRpcRequest.getMethod().equals("getLedgerEntries")
&& "AAAABgAAAAHXkotywnA8z+r365/0701QSlWouXn8m0UOoshCtNHOYQAAABAAAAABAAAAAgAAAA8AAAAHQmFsYW5jZQAAAAASAAAAATS6wvBZn2PXUHdI2oDWZqeECiy17cpcL6qrr1lqpqu6AAAAAQ=="
.equals(
sorobanRpcRequest.getParams().getKeys().stream()
.findFirst()
.orElse(null))) {
return new MockResponse().setResponseCode(200).setBody(json);
}
return new MockResponse().setResponseCode(404);
}
};
mockWebServer.setDispatcher(dispatcher);
mockWebServer.start();

HttpUrl baseUrl = mockWebServer.url("");
SorobanServer server = new SorobanServer(baseUrl.toString());
GetSacBalanceResponse balance =
server.getSacBalance(
"CA2LVQXQLGPWHV2QO5ENVAGWM2TYICRMWXW4UXBPVKV26WLKU2V3UTH5",
Asset.createNativeAsset(),
Network.TESTNET);
assertEquals(1104160, balance.getLatestLedger());
assertEquals((Long) 3163876L, balance.getBalanceEntry().getLiveUntilLedgerSeq());
assertEquals((Long) 1090887L, balance.getBalanceEntry().getLastModifiedLedgerSeq());
assertEquals("28", balance.getBalanceEntry().getAmount());
assertTrue(balance.getBalanceEntry().getAuthorized());
assertFalse(balance.getBalanceEntry().getClawback());

server.close();
mockWebServer.close();
}

@Test
public void testSorobanRpcErrorResponseThrows() throws IOException {
String filePath = "src/test/resources/soroban_server/soroban_rpc_error.json";
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/soroban_server/get_sac_balance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"jsonrpc": "2.0",
"id": "5715b491-e70c-483b-b000-315735c4129b",
"result": {
"entries": [
{
"key": "AAAABgAAAAHXkotywnA8z+r365/0701QSlWouXn8m0UOoshCtNHOYQAAABAAAAABAAAAAgAAAA8AAAAHQmFsYW5jZQAAAAASAAAAATS6wvBZn2PXUHdI2oDWZqeECiy17cpcL6qrr1lqpqu6AAAAAQ==",
"xdr": "AAAABgAAAAAAAAAB15KLcsJwPM/q9+uf9O9NUEpVqLl5/JtFDqLIQrTRzmEAAAAQAAAAAQAAAAIAAAAPAAAAB0JhbGFuY2UAAAAAEgAAAAE0usLwWZ9j11B3SNqA1manhAoste3KXC+qq69ZaqarugAAAAEAAAARAAAAAQAAAAMAAAAPAAAABmFtb3VudAAAAAAACgAAAAAAAAAAAAAAAAAAABwAAAAPAAAACmF1dGhvcml6ZWQAAAAAAAAAAAABAAAADwAAAAhjbGF3YmFjawAAAAAAAAAA",
"lastModifiedLedgerSeq": 1090887,
"liveUntilLedgerSeq": 3163876
}
],
"latestLedger": 1104160
}
}