-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_HelpArijit.java
More file actions
81 lines (72 loc) · 3.36 KB
/
_HelpArijit.java
File metadata and controls
81 lines (72 loc) · 3.36 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
import java.nio.ByteBuffer;
import com.google.protobuf.ByteString;
import com.hedera.hashgraph.sdk.contract.ContractFunctionResult;
import com.hedera.hashgraph.sdk.TransactionId;
import com.hedera.hashgraph.sdk.contract.ContractExecuteTransaction;
import com.hedera.hashgraph.sdk.contract.ContractId;
import com.hedera.hashgraph.sdk.Status;
import net.aoc.JCoHelper;
public class _HelpArijit {
private static ByteString rawResult = null;
public _HelpArijit() {
}
public static void main(String... arguments) throws Exception {
var IssuerId = JCoHelper.getOperatorId();
var client = JCoHelper.createHederaClient().setMaxTransactionFee(834_000_000);
String memoField = "Help Arijit";
//ContractId stringContract = new ContractId(0, 0, 46020);
//ContractId stringContract = new ContractId(0, 0, 46976);
ContractId stringContract = new ContractId(0, 0, 46972);
System.out.println("The Contract is " + stringContract);
if (stringContract != null) {
long cost = 150_000_000;
TransactionId txId = new TransactionId(IssuerId);
var contractTx = new ContractExecuteTransaction().setTransactionId(txId).setGas(50_400)
.setTransactionMemo(memoField).setContractId(stringContract).setFunction("readStringArray");
var contractReceipt = contractTx.execute(client).getReceipt(client);
if (contractReceipt.status != Status.Success) {
System.out.println("Error calling contract: " + contractReceipt.status);
return;
}
ContractFunctionResult contractResult = txId.getRecord(client).getContractExecuteResult();
rawResult = ByteString.copyFrom(contractResult.asBytes());
//Now we work on the String Array reader.
String[] contractStringArray = getStringArray(0);
System.out.println(contractStringArray);
}
}
public static String[] getStringArray(int valIndex) {
int offset = getIntValueAt(valIndex * 32);
int arrayLength = getIntValueAt(offset);
int[] offsetForEntry = new int[arrayLength];
int[] lengthPerEntry = new int[arrayLength];
String[] stringPerEntry = new String[arrayLength];
offset += 32 + arrayLength * 32;
for (int i = 0; i < arrayLength; i++ ) {
offsetForEntry[i] = offset;
System.out.println("Offset " + offsetForEntry[i]);
lengthPerEntry[i] = getIntValueAt(offsetForEntry[i]);
System.out.println("Length " + lengthPerEntry[i]);
offset += 32;
stringPerEntry[i] = getStringAt(offset,lengthPerEntry[i]);
System.out.println("String " + stringPerEntry[i]);
int mod32 = 32 - lengthPerEntry[i] % 32;
offset += lengthPerEntry[i] + mod32;
}
return stringPerEntry;
}
private static int getIntValueAt(int valueOffset) {
return getByteBuffer(valueOffset + 28).getInt();
}
private static ByteBuffer getByteBuffer(int offset) {
ByteBuffer byteBuffer = rawResult.asReadOnlyByteBuffer();
byteBuffer.position(byteBuffer.position() + offset);
return byteBuffer;
}
private static ByteString getByteString(int startIndex, int endIndex) {
return rawResult.substring(startIndex, endIndex);
}
public static String getStringAt(int valIndex, int len) {
return getByteString(valIndex, valIndex + len).toStringUtf8();
}
}