-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSmartContractClientImpl.java
More file actions
145 lines (127 loc) · 5.26 KB
/
Copy pathSmartContractClientImpl.java
File metadata and controls
145 lines (127 loc) · 5.26 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
package org.hiero.base.implementation;
import static org.hiero.base.implementation.ProtocolLayerClientImpl.MAX_GAS_LIMIT;
import com.hedera.hashgraph.sdk.ContractFunctionResult;
import com.hedera.hashgraph.sdk.ContractId;
import com.hedera.hashgraph.sdk.FileId;
import com.hedera.hashgraph.sdk.Hbar;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import org.hiero.base.FileClient;
import org.hiero.base.HieroException;
import org.hiero.base.SmartContractClient;
import org.hiero.base.data.ContractCallResult;
import org.hiero.base.data.ContractParam;
import org.hiero.base.protocol.ProtocolLayerClient;
import org.hiero.base.protocol.data.ContractCallRequest;
import org.hiero.base.protocol.data.ContractCreateRequest;
import org.hiero.base.protocol.data.ContractCreateResult;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SmartContractClientImpl implements SmartContractClient {
private static final Logger log = LoggerFactory.getLogger(SmartContractClientImpl.class);
private final ProtocolLayerClient protocolLayerClient;
private final FileClient fileClient;
public SmartContractClientImpl(
@NonNull final ProtocolLayerClient protocolLayerClient, FileClient fileClient) {
this.protocolLayerClient =
Objects.requireNonNull(protocolLayerClient, "protocolLayerClient must not be null");
this.fileClient = Objects.requireNonNull(fileClient, "fileClient must not be null");
}
@NonNull
@Override
public ContractId createContract(
@NonNull final FileId fileId,
@NonNull final Hbar maxTransactionFee,
final int gas,
@Nullable final ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(fileId, "fileId must not be null");
Objects.requireNonNull(maxTransactionFee, "maxTransactionFee must not be null");
if (gas < 0 || gas > MAX_GAS_LIMIT) {
throw new IllegalArgumentException(
"gas must be between 0 and " + MAX_GAS_LIMIT + " inclusive");
}
try {
final ContractCreateRequest request;
if (constructorParams == null) {
request = ContractCreateRequest.of(fileId, maxTransactionFee, gas);
} else {
request =
ContractCreateRequest.of(
fileId, maxTransactionFee, gas, Arrays.asList(constructorParams));
}
final ContractCreateResult result =
protocolLayerClient.executeContractCreateTransaction(request);
return result.contractId();
} catch (Exception e) {
throw new HieroException("Failed to create contract with fileId " + fileId, e);
}
}
@NonNull
@Override
public ContractId createContract(
@NonNull final byte[] contents,
@NonNull final Hbar maxTransactionFee,
final int gas,
@Nullable final ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(contents, "contents must not be null");
Objects.requireNonNull(maxTransactionFee, "maxTransactionFee must not be null");
try {
final FileId fileId = fileClient.createFile(contents);
final ContractId contract = createContract(fileId, maxTransactionFee, gas, constructorParams);
fileClient.deleteFile(fileId);
return contract;
} catch (Exception e) {
throw new HieroException("Failed to create contract out of byte array", e);
}
}
@NonNull
@Override
public ContractId createContract(
@NonNull final Path pathToBin,
@NonNull final Hbar maxTransactionFee,
final int gas,
@Nullable final ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(pathToBin, "pathToBin must not be null");
Objects.requireNonNull(maxTransactionFee, "maxTransactionFee must not be null");
try {
final byte[] bytes = Files.readAllBytes(pathToBin);
return createContract(bytes, maxTransactionFee, gas, constructorParams);
} catch (Exception e) {
throw new HieroException("Failed to create contract from path " + pathToBin, e);
}
}
@NonNull
@Override
public ContractCallResult callContractFunction(
@NonNull final ContractId contractId,
@NonNull final String functionName,
@NonNull final Hbar maxTransactionFee,
final int gas,
@Nullable ContractParam<?>... params)
throws HieroException {
Objects.requireNonNull(contractId, "contractId must not be null");
Objects.requireNonNull(functionName, "functionName must not be null");
Objects.requireNonNull(maxTransactionFee, "maxTransactionFee must not be null");
if (gas < 0 || gas > MAX_GAS_LIMIT) {
throw new IllegalArgumentException(
"gas must be between 0 and " + MAX_GAS_LIMIT + " inclusive");
}
try {
final ContractCallRequest request =
ContractCallRequest.of(contractId, functionName, maxTransactionFee, gas, params);
final ContractFunctionResult result =
protocolLayerClient.executeContractCallTransaction(request).contractFunctionResult();
return new ContractCallResultImpl(result);
} catch (Exception e) {
throw new HieroException(
"Failed to call function '" + functionName + "' on contract with id " + contractId, e);
}
}
}