Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetLedgerEntries and refactor calling SorobanServer with Closeable interface #671

Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions examples/src/main/java/network/lightsail/GetLedgerEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package network.lightsail;

import static org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse.*;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.SorobanServer;
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
import org.stellar.sdk.xdr.LedgerEntry;
import org.stellar.sdk.xdr.LedgerEntryType;
import org.stellar.sdk.xdr.LedgerKey;
import org.stellar.sdk.xdr.LedgerKey.LedgerKeyAccount;

public class GetLedgerEntries {
public static void main(String[] args) throws IOException {
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
GetLedgerEntriesResponse response;
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
KeyPair keyPair =
KeyPair.fromAccountId("GDJLBYYKMCXNVVNABOE66NYXQGIA5AC5D223Z2KF6ZEYK4UBCA7FKLTG");

// Create ledger keys
List<LedgerKey> ledgerKeys =
Collections.singletonList(
LedgerKey.builder()
.account(LedgerKeyAccount.builder().accountID(keyPair.getXdrAccountId()).build())
.discriminant(LedgerEntryType.ACCOUNT)
.build());

// Get ledger entries
response = sorobanServer.getLedgerEntries(ledgerKeys);
}

// Print ledger entries
for (LedgerEntryResult result : response.getEntries()) {
LedgerEntry.LedgerEntryData ledgerEntryData =
LedgerEntry.LedgerEntryData.fromXdrBase64(result.getXdr());
System.out.println(ledgerEntryData);
}
}
}
146 changes: 74 additions & 72 deletions examples/src/main/java/network/lightsail/SorobanCreateContract.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package network.lightsail;

import java.io.IOException;
import java.util.List;
import org.stellar.sdk.Address;
import org.stellar.sdk.KeyPair;
Expand All @@ -19,94 +20,95 @@
import org.stellar.sdk.xdr.TransactionMeta;

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

KeyPair keyPair = KeyPair.fromSecretSeed(secret);
SorobanServer sorobanServer = new SorobanServer(rpcServerUrl);
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());

String wasmId = "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593";
List<SCVal> constructorArgs =
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
InvokeHostFunctionOperation invokeHostFunctionOperation =
InvokeHostFunctionOperation.createContractOperationBuilder(
wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
.build();
GetTransactionResponse getTransactionResponse;
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());

// Build the transaction
Transaction unpreparedTransaction =
new TransactionBuilder(source, network)
.setBaseFee(Transaction.MIN_BASE_FEE)
.addOperation(invokeHostFunctionOperation)
.setTimeout(300)
.build();
String wasmId = "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593";
List<SCVal> constructorArgs =
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
InvokeHostFunctionOperation invokeHostFunctionOperation =
InvokeHostFunctionOperation.createContractOperationBuilder(
wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
.build();

// Prepare the transaction
Transaction transaction;
try {
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
} catch (PrepareTransactionException e) {
throw new RuntimeException("Prepare transaction failed", e);
} catch (NetworkException e) {
throw new RuntimeException("Network error", e);
}
// Build the transaction
Transaction unpreparedTransaction =
new TransactionBuilder(source, network)
.setBaseFee(Transaction.MIN_BASE_FEE)
.addOperation(invokeHostFunctionOperation)
.setTimeout(300)
.build();

// Sign the transaction
transaction.sign(keyPair);
// Prepare the transaction
Transaction transaction;
try {
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
} catch (PrepareTransactionException e) {
throw new RuntimeException("Prepare transaction failed", e);
} catch (NetworkException e) {
throw new RuntimeException("Network error", e);
}

// Send the transaction
SendTransactionResponse sendTransactionResponse;
try {
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
} catch (NetworkException e) {
throw new RuntimeException("Send transaction failed", e);
}
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
sendTransactionResponse.getStatus())) {
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
}
// Sign the transaction
transaction.sign(keyPair);

// Check the transaction status
GetTransactionResponse getTransactionResponse;
while (true) {
// Send the transaction
SendTransactionResponse sendTransactionResponse;
try {
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
} catch (NetworkException e) {
throw new RuntimeException("Get transaction failed", e);
throw new RuntimeException("Send transaction failed", e);
}

if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
getTransactionResponse.getStatus())) {
break;
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
sendTransactionResponse.getStatus())) {
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
}
// Wait for 3 seconds before checking the transaction status again
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();

// Check the transaction status
while (true) {
try {
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
} catch (NetworkException e) {
throw new RuntimeException("Get transaction failed", e);
}

if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
getTransactionResponse.getStatus())) {
break;
}
// Wait for 3 seconds before checking the transaction status again
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
getTransactionResponse.getStatus())) {
System.out.println("Transaction succeeded: " + getTransactionResponse);
// parse the function return value
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
byte[] hash =
transactionMeta
.getV3()
.getSorobanMeta()
.getReturnValue()
.getAddress()
.getContractId()
.getHash();
String contractId = StrKey.encodeContract(hash);
System.out.println("Contract ID: " + contractId);
} else {
System.out.println("Transaction failed: " + getTransactionResponse);
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
getTransactionResponse.getStatus())) {
System.out.println("Transaction succeeded: " + getTransactionResponse);
// parse the function return value
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
byte[] hash =
transactionMeta
.getV3()
.getSorobanMeta()
.getReturnValue()
.getAddress()
.getContractId()
.getHash();
String contractId = StrKey.encodeContract(hash);
System.out.println("Contract ID: " + contractId);
} else {
System.out.println("Transaction failed: " + getTransactionResponse);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package network.lightsail;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.stellar.sdk.KeyPair;
Expand All @@ -18,94 +19,95 @@
import org.stellar.sdk.xdr.TransactionMeta;

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

KeyPair keyPair = KeyPair.fromSecretSeed(secret);
SorobanServer sorobanServer = new SorobanServer(rpcServerUrl);
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());

// The invoke host function operation
// https://github.com/stellar/soroban-examples/tree/main/hello_world
String contractId = "CDZJVZWCY4NFGHCCZMX6QW5AK3ET5L3UUAYBVNDYOXDLQXW7PHXGYOBJ";
String functionName = "hello";
List<SCVal> parameters =
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
InvokeHostFunctionOperation invokeHostFunctionOperation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId, functionName, parameters)
.build();
// The invoke host function operation
// https://github.com/stellar/soroban-examples/tree/main/hello_world
String contractId = "CDZJVZWCY4NFGHCCZMX6QW5AK3ET5L3UUAYBVNDYOXDLQXW7PHXGYOBJ";
String functionName = "hello";
List<SCVal> parameters =
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
InvokeHostFunctionOperation invokeHostFunctionOperation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId, functionName, parameters)
.build();

// Build the transaction
Transaction unpreparedTransaction =
new TransactionBuilder(source, network)
.setBaseFee(Transaction.MIN_BASE_FEE)
.addOperation(invokeHostFunctionOperation)
.setTimeout(300)
.build();
// Build the transaction
Transaction unpreparedTransaction =
new TransactionBuilder(source, network)
.setBaseFee(Transaction.MIN_BASE_FEE)
.addOperation(invokeHostFunctionOperation)
.setTimeout(300)
.build();

// Prepare the transaction
Transaction transaction;
try {
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
} catch (PrepareTransactionException e) {
throw new RuntimeException("Prepare transaction failed", e);
} catch (NetworkException e) {
throw new RuntimeException("Network error", e);
}

// Sign the transaction
transaction.sign(keyPair);
// Prepare the transaction
Transaction transaction;
try {
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
} catch (PrepareTransactionException e) {
throw new RuntimeException("Prepare transaction failed", e);
} catch (NetworkException e) {
throw new RuntimeException("Network error", e);
}

// Send the transaction
SendTransactionResponse sendTransactionResponse;
try {
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
} catch (NetworkException e) {
throw new RuntimeException("Send transaction failed", e);
}
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
sendTransactionResponse.getStatus())) {
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
}
// Sign the transaction
transaction.sign(keyPair);

// Check the transaction status
GetTransactionResponse getTransactionResponse;
while (true) {
// Send the transaction
SendTransactionResponse sendTransactionResponse;
try {
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
} catch (NetworkException e) {
throw new RuntimeException("Get transaction failed", e);
throw new RuntimeException("Send transaction failed", e);
}

if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
getTransactionResponse.getStatus())) {
break;
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
sendTransactionResponse.getStatus())) {
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
}
// Wait for 3 seconds before checking the transaction status again
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();

// Check the transaction status
GetTransactionResponse getTransactionResponse;
while (true) {
try {
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
} catch (NetworkException e) {
throw new RuntimeException("Get transaction failed", e);
}

if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
getTransactionResponse.getStatus())) {
break;
}
// Wait for 3 seconds before checking the transaction status again
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
getTransactionResponse.getStatus())) {
System.out.println("Transaction succeeded: " + getTransactionResponse);
// parse the function return value
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
SCVal rawReturnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
System.out.println("Raw return value: " + rawReturnValue);
List<SCVal> vec = Scv.fromVec(rawReturnValue).stream().toList();
String returnValue0 = new String(Scv.fromString(vec.get(0)), StandardCharsets.UTF_8);
String returnValue1 = new String(Scv.fromString(vec.get(1)), StandardCharsets.UTF_8);
System.out.println("Return value 0: " + returnValue0);
System.out.println("Return value 1: " + returnValue1);
} else {
System.out.println("Transaction failed: " + getTransactionResponse);
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
getTransactionResponse.getStatus())) {
System.out.println("Transaction succeeded: " + getTransactionResponse);
// parse the function return value
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
SCVal rawReturnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
System.out.println("Raw return value: " + rawReturnValue);
List<SCVal> vec = Scv.fromVec(rawReturnValue).stream().toList();
String returnValue0 = new String(Scv.fromString(vec.get(0)), StandardCharsets.UTF_8);
String returnValue1 = new String(Scv.fromString(vec.get(1)), StandardCharsets.UTF_8);
System.out.println("Return value 0: " + returnValue0);
System.out.println("Return value 1: " + returnValue1);
} else {
System.out.println("Transaction failed: " + getTransactionResponse);
}
}
}
}
Loading