Skip to content

Commit 4d7b5e9

Browse files
authored
chore: add GetLedgerEntries and refactor calling SorobanServer with Closeable interface (#671)
1 parent f80b424 commit 4d7b5e9

File tree

3 files changed

+191
-144
lines changed

3 files changed

+191
-144
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package network.lightsail;
2+
3+
import static org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse.*;
4+
5+
import java.io.IOException;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import org.stellar.sdk.KeyPair;
9+
import org.stellar.sdk.SorobanServer;
10+
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
11+
import org.stellar.sdk.xdr.LedgerEntry;
12+
import org.stellar.sdk.xdr.LedgerEntryType;
13+
import org.stellar.sdk.xdr.LedgerKey;
14+
import org.stellar.sdk.xdr.LedgerKey.LedgerKeyAccount;
15+
16+
public class GetLedgerEntries {
17+
public static void main(String[] args) throws IOException {
18+
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
19+
GetLedgerEntriesResponse response;
20+
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
21+
KeyPair keyPair =
22+
KeyPair.fromAccountId("GDJLBYYKMCXNVVNABOE66NYXQGIA5AC5D223Z2KF6ZEYK4UBCA7FKLTG");
23+
24+
// Create ledger keys
25+
List<LedgerKey> ledgerKeys =
26+
Collections.singletonList(
27+
LedgerKey.builder()
28+
.account(LedgerKeyAccount.builder().accountID(keyPair.getXdrAccountId()).build())
29+
.discriminant(LedgerEntryType.ACCOUNT)
30+
.build());
31+
32+
// Get ledger entries
33+
response = sorobanServer.getLedgerEntries(ledgerKeys);
34+
}
35+
36+
// Print ledger entries
37+
for (LedgerEntryResult result : response.getEntries()) {
38+
LedgerEntry.LedgerEntryData ledgerEntryData =
39+
LedgerEntry.LedgerEntryData.fromXdrBase64(result.getXdr());
40+
System.out.println(ledgerEntryData);
41+
}
42+
}
43+
}
Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package network.lightsail;
22

3+
import java.io.IOException;
34
import java.util.List;
45
import org.stellar.sdk.Address;
56
import org.stellar.sdk.KeyPair;
@@ -19,94 +20,95 @@
1920
import org.stellar.sdk.xdr.TransactionMeta;
2021

2122
public class SorobanCreateContract {
22-
public static void main(String[] args) {
23+
public static void main(String[] args) throws IOException {
2324
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
2425
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
2526
Network network = Network.TESTNET;
2627

2728
KeyPair keyPair = KeyPair.fromSecretSeed(secret);
28-
SorobanServer sorobanServer = new SorobanServer(rpcServerUrl);
29-
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
30-
31-
String wasmId = "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593";
32-
List<SCVal> constructorArgs =
33-
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
34-
InvokeHostFunctionOperation invokeHostFunctionOperation =
35-
InvokeHostFunctionOperation.createContractOperationBuilder(
36-
wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
37-
.build();
29+
GetTransactionResponse getTransactionResponse;
30+
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
31+
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
3832

39-
// Build the transaction
40-
Transaction unpreparedTransaction =
41-
new TransactionBuilder(source, network)
42-
.setBaseFee(Transaction.MIN_BASE_FEE)
43-
.addOperation(invokeHostFunctionOperation)
44-
.setTimeout(300)
45-
.build();
33+
String wasmId = "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593";
34+
List<SCVal> constructorArgs =
35+
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
36+
InvokeHostFunctionOperation invokeHostFunctionOperation =
37+
InvokeHostFunctionOperation.createContractOperationBuilder(
38+
wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
39+
.build();
4640

47-
// Prepare the transaction
48-
Transaction transaction;
49-
try {
50-
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
51-
} catch (PrepareTransactionException e) {
52-
throw new RuntimeException("Prepare transaction failed", e);
53-
} catch (NetworkException e) {
54-
throw new RuntimeException("Network error", e);
55-
}
41+
// Build the transaction
42+
Transaction unpreparedTransaction =
43+
new TransactionBuilder(source, network)
44+
.setBaseFee(Transaction.MIN_BASE_FEE)
45+
.addOperation(invokeHostFunctionOperation)
46+
.setTimeout(300)
47+
.build();
5648

57-
// Sign the transaction
58-
transaction.sign(keyPair);
49+
// Prepare the transaction
50+
Transaction transaction;
51+
try {
52+
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
53+
} catch (PrepareTransactionException e) {
54+
throw new RuntimeException("Prepare transaction failed", e);
55+
} catch (NetworkException e) {
56+
throw new RuntimeException("Network error", e);
57+
}
5958

60-
// Send the transaction
61-
SendTransactionResponse sendTransactionResponse;
62-
try {
63-
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
64-
} catch (NetworkException e) {
65-
throw new RuntimeException("Send transaction failed", e);
66-
}
67-
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
68-
sendTransactionResponse.getStatus())) {
69-
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
70-
}
59+
// Sign the transaction
60+
transaction.sign(keyPair);
7161

72-
// Check the transaction status
73-
GetTransactionResponse getTransactionResponse;
74-
while (true) {
62+
// Send the transaction
63+
SendTransactionResponse sendTransactionResponse;
7564
try {
76-
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
65+
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
7766
} catch (NetworkException e) {
78-
throw new RuntimeException("Get transaction failed", e);
67+
throw new RuntimeException("Send transaction failed", e);
7968
}
80-
81-
if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
82-
getTransactionResponse.getStatus())) {
83-
break;
69+
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
70+
sendTransactionResponse.getStatus())) {
71+
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
8472
}
85-
// Wait for 3 seconds before checking the transaction status again
86-
try {
87-
Thread.sleep(3000);
88-
} catch (InterruptedException e) {
89-
e.printStackTrace();
73+
74+
// Check the transaction status
75+
while (true) {
76+
try {
77+
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
78+
} catch (NetworkException e) {
79+
throw new RuntimeException("Get transaction failed", e);
80+
}
81+
82+
if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
83+
getTransactionResponse.getStatus())) {
84+
break;
85+
}
86+
// Wait for 3 seconds before checking the transaction status again
87+
try {
88+
Thread.sleep(3000);
89+
} catch (InterruptedException e) {
90+
e.printStackTrace();
91+
}
9092
}
91-
}
9293

