Skip to content

Commit 0bac883

Browse files
authored
refactor!: remove createStellarAssetContractOperationBuilder(Address, byte[]), use createContractOperationBuilder instead (#764)
1 parent f855afd commit 0bac883

File tree

6 files changed

+48
-90
lines changed

6 files changed

+48
-90
lines changed

.github/workflows/test-deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ jobs:
2828
ENABLE_LOGS: true
2929
NETWORK: local
3030
ENABLE_SOROBAN_RPC: true
31-
PROTOCOL_VERSION: 24
31+
PROTOCOL_VERSION: 25
3232
options: >-
33-
--health-cmd "curl --no-progress-meter --fail-with-body -X POST \"http://localhost:8000/soroban/rpc\" -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"id\":8675309,\"method\":\"getNetwork\"}' && curl --no-progress-meter \"http://localhost:8000/friendbot\" | grep '\"invalid_field\": \"addr\"'"
33+
--health-cmd "curl --no-progress-meter --fail-with-body -X POST \"http://localhost:8000/rpc\" -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"id\":8675309,\"method\":\"getNetwork\"}' && curl --no-progress-meter \"http://localhost:8000/friendbot\" | grep '\"invalid_field\": \"addr\"'"
3434
--health-interval 10s
3535
--health-timeout 5s
3636
--health-retries 50

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Pending
4+
- refactor!: remove `InvokeHostFunctionOperation.createStellarAssetContractOperationBuilder(Address, byte[])`, use `InvokeHostFunctionOperation.createContractOperationBuilder` instead.
45

56
## 2.2.2
67

src/main/java/org/stellar/sdk/operations/InvokeHostFunctionOperation.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -191,49 +191,6 @@ public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) {
191191
return builder().hostFunction(hostFunction);
192192
}
193193

194-
/**
195-
* This function will create an {@link InvokeHostFunctionOperationBuilder} with the "hostFunction"
196-
* parameter preset, so that you can conveniently build an {@link InvokeHostFunctionOperation} to
197-
* create a token contract wrapping a classic asset.
198-
*
199-
* @param address The address to use to derive the contract ID.
200-
* @param salt The 32-byte salt to use to derive the contract ID, if null, a random salt will be
201-
* generated.
202-
* @return {@link InvokeHostFunctionOperationBuilder}
203-
*/
204-
public static InvokeHostFunctionOperationBuilder<?, ?> createStellarAssetContractOperationBuilder(
205-
Address address, @Nullable byte[] salt) {
206-
if (salt == null) {
207-
salt = new byte[32];
208-
new SecureRandom().nextBytes(salt);
209-
} else if (salt.length != 32) {
210-
throw new IllegalArgumentException("\"salt\" must be 32 bytes long");
211-
}
212-
213-
CreateContractArgs createContractArgs =
214-
CreateContractArgs.builder()
215-
.contractIDPreimage(
216-
ContractIDPreimage.builder()
217-
.discriminant(ContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ADDRESS)
218-
.fromAddress(
219-
ContractIDPreimage.ContractIDPreimageFromAddress.builder()
220-
.address(address.toSCAddress())
221-
.salt(new Uint256(salt))
222-
.build())
223-
.build())
224-
.executable(
225-
ContractExecutable.builder()
226-
.discriminant(ContractExecutableType.CONTRACT_EXECUTABLE_STELLAR_ASSET)
227-
.build())
228-
.build();
229-
HostFunction hostFunction =
230-
HostFunction.builder()
231-
.discriminant(HostFunctionType.HOST_FUNCTION_TYPE_CREATE_CONTRACT)
232-
.createContract(createContractArgs)
233-
.build();
234-
return builder().hostFunction(hostFunction);
235-
}
236-
237194
/**
238195
* This function will create an {@link InvokeHostFunctionOperationBuilder} with the "hostFunction"
239196
* parameter preset, so that you can conveniently build an {@link InvokeHostFunctionOperation} to

src/test/java/org/stellar/sdk/IntegrationUtils.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.stellar.sdk;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
5+
import java.io.InputStream;
46
import java.math.BigDecimal;
57
import java.math.BigInteger;
68
import java.net.HttpURLConnection;
@@ -17,7 +19,7 @@
1719
import org.stellar.sdk.xdr.SCVal;
1820

1921
public class IntegrationUtils {
20-
public static final String RPC_URL = "http://127.0.0.1:8000/soroban/rpc";
22+
public static final String RPC_URL = "http://127.0.0.1:8000/rpc";
2123
public static final String HORIZON_URL = "http://127.0.0.1:8000";
2224
public static final String FRIEND_BOT_URL = "http://127.0.0.1:8000/friendbot";
2325
public static final Network NETWORK = Network.STANDALONE;
@@ -57,12 +59,52 @@ public static void createAssetContract(Asset asset, KeyPair source) {
5759
}
5860
}
5961

62+
public static byte[] uploadWasm(byte[] contractWasm, KeyPair source) {
63+
try (SorobanServer server = new SorobanServer(RPC_URL)) {
64+
TransactionBuilderAccount account = server.getAccount(source.getAccountId());
65+
InvokeHostFunctionOperation op =
66+
InvokeHostFunctionOperation.uploadContractWasmOperationBuilder(contractWasm).build();
67+
TransactionBuilder transactionBuilder =
68+
new TransactionBuilder(account, NETWORK).setBaseFee(100).addOperation(op).setTimeout(300);
69+
AssembledTransaction<byte[]> assembledTransaction =
70+
new AssembledTransaction<>(transactionBuilder, server, source, Scv::fromBytes, 300);
71+
AssembledTransaction<byte[]> simulated = assembledTransaction.simulate(false);
72+
if (simulated.isReadCall()) {
73+
return simulated.result();
74+
} else {
75+
return simulated.signAndSubmit(source, false);
76+
}
77+
} catch (IOException e) {
78+
throw new RuntimeException(e);
79+
}
80+
}
81+
6082
public static String getRandomContractId(KeyPair source) {
83+
byte[] wasm;
84+
try (InputStream is =
85+
IntegrationUtils.class.getResourceAsStream(
86+
"/wasm_files/soroban_hello_world_contract.wasm")) {
87+
if (is == null) {
88+
throw new RuntimeException("soroban_hello_world_contract.wasm not found");
89+
}
90+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
91+
byte[] data = new byte[1024];
92+
int bytesRead;
93+
while ((bytesRead = is.read(data, 0, data.length)) != -1) {
94+
buffer.write(data, 0, bytesRead);
95+
}
96+
wasm = buffer.toByteArray();
97+
} catch (IOException e) {
98+
throw new RuntimeException(e);
99+
}
100+
101+
byte[] wasmId = uploadWasm(wasm, source);
102+
61103
try (SorobanServer server = new SorobanServer(RPC_URL)) {
62104
TransactionBuilderAccount account = server.getAccount(source.getAccountId());
63105
InvokeHostFunctionOperation op =
64-
InvokeHostFunctionOperation.createStellarAssetContractOperationBuilder(
65-
new Address(source.getAccountId()), null)
106+
InvokeHostFunctionOperation.createContractOperationBuilder(
107+
wasmId, new Address(source.getAccountId()), null, null)
66108
.build();
67109
TransactionBuilder transactionBuilder =
68110
new TransactionBuilder(account, NETWORK).setBaseFee(100).addOperation(op).setTimeout(300);

src/test/java/org/stellar/sdk/operations/InvokeHostFunctionOperationTest.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -469,48 +469,6 @@ public void createContractOperationBuilderWithConstructorArgs() {
469469
assertEquals(expectedXdr, operation.toXdrBase64());
470470
}
471471

472-
@Test
473-
public void createStellarAssetContractOperationBuilderWithAddress() {
474-
Address address = new Address("GAHJJJKMOKYE4RVPZEWZTKH5FVI4PA3VL7GK2LFNUBSGBV6OJP7TQSLX");
475-
byte[] salt =
476-
new byte[] {
477-
0x11, 0x33, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
478-
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
479-
0x1e, 0x1f
480-
};
481-
InvokeHostFunctionOperation operation =
482-
InvokeHostFunctionOperation.createStellarAssetContractOperationBuilder(address, salt)
483-
.build();
484-
CreateContractArgs createContractArgs =
485-
CreateContractArgs.builder()
486-
.contractIDPreimage(
487-
ContractIDPreimage.builder()
488-
.discriminant(ContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ADDRESS)
489-
.fromAddress(
490-
ContractIDPreimage.ContractIDPreimageFromAddress.builder()
491-
.address(address.toSCAddress())
492-
.salt(new Uint256(salt))
493-
.build())
494-
.build())
495-
.executable(
496-
ContractExecutable.builder()
497-
.discriminant(ContractExecutableType.CONTRACT_EXECUTABLE_STELLAR_ASSET)
498-
.build())
499-
.build();
500-
HostFunction expectedFunction =
501-
HostFunction.builder()
502-
.discriminant(HostFunctionType.HOST_FUNCTION_TYPE_CREATE_CONTRACT)
503-
.createContract(createContractArgs)
504-
.build();
505-
506-
assertEquals(operation.getHostFunction(), expectedFunction);
507-
assertTrue(operation.getAuth().isEmpty());
508-
assertNull(operation.getSourceAccount());
509-
String expectedXdr =
510-
"AAAAAAAAABgAAAABAAAAAAAAAAAAAAAADpSlTHKwTkavyS2ZqP0tUceDdV/MrSytoGRg185L/zgRMwIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHwAAAAEAAAAA";
511-
assertEquals(expectedXdr, operation.toXdrBase64());
512-
}
513-
514472
@Test
515473
public void createStellarAssetContractOperationBuilderWithAsset() {
516474
Asset asset =
660 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)