Skip to content

Commit 7cf68e9

Browse files
committed
Merge remote-tracking branch 'sdf/master' into examples/check-stellar-asset-contract
2 parents fdb2789 + 42733cc commit 7cf68e9

File tree

3 files changed

+201
-57
lines changed

3 files changed

+201
-57
lines changed

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

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
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+
import org.stellar.sdk.*;
136
import org.stellar.sdk.exception.NetworkException;
147
import org.stellar.sdk.exception.PrepareTransactionException;
158
import org.stellar.sdk.operations.InvokeHostFunctionOperation;
@@ -19,65 +12,29 @@
1912
import org.stellar.sdk.xdr.SCVal;
2013
import org.stellar.sdk.xdr.TransactionMeta;
2114

22-
/**
23-
* This example shows how to deploy a contract with an installed (uploaded) wasm id.
24-
*/
15+
/** This example shows how to deploy a contract with an installed (uploaded) wasm id. */
2516
public class SorobanCreateContract {
2617
public static void main(String[] args) throws IOException {
2718
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
2819
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
29-
Network network = Network.TESTNET;
20+
KeyPair sourceAccount = KeyPair.fromSecretSeed(secret);
3021

31-
KeyPair keyPair = KeyPair.fromSecretSeed(secret);
32-
GetTransactionResponse getTransactionResponse;
3322
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
34-
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());
35-
36-
String wasmId = "406edc375a4334ea2849d22e490919a5456ee176dd2f9fc3e1e557cd242ec593";
37-
List<SCVal> constructorArgs =
38-
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
39-
InvokeHostFunctionOperation invokeHostFunctionOperation =
40-
InvokeHostFunctionOperation.createContractOperationBuilder(
41-
wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
42-
.build();
43-
44-
// Build the transaction
45-
Transaction unpreparedTransaction =
46-
new TransactionBuilder(source, network)
47-
.setBaseFee(Transaction.MIN_BASE_FEE)
48-
.addOperation(invokeHostFunctionOperation)
49-
.setTimeout(300)
50-
.build();
51-
52-
// Prepare the transaction
53-
Transaction transaction;
54-
try {
55-
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
56-
} catch (PrepareTransactionException e) {
57-
throw new RuntimeException("Prepare transaction failed", e);
58-
} catch (NetworkException e) {
59-
throw new RuntimeException("Network error", e);
60-
}
61-
62-
// Sign the transaction
63-
transaction.sign(keyPair);
64-
65-
// Send the transaction
66-
SendTransactionResponse sendTransactionResponse;
67-
try {
68-
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
69-
} catch (NetworkException e) {
70-
throw new RuntimeException("Send transaction failed", e);
71-
}
72-
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
73-
sendTransactionResponse.getStatus())) {
74-
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
75-
}
23+
List<SCVal> constructorArgs = List.of(Scv.toString("World!"));
24+
String txHash =
25+
createContractWithWasmId(
26+
sorobanServer,
27+
Network.TESTNET,
28+
"fec2819684a9c2964614b769ef881c66848af7ef0eabf1ddb968fc0fef36b11e", // Please check
29+
// `SorobanUploadWasm.java` to see how to upload the WASM file and get the WASM ID
30+
sourceAccount,
31+
null);
7632