93-
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
94-
getTransactionResponse.getStatus())) {
95-
System.out.println("Transaction succeeded: " + getTransactionResponse);
96-
// parse the function return value
97-
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
98-
byte[] hash =
99-
transactionMeta
100-
.getV3()
101-
.getSorobanMeta()
102-
.getReturnValue()
103-
.getAddress()
104-
.getContractId()
105-
.getHash();
106-
String contractId = StrKey.encodeContract(hash);
107-
System.out.println("Contract ID: " + contractId);
108-
} else {
109-
System.out.println("Transaction failed: " + getTransactionResponse);
94+
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
95+
getTransactionResponse.getStatus())) {
96+
System.out.println("Transaction succeeded: " + getTransactionResponse);
97+
// parse the function return value
98+
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
99+
byte[] hash =
100+
transactionMeta
101+
.getV3()
102+
.getSorobanMeta()
103+
.getReturnValue()
104+
.getAddress()
105+
.getContractId()
106+
.getHash();
107+
String contractId = StrKey.encodeContract(hash);
108+
System.out.println("Contract ID: " + contractId);
109+
} else {
110+
System.out.println("Transaction failed: " + getTransactionResponse);
111+
}
110112
}
111113
}
112114
}
Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package network.lightsail;
22

3+
import java.io.IOException;
34
import java.nio.charset.StandardCharsets;
45
import java.util.List;
56
import org.stellar.sdk.KeyPair;
@@ -18,94 +19,95 @@
1819
import org.stellar.sdk.xdr.TransactionMeta;
1920

