|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.util.List; |
5 | | -import org.stellar.sdk.Address; |
6 | | -import org.stellar.sdk.KeyPair; |
7 | | -import org.stellar.sdk.Network; |
8 | | -import org.stellar.sdk.SorobanServer; |
9 | | -import org.stellar.sdk.StrKey; |
10 | | -import org.stellar.sdk.Transaction; |
11 | | -import org.stellar.sdk.TransactionBuilder; |
12 | | -import org.stellar.sdk.TransactionBuilderAccount; |
| 5 | + |
| 6 | +import org.stellar.sdk.*; |
13 | 7 | import org.stellar.sdk.exception.NetworkException; |
14 | 8 | import org.stellar.sdk.exception.PrepareTransactionException; |
15 | 9 | import org.stellar.sdk.operations.InvokeHostFunctionOperation; |
|
19 | 13 | import org.stellar.sdk.xdr.SCVal; |
20 | 14 | import org.stellar.sdk.xdr.TransactionMeta; |
21 | 15 |
|
| 16 | +/** This example shows how to deploy a contract with an installed (uploaded) wasm id. */ |
22 | 17 | public class SorobanCreateContract { |
23 | 18 | public static void main(String[] args) throws IOException { |
24 | 19 | String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV"; |
25 | 20 | String rpcServerUrl = "https://soroban-testnet.stellar.org:443"; |
26 | | - Network network = Network.TESTNET; |
| 21 | + KeyPair sourceAccount = KeyPair.fromSecretSeed(secret); |
27 | 22 |
|
28 | | - KeyPair keyPair = KeyPair.fromSecretSeed(secret); |
29 | | - GetTransactionResponse getTransactionResponse; |
30 | 23 | try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) { |
31 | | - TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId()); |
32 | | - |
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(); |
40 | | - |
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(); |
48 | | - |
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); |
61 | | - |
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 | | - } |
| 24 | + List<SCVal> constructorArgs = List.of(Scv.toString("World!")); |
| 25 | + String txHash = |
| 26 | + createContractWithWasmId( |
| 27 | + sorobanServer, |
| 28 | + Network.TESTNET, |
| 29 | + "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593", |
| 30 | + sourceAccount, |
| 31 | + constructorArgs); // Scv is a helper class to create SCVal objects |
73 | 32 |
|
| 33 | + GetTransactionResponse getTransactionResponse; |
74 | 34 | // Check the transaction status |
75 | 35 | while (true) { |
76 | 36 | try { |
77 | | - getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash()); |
| 37 | + getTransactionResponse = sorobanServer.getTransaction(txHash); |
78 | 38 | } catch (NetworkException e) { |
79 | 39 | throw new RuntimeException("Get transaction failed", e); |
80 | 40 | } |
@@ -111,4 +71,65 @@ wasmId, new Address(keyPair.getAccountId()), constructorArgs, null) |
111 | 71 | } |
112 | 72 | } |
113 | 73 | } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a contract with the given WASM ID and constructor arguments. |
| 77 | + * |
| 78 | + * @param sorobanServer the Soroban server |
| 79 | + * @param network the network (e.g., TESTNET) |
| 80 | + * @param wasmId the WASM ID of the contract |
| 81 | + * @param sourceAccount the source account |
| 82 | + * @param constructorArgs the constructor arguments |
| 83 | + * @return the transaction hash |
| 84 | + */ |
| 85 | + private static String createContractWithWasmId( |
| 86 | + SorobanServer sorobanServer, |
| 87 | + Network network, |
| 88 | + String wasmId, |
| 89 | + KeyPair sourceAccount, |
| 90 | + List<SCVal> constructorArgs) { |
| 91 | + |
| 92 | + GetTransactionResponse getTransactionResponse; |
| 93 | + TransactionBuilderAccount source = sorobanServer.getAccount(sourceAccount.getAccountId()); |
| 94 | + |
| 95 | + InvokeHostFunctionOperation invokeHostFunctionOperation = |
| 96 | + InvokeHostFunctionOperation.createContractOperationBuilder( |
| 97 | + wasmId, new Address(sourceAccount.getAccountId()), constructorArgs, null) |
| 98 | + .build(); |
| 99 | + |
| 100 | + // Build the transaction |
| 101 | + Transaction unpreparedTransaction = |
| 102 | + new TransactionBuilder(source, network) |
| 103 | + .setBaseFee(Transaction.MIN_BASE_FEE) |
| 104 | + .addOperation(invokeHostFunctionOperation) |
| 105 | + .setTimeout(300) |
| 106 | + .build(); |
| 107 | + |
| 108 | + // Prepare the transaction |
| 109 | + Transaction transaction; |
| 110 | + try { |
| 111 | + transaction = sorobanServer.prepareTransaction(unpreparedTransaction); |
| 112 | + } catch (PrepareTransactionException e) { |
| 113 | + throw new RuntimeException("Prepare transaction failed", e); |
| 114 | + } catch (NetworkException e) { |
| 115 | + throw new RuntimeException("Network error", e); |
| 116 | + } |
| 117 | + |
| 118 | + // Sign the transaction |
| 119 | + transaction.sign(sourceAccount); |
| 120 | + |
| 121 | + // Send the transaction |
| 122 | + SendTransactionResponse sendTransactionResponse; |
| 123 | + try { |
| 124 | + sendTransactionResponse = sorobanServer.sendTransaction(transaction); |
| 125 | + } catch (NetworkException e) { |
| 126 | + throw new RuntimeException("Send transaction failed", e); |
| 127 | + } |
| 128 | + if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals( |
| 129 | + sendTransactionResponse.getStatus())) { |
| 130 | + throw new RuntimeException("Send transaction failed: " + sendTransactionResponse); |
| 131 | + } |
| 132 | + |
| 133 | + return sendTransactionResponse.getHash(); |
| 134 | + } |
114 | 135 | } |
0 commit comments