Skip to content

Commit 434cc3c

Browse files
authored
Refactor the SorobanCreateContract example (#690)
* Refactor SorobanCreateContract example * Refactor out the createContractWithWasmId
1 parent 0190956 commit 434cc3c

File tree

1 file changed

+75
-54
lines changed

1 file changed

+75
-54
lines changed

examples/src/main/java/network/lightsail/SorobanCreateContract.java

Lines changed: 75 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
import java.io.IOException;
44
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.*;
137
import org.stellar.sdk.exception.NetworkException;
148
import org.stellar.sdk.exception.PrepareTransactionException;
159
import org.stellar.sdk.operations.InvokeHostFunctionOperation;
@@ -19,62 +13,28 @@
1913
import org.stellar.sdk.xdr.SCVal;
2014
import org.stellar.sdk.xdr.TransactionMeta;
2115

16+
/** This example shows how to deploy a contract with an installed (uploaded) wasm id. */
2217
public class SorobanCreateContract {
2318
public static void main(String[] args) throws IOException {
2419
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
2520
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
26-
Network network = Network.TESTNET;
21+
KeyPair sourceAccount = KeyPair.fromSecretSeed(secret);
2722

28-
KeyPair keyPair = KeyPair.fromSecretSeed(secret);
29-
GetTransactionResponse getTransactionResponse;
3023
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
7332

33+
GetTransactionResponse getTransactionResponse;
7434
// Check the transaction status
7535
while (true) {
7636
try {
77-
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
37+
getTransactionResponse = sorobanServer.getTransaction(txHash);
7838
} catch (NetworkException e) {
7939
throw new RuntimeException("Get transaction failed", e);
8040
}
@@ -111,4 +71,65 @@ wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
11171
}
11272
}
11373
}
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+
}
114135
}

0 commit comments

Comments
 (0)