2021
public class SorobanInvokeContractFunction {
21-
public static void main(String[] args) {
22+
public static void main(String[] args) throws IOException {
2223
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
2324
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
2425
Network network = Network.TESTNET;
2526

2627
KeyPair keyPair = KeyPair.fromSecretSeed(secret);
27-
SorobanServer sorobanServer = new SorobanServer(rpcServerUrl);
28-
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
28+
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
29+
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
2930

30-
// The invoke host function operation
31-
// https://github.com/stellar/soroban-examples/tree/main/hello_world
32-
String contractId = "CDZJVZWCY4NFGHCCZMX6QW5AK3ET5L3UUAYBVNDYOXDLQXW7PHXGYOBJ";
33-
String functionName = "hello";
34-
List<SCVal> parameters =
35-
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
36-
InvokeHostFunctionOperation invokeHostFunctionOperation =
37-
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
38-
contractId, functionName, parameters)
39-
.build();
31+
// The invoke host function operation
32+
// https://github.com/stellar/soroban-examples/tree/main/hello_world
33+
String contractId = "CDZJVZWCY4NFGHCCZMX6QW5AK3ET5L3UUAYBVNDYOXDLQXW7PHXGYOBJ";
34+
String functionName = "hello";
35+
List<SCVal> parameters =
36+
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
37+
InvokeHostFunctionOperation invokeHostFunctionOperation =
38+
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
39+
contractId, functionName, parameters)
40+
.build();
4041

41-
// Build the transaction
42-
Transaction unpreparedTransaction =
43-
new TransactionBuilder(source, network)
44-
.setBaseFee(Transaction.MIN_BASE_FEE)
45-
.addOperation(invokeHostFunctionOperation)
46-
.setTimeout(300)
47-
.build();
42+
// Build the transaction
43+
Transaction unpreparedTransaction =
44+
new TransactionBuilder(source, network)
45+
.setBaseFee(Transaction.MIN_BASE_FEE)
46+
.addOperation(invokeHostFunctionOperation)
47+
.setTimeout(300)
48+
.build();
4849

49-
// Prepare the transaction
50-
Transaction transaction;
51-
try {
52-
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
53-
} catch (PrepareTransactionException e) {
54-
throw new RuntimeException("Prepare transaction failed", e);
55-
} catch (NetworkException e) {
56-
throw new RuntimeException("Network error", e);
57-
}
58-
59-
// Sign the transaction
60-
transaction.sign(keyPair);
50+
// Prepare the transaction
51+
Transaction transaction;
52+
try {
53+
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
54+
} catch (PrepareTransactionException e) {
55+
throw new RuntimeException("Prepare transaction failed", e);
56+
} catch (NetworkException e) {
57+
throw new RuntimeException("Network error", e);
58+
}
6159

62-
// Send the transaction
63-
SendTransactionResponse sendTransactionResponse;
64-
try {
65-
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
66-
} catch (NetworkException e) {
67-
throw new RuntimeException("Send transaction failed", e);
68-
}
69-
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
70-
sendTransactionResponse.getStatus())) {
71-
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
72-
}
60+
// Sign the transaction
61+
transaction.sign(keyPair);
7362

74-
// Check the transaction status
75-
GetTransactionResponse getTransactionResponse;
76-
while (true) {
63+
// Send the transaction
64+
SendTransactionResponse sendTransactionResponse;
7765
try {
78-
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
66+
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
7967
} catch (NetworkException e) {
80-
throw new RuntimeException("Get transaction failed", e);
68+
throw new RuntimeException("Send transaction failed", e);
8169
}
82-
83-
if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
84-
getTransactionResponse.getStatus())) {
85-
break;
70+
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
71+
sendTransactionResponse.getStatus())) {
72+
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
8673
}
87-
// Wait for 3 seconds before checking the transaction status again
88-
try {
89-
Thread.sleep(3000);
90-
} catch (InterruptedException e) {
91-
e.printStackTrace();
74+
75+
// Check the transaction status
76+
GetTransactionResponse getTransactionResponse;
77+
while (true) {
78+
try {
79+
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
80+
} catch (NetworkException e) {
81+
throw new RuntimeException("Get transaction failed", e);
82+
}
83+
84+
if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
85+
getTransactionResponse.getStatus())) {
86+
break;
87+
}
88+
// Wait for 3 seconds before checking the transaction status again
89+
try {
90+
Thread.sleep(3000);
91+
} catch (InterruptedException e) {
92+
e.printStackTrace();
93+
}
9294
}
93-
}
9495

95-
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
96-
getTransactionResponse.getStatus())) {
97-
System.out.println("Transaction succeeded: " + getTransactionResponse);
98-
// parse the function return value
99-
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
100-
SCVal rawReturnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
101-
System.out.println("Raw return value: " + rawReturnValue);
102-
List<SCVal> vec = Scv.fromVec(rawReturnValue).stream().toList();
103-
String returnValue0 = new String(Scv.fromString(vec.get(0)), StandardCharsets.UTF_8);
104-
String returnValue1 = new String(Scv.fromString(vec.get(1)), StandardCharsets.UTF_8);
105-
System.out.println("Return value 0: " + returnValue0);
106-
System.out.println("Return value 1: " + returnValue1);
107-
} else {
108-
System.out.println("Transaction failed: " + getTransactionResponse);
96+
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
97+
getTransactionResponse.getStatus())) {
98+
System.out.println("Transaction succeeded: " + getTransactionResponse);
99+
// parse the function return value
100+
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
101+
SCVal rawReturnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
102+
System.out.println("Raw return value: " + rawReturnValue);
103+
List<SCVal> vec = Scv.fromVec(rawReturnValue).stream().toList();
104+
String returnValue0 = new String(Scv.fromString(vec.get(0)), StandardCharsets.UTF_8);
105+
String returnValue1 = new String(Scv.fromString(vec.get(1)), StandardCharsets.UTF_8);
106+
System.out.println("Return value 0: " + returnValue0);
107+
System.out.println("Return value 1: " + returnValue1);
108+
} else {
109+
System.out.println("Transaction failed: " + getTransactionResponse);
110+
}
109111
}
110112
}
111113
}

0 commit comments

Comments
 (0)