-
Notifications
You must be signed in to change notification settings - Fork 113
/
ContractFunctionsWrapper.java
327 lines (269 loc) · 13.6 KB
/
ContractFunctionsWrapper.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.hedera.examples.contractWrappers;
import java.io.File;
import java.io.FileReader;
import java.math.BigInteger;
import org.ethereum.core.CallTransaction;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import com.hedera.examples.utilities.ExampleUtilities;
import com.hedera.sdk.common.HederaTransactionReceipt;
import com.hedera.sdk.common.HederaTransactionRecord;
import com.hedera.sdk.common.Utilities;
import com.hedera.sdk.contract.HederaContract;
import com.hedera.sdk.contract.HederaContractFunctionResult;
import com.hedera.sdk.node.HederaNodeList;
import com.hedera.sdk.transaction.HederaTransactionResult;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
import edu.emory.mathcs.backport.java.util.Arrays;
public class ContractFunctionsWrapper {
private JSONArray abis;
final ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(ContractFunctionsWrapper.class);
public void setABI(String abi) throws ParseException {
this.abis = (JSONArray) JSONValue.parseWithException(abi);
}
public void setABIFromFile(String fileLocation) throws Exception {
File abiFile = new File(fileLocation);
JSONParser parser = new JSONParser();
Object abisObject = parser.parse(new FileReader(abiFile));
this.abis = (JSONArray) abisObject;
}
private JSONObject findABI(String functionName) throws Exception {
JSONObject abi = new JSONObject();
boolean bFound = false;
for (int i=0; i < this.abis.size(); i++) {
abi = (JSONObject) this.abis.get(i);
if (abi.get("name").equals(functionName) ) {
bFound = true;
break;
}
}
if (!bFound) {
throw new Exception ("Function " + functionName + " not found in ABI.");
}
return abi;
}
private JSONObject findConstructor() throws Exception {
JSONObject abi = new JSONObject();
boolean bFound = false;
for (int i=0; i < this.abis.size(); i++) {
abi = (JSONObject) this.abis.get(i);
if (abi.get("type").equals("constructor") ) {
bFound = true;
break;
}
}
if (!bFound) {
throw new Exception ("Constructor not found in ABI.");
}
return abi;
}
public byte[] constructor(Object ... parameterValues) throws Exception {
JSONObject abiJSON = findConstructor();
String abi = abiJSON.toString();
JSONArray inputs = (JSONArray) abiJSON.get("inputs");
if (inputs.size() != parameterValues.length) {
throw new Exception ("Numer of parameter values mismatch with abi inputs");
}
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(abi);
byte[] constructor = new byte[0];
if (parameterValues.length == 0) {
constructor = function.encode();
} else {
constructor = function.encodeArguments(parameterValues);
}
return constructor;
}
public byte[] functionCall(String functionName, Object ... parameterValues) throws Exception {
JSONObject abiJSON = findABI(functionName);
String abi = abiJSON.toString();
JSONArray inputs = (JSONArray) abiJSON.get("inputs");
if (inputs.size() != parameterValues.length) {
throw new Exception ("Numer of parameter values mismatch with abi inputs");
}
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(abi);
byte[] encodedFunc = new byte[0];
if (parameterValues.length == 0) {
encodedFunc = function.encode();
} else {
encodedFunc = function.encode(parameterValues);
}
return encodedFunc;
}
public HederaContractFunctionResult callLocal(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
JSONObject abiJSON = findABI(functionName);
String abi = abiJSON.toString();
JSONArray inputs = (JSONArray) abiJSON.get("inputs");
if (inputs.size() != parameterValues.length) {
throw new Exception ("Numer of parameter values mismatch with abi inputs");
}
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(abi);
byte[] encodedFunc = new byte[0];
if (parameterValues.length == 0) {
encodedFunc = function.encode();
} else {
encodedFunc = function.encode(parameterValues);
}
ExampleUtilities.showResult("** CONTRACT RUN LOCAL");
HederaContractFunctionResult functionResult = new HederaContractFunctionResult();
// run a call local query
functionResult = contract.callLocal(localGas, encodedFunc, maxResultSize);
if (functionResult != null) {
// it was successful, print it
ExampleUtilities.showResult(String.format("** Got error message=%s\n"
+ "** Got gas used=%d\n"
+ "** Got contract num=%d"
,functionResult.errorMessage()
,functionResult.gasUsed()
,functionResult.contractID().contractNum));
} else if (contract.getPrecheckResult() == ResponseCodeEnum.BUSY) {
logger.info("system busy, try again later");
functionResult = null;
} else {
// an error occurred
ExampleUtilities.showResult("** Running local function - precheck ERROR " + contract.getPrecheckResult());
}
if (functionResult == null) {
throw new Exception ("Error running local smart contract function " + functionName);
}
return functionResult;
}
public int callLocalInt(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
BigInteger bigResult = (BigInteger) retResults[0];
return bigResult.intValue();
}
public String callLocalString(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (String) retResults[0];
}
public BigInteger callLocalBigInt(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (BigInteger) retResults[0];
}
public boolean callLocalBoolean(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (boolean) retResults[0];
}
public long callLocalLong(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
BigInteger bigResult = (BigInteger) retResults[0];
return bigResult.longValue();
}
public String callLocalAddress(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return Hex.toHexString((byte[]) retResults[0]);
}
public HederaContractFunctionResult callFunctionResult(HederaContract contract, long gas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaTransactionRecord record = this.callForRecord(contract, gas, amount, functionName, parameterValues);
return record.contractCallResult;
}
public Object[] callForObjectArray(HederaContract contract, long gas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaTransactionRecord record = this.callForRecord(contract, gas, amount, functionName, parameterValues);
return getFunctionResult(functionName, record.contractCallResult);
}
public HederaTransactionRecord callForRecord(HederaContract contract, long gas, long amount, String functionName, Object ... parameterValues) throws Exception {
JSONObject abiJSON = findABI(functionName);
String abi = abiJSON.toString();
JSONArray inputs = (JSONArray) abiJSON.get("inputs");
if (inputs.size() != parameterValues.length) {
throw new Exception ("Numer of parameter values mismatch with abi inputs");
}
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(abi);
byte[] encodedFunc = new byte[0];
if (parameterValues.length == 0) {
encodedFunc = function.encode();
} else {
encodedFunc = function.encode(parameterValues);
}
ExampleUtilities.showResult("** CONTRACT CALL");
// call the smart contract
// smart contract call transaction
HederaTransactionResult callResult = contract.call(gas, amount, encodedFunc);
// was it successful ?
if (callResult.getPrecheckResult() == ResponseCodeEnum.OK) {
// yes, get a receipt for the transaction
HederaTransactionReceipt receipt = Utilities.getReceipt(contract.hederaTransactionID,
HederaNodeList.randomNode());
// was that successful ?
if (receipt.transactionStatus == ResponseCodeEnum.SUCCESS) {
// and print it out
ExampleUtilities.showResult(String.format("** Smart Contract call success"));
} else {
ExampleUtilities.showResult("** TransactionStatus:" + receipt.transactionStatus.toString());
// callResult = null;
}
} else if (contract.getPrecheckResult() == ResponseCodeEnum.BUSY) {
logger.info("system busy, try again later");
callResult = null;
} else {
ExampleUtilities.showResult("** Failed with getPrecheckResult:" + contract.getPrecheckResult());
callResult = null;
}
if (callResult == null) {
throw new Exception ("Error running local smart contract function " + functionName);
}
HederaTransactionRecord record = new HederaTransactionRecord(contract.hederaTransactionID, HederaNodeList.randomNode().contractGetRecordsQueryFee, contract.txQueryDefaults);
return record;
}
public int callInt(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
BigInteger bigResult = (BigInteger) retResults[0];
return bigResult.intValue();
}
public String callString(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (String) retResults[0];
}
public BigInteger callBigInt(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (BigInteger) retResults[0];
}
public boolean callBoolean(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return (boolean) retResults[0];
}
public long callLong(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
BigInteger bigResult = (BigInteger) retResults[0];
return bigResult.longValue();
}
public String callAddress(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
HederaContractFunctionResult result = this.callFunctionResult(contract, localGas, amount, functionName, parameterValues);
Object[] retResults = getFunctionResult(functionName, result);
return Hex.toHexString((byte[]) retResults[0]);
}
public String outputData(HederaContractFunctionResult result) {
byte[] callResult = result.contractCallResult();
String encodeResult = Hex.toHexString(callResult);
int dataOffsetStart = 8;
int dataOffsetEnd = 8 + 64;
int offset = 2 * Integer.parseInt(encodeResult.substring(dataOffsetStart, dataOffsetEnd), 16);
int lengthStart = 8 + 64;
int lengthEnd = 8 + 64 + 64;
int len = 2 * Integer.parseInt(encodeResult.substring(lengthStart, lengthEnd), 16);
byte[] resultBytes = Arrays.copyOfRange(callResult, offset, offset+len);
return new String(resultBytes);
}
public Object[] getFunctionResult(String functionName, HederaContractFunctionResult result) throws Exception {
JSONObject abiJSON = findABI(functionName);
String abi = abiJSON.toString();
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(abi);
return function.decodeResult(result.contractCallResult());
}
}