33+
GetTransactionResponse getTransactionResponse;
7734
// Check the transaction status
7835
while (true) {
7936
try {
80-
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
37+
getTransactionResponse = sorobanServer.getTransaction(txHash);
8138
} catch (NetworkException e) {
8239
throw new RuntimeException("Get transaction failed", e);
8340
}
@@ -114,4 +71,65 @@ wasmId, new Address(keyPair.getAccountId()), constructorArgs, null)
11471
}
11572
}
11673
}
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+
}
117135
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package network.lightsail;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import org.stellar.sdk.*;
7+
import org.stellar.sdk.KeyPair;
8+
import org.stellar.sdk.Network;
9+
import org.stellar.sdk.SorobanServer;
10+
import org.stellar.sdk.Transaction;
11+
import org.stellar.sdk.TransactionBuilder;
12+
import org.stellar.sdk.TransactionBuilderAccount;
13+
import org.stellar.sdk.exception.NetworkException;
14+
import org.stellar.sdk.exception.PrepareTransactionException;
15+
import org.stellar.sdk.operations.InvokeHostFunctionOperation;
16+
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
17+
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
18+
import org.stellar.sdk.xdr.TransactionMeta;
19+
20+
/** This example shows how to upload a WASM file to the Soroban network and get the WASM ID. */
21+
public class SorobanUploadWasm {
22+
public static void main(String[] args) throws IOException {
23+
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
24+
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
25+
KeyPair sourceAccount = KeyPair.fromSecretSeed(secret);
26+
27+
// Load the WASM file
28+
String wasmFilePath = "src/main/resources/wasm/soroban_hello_world_contract.wasm";
29+
byte[] wasmBytes = Files.readAllBytes(Paths.get(wasmFilePath));
30+
31+
try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
32+
TransactionBuilderAccount source = sorobanServer.getAccount(sourceAccount.getAccountId());
33+
34+
String txHash = uploadWasm(sorobanServer, Network.TESTNET, wasmBytes, sourceAccount);
35+
36+
GetTransactionResponse getTransactionResponse;
37+
38+
// Check the transaction status
39+
while (true) {
40+
try {
41+
getTransactionResponse = sorobanServer.getTransaction(txHash);
42+
} catch (NetworkException e) {
43+
throw new RuntimeException("Get transaction failed", e);
44+
}
45+
46+
if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
47+
getTransactionResponse.getStatus())) {
48+
break;
49+
}
50+
// Wait for 3 seconds before checking the transaction status again
51+
try {
52+
Thread.sleep(3000);
53+
} catch (InterruptedException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
58+
if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
59+
getTransactionResponse.getStatus())) {
60+
System.out.println("Transaction succeeded: " + getTransactionResponse);
61+
// parse the function return value
62+
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
63+
byte[] wasmId =
64+
transactionMeta.getV3().getSorobanMeta().getReturnValue().getBytes().getSCBytes();
65+
System.out.println("Wasm ID: " + Util.bytesToHex(wasmId).toLowerCase());
66+
} else {
67+
System.out.println("Transaction failed: " + getTransactionResponse);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Creates a contract with the given WASM bytes and constructor arguments.
74+
*
75+
* @param sorobanServer the Soroban server
76+
* @param network the network (e.g., TESTNET)
77+
* @param wasmBytes the WASM bytes
78+
* @param sourceAccount the source account
79+
* @return the transaction hash
80+
*/
81+
public static String uploadWasm(
82+
SorobanServer sorobanServer, Network network, byte[] wasmBytes, KeyPair sourceAccount) {
83+
84+
GetTransactionResponse getTransactionResponse;
85+
TransactionBuilderAccount source = sorobanServer.getAccount(sourceAccount.getAccountId());
86+
87+
InvokeHostFunctionOperation invokeHostFunctionOperation =
88+
InvokeHostFunctionOperation.uploadContractWasmOperationBuilder(wasmBytes).build();
89+
90+
// Build the transaction
91+
Transaction unpreparedTransaction =
92+
new TransactionBuilder(source, network)
93+
.setBaseFee(Transaction.MIN_BASE_FEE)
94+
.addOperation(invokeHostFunctionOperation)
95+
.setTimeout(300)
96+
.build();
97+
98+
// Prepare the transaction
99+
Transaction transaction;
100+
try {
101+
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
102+
} catch (PrepareTransactionException e) {
103+
throw new RuntimeException("Prepare transaction failed", e);
104+
} catch (NetworkException e) {
105+
throw new RuntimeException("Network error", e);
106+
}
107+
108+
// Sign the transaction
109+
transaction.sign(sourceAccount);
110+
111+
// Send the transaction
112+
SendTransactionResponse sendTransactionResponse;
113+
try {
114+
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
115+
} catch (NetworkException e) {
116+
throw new RuntimeException("Send transaction failed", e);
117+
}
118+
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
119+
sendTransactionResponse.getStatus())) {
120+
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
121+
}
122+
123+
// Check the transaction status
124+
return sendTransactionResponse.getHash();
125+
}
126+
}
575 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)