|
1 | 1 | package network.lightsail; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.nio.charset.StandardCharsets; |
4 | 5 | import java.util.List; |
5 | 6 | import org.stellar.sdk.KeyPair; |
|
18 | 19 | import org.stellar.sdk.xdr.TransactionMeta; |
19 | 20 |
|
20 | 21 | public class SorobanInvokeContractFunction { |
21 | | - public static void main(String[] args) { |
| 22 | + public static void main(String[] args) throws IOException { |
22 | 23 | String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV"; |
23 | 24 | String rpcServerUrl = "https://soroban-testnet.stellar.org:443"; |
24 | 25 | Network network = Network.TESTNET; |
25 | 26 |
|
26 | 27 | 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()); |
29 | 30 |
|
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(); |
40 | 41 |
|
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(); |
48 | 49 |
|
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 | + } |
61 | 59 |
|
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); |
73 | 62 |
|
74 | | - // Check the transaction status |
75 | | - GetTransactionResponse getTransactionResponse; |
76 | | - while (true) { |
| 63 | + // Send the transaction |
| 64 | + SendTransactionResponse sendTransactionResponse; |
77 | 65 | try { |
78 | | - getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash()); |
| 66 | + sendTransactionResponse = sorobanServer.sendTransaction(transaction); |
79 | 67 | } catch (NetworkException e) { |
80 | | - throw new RuntimeException("Get transaction failed", e); |
| 68 | + throw new RuntimeException("Send transaction failed", e); |
81 | 69 | } |
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); |
86 | 73 | } |
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 | + } |
92 | 94 | } |
93 | | - } |
94 | 95 |
|
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 | + } |
109 | 111 | } |
110 | 112 | } |
111 | 113 | } |
0 commit comments