-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathIntegrationUtils.java
More file actions
170 lines (156 loc) · 6.8 KB
/
IntegrationUtils.java
File metadata and controls
170 lines (156 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.stellar.sdk;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import org.stellar.sdk.contract.AssembledTransaction;
import org.stellar.sdk.contract.ContractClient;
import org.stellar.sdk.contract.exception.SimulationFailedException;
import org.stellar.sdk.operations.ChangeTrustOperation;
import org.stellar.sdk.operations.InvokeHostFunctionOperation;
import org.stellar.sdk.operations.PaymentOperation;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.scval.Scv;
import org.stellar.sdk.xdr.SCVal;
public class IntegrationUtils {
public static final String RPC_URL = "http://127.0.0.1:8000/rpc";
public static final String HORIZON_URL = "http://127.0.0.1:8000";
public static final String FRIEND_BOT_URL = "http://127.0.0.1:8000/friendbot";
public static final Network NETWORK = Network.STANDALONE;
public static void fundAccount(String accountId) throws IOException {
HttpURLConnection connection = null;
try {
URL url = new URL(FRIEND_BOT_URL + "?addr=" + accountId);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
throw new IOException("HTTP error code: " + responseCode);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public static void createAssetContract(Asset asset, KeyPair source) {
try (SorobanServer server = new SorobanServer(RPC_URL)) {
TransactionBuilderAccount account = server.getAccount(source.getAccountId());
InvokeHostFunctionOperation op =
InvokeHostFunctionOperation.createStellarAssetContractOperationBuilder(asset).build();
TransactionBuilder transactionBuilder =
new TransactionBuilder(account, NETWORK).setBaseFee(100).addOperation(op).setTimeout(300);
AssembledTransaction<SCVal> assembledTransaction =
new AssembledTransaction<>(transactionBuilder, server, source, null, 300);
assembledTransaction.simulate(true).signAndSubmit(source, false);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SimulationFailedException ignored) {
}
}
public static byte[] uploadWasm(byte[] contractWasm, KeyPair source) {
try (SorobanServer server = new SorobanServer(RPC_URL)) {
TransactionBuilderAccount account = server.getAccount(source.getAccountId());
InvokeHostFunctionOperation op =
InvokeHostFunctionOperation.uploadContractWasmOperationBuilder(contractWasm).build();
TransactionBuilder transactionBuilder =
new TransactionBuilder(account, NETWORK).setBaseFee(100).addOperation(op).setTimeout(300);
AssembledTransaction<byte[]> assembledTransaction =
new AssembledTransaction<>(transactionBuilder, server, source, Scv::fromBytes, 300);
AssembledTransaction<byte[]> simulated = assembledTransaction.simulate(false);
if (simulated.isReadCall()) {
return simulated.result();
} else {
return simulated.signAndSubmit(source, false);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getRandomContractId(KeyPair source) {
byte[] wasm;
try (InputStream is =
IntegrationUtils.class.getResourceAsStream(
"/wasm_files/soroban_hello_world_contract.wasm")) {
if (is == null) {
throw new RuntimeException("soroban_hello_world_contract.wasm not found");
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytesRead);
}
wasm = buffer.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
byte[] wasmId = uploadWasm(wasm, source);
try (SorobanServer server = new SorobanServer(RPC_URL)) {
TransactionBuilderAccount account = server.getAccount(source.getAccountId());
InvokeHostFunctionOperation op =
InvokeHostFunctionOperation.createContractOperationBuilder(
wasmId, new Address(source.getAccountId()), null, null)
.build();
TransactionBuilder transactionBuilder =
new TransactionBuilder(account, NETWORK).setBaseFee(100).addOperation(op).setTimeout(300);
AssembledTransaction<Address> assembledTransaction =
new AssembledTransaction<>(transactionBuilder, server, source, Scv::fromAddress, 300);
Address contractId = assembledTransaction.simulate(true).signAndSubmit(source, false);
// TODO: This is strange; after the transaction above is submitted, the account's seq does not
// update immediately.
Thread.sleep(1000);
return contractId.toString();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
public static BigInteger getBalanceForContract(String contractId, Asset asset, KeyPair source) {
try (ContractClient contractClient =
new ContractClient(asset.getContractId(NETWORK), RPC_URL, NETWORK)) {
return contractClient
.invoke(
"balance",
Collections.singletonList(Scv.toAddress(contractId)),
source.getAccountId(),
source,
Scv::fromInt128,
100)
.result();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void issueAsset(
String assetCode, KeyPair issuer, KeyPair receiver, BigDecimal amount) {
Asset asset = Asset.createNonNativeAsset(assetCode, issuer.getAccountId());
try (Server server = new Server(HORIZON_URL)) {
AccountResponse issuerAccount = server.accounts().account(issuer.getAccountId());
Transaction transaction =
new TransactionBuilder(issuerAccount, NETWORK)
.setBaseFee(100)
.addOperation(
ChangeTrustOperation.builder()
.asset(new ChangeTrustAsset(asset))
.sourceAccount(receiver.getAccountId())
.limit(new BigDecimal("922337203685.4775807"))
.build())
.addOperation(
PaymentOperation.builder()
.asset(asset)
.amount(amount)
.destination(receiver.getAccountId())
.sourceAccount(issuer.getAccountId())
.build())
.setTimeout(300)
.build();
transaction.sign(issuer);
transaction.sign(receiver);
server.submitTransaction(transaction);
}
}
}