-
Notifications
You must be signed in to change notification settings - Fork 113
/
ContractFunctionsWrapper.java
287 lines (239 loc) · 11.2 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
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.transaction.HederaTransactionResult;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
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;
}
private Object[] callLocal(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] retResults = new Object[0];
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());
functionResult = null;
}
if (functionResult == null) {
throw new Exception ("Error running local smart contract function " + functionName);
}
retResults = function.decodeResult(functionResult.contractCallResult());
return retResults;
}
public int callLocalInt(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
BigInteger bigResult = (BigInteger) result[0];
return bigResult.intValue();
}
public String callLocalString(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
return (String) result[0];
}
public BigInteger callLocalBigInt(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
return (BigInteger) result[0];
}
public boolean callLocalBoolean(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
return (boolean) result[0];
}
public long callLocalLong(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
BigInteger bigResult = (BigInteger) result[0];
return bigResult.longValue();
}
public String callLocalAddress(HederaContract contract, long localGas, long maxResultSize, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.callLocal(contract, localGas, maxResultSize, functionName, parameterValues);
return Hex.toHexString((byte[]) result[0]);
}
public Object[] call(HederaContract contract, long gas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] retResults = new Object[0];
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,
contract.txQueryDefaults.node, 10, 4000, 0);
// was that successful ?
if (receipt.transactionStatus == ResponseCodeEnum.SUCCESS) {
// and print it out
ExampleUtilities.showResult(String.format("** Smart Contract call success"));
} else {
ExampleUtilities.showResult("** Failed with 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);
}
JSONArray outputs = (JSONArray) abiJSON.get("outputs");
if (outputs.size() != 0) {
// there are outputs, get the result
HederaTransactionRecord record = new HederaTransactionRecord(contract.hederaTransactionID, contract.txQueryDefaults.node.contractGetRecordsQueryFee, contract.txQueryDefaults);
HederaContractFunctionResult functionResult = record.contractCallResult;
retResults = function.decodeResult(functionResult.contractCallResult());
}
return retResults;
}
public int callInt(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
BigInteger bigResult = (BigInteger) result[0];
return bigResult.intValue();
}
public String callString(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
return (String) result[0];
}
public BigInteger callBigInt(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
return (BigInteger) result[0];
}
public boolean callBoolean(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
return (boolean) result[0];
}
public long callLong(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
BigInteger bigResult = (BigInteger) result[0];
return bigResult.longValue();
}
public String callAddress(HederaContract contract, long localGas, long amount, String functionName, Object ... parameterValues) throws Exception {
Object[] result = this.call(contract, localGas, amount, functionName, parameterValues);
return Hex.toHexString((byte[]) result[0]);
}
}