diff --git a/examples/src/main/java/network/lightsail/GetLedgerEntries.java b/examples/src/main/java/network/lightsail/GetLedgerEntries.java new file mode 100644 index 000000000..05e650f78 --- /dev/null +++ b/examples/src/main/java/network/lightsail/GetLedgerEntries.java @@ -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 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); + } + } +} diff --git a/examples/src/main/java/network/lightsail/SorobanCreateContract.java b/examples/src/main/java/network/lightsail/SorobanCreateContract.java index b44341e9d..5eeca7c75 100644 --- a/examples/src/main/java/network/lightsail/SorobanCreateContract.java +++ b/examples/src/main/java/network/lightsail/SorobanCreateContract.java @@ -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; @@ -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 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 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); + } } } } diff --git a/examples/src/main/java/network/lightsail/SorobanInvokeContractFunction.java b/examples/src/main/java/network/lightsail/SorobanInvokeContractFunction.java index 788f5398e..04d4c4753 100644 --- a/examples/src/main/java/network/lightsail/SorobanInvokeContractFunction.java +++ b/examples/src/main/java/network/lightsail/SorobanInvokeContractFunction.java @@ -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; @@ -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 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 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 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 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); + } } } }