Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.hiero.base;

import static org.hiero.base.implementation.ProtocolLayerClientImpl.DEFAULT_GAS;
import static org.hiero.base.protocol.data.ContractCreateRequest.DEFAULT_CONTRACT_CREATE_TRANSACTION_FEE;
import static org.hiero.base.protocol.data.TransactionRequest.DEFAULT_MAX_TRANSACTION_FEE;

import com.hedera.hashgraph.sdk.ContractId;
import com.hedera.hashgraph.sdk.FileId;
import com.hedera.hashgraph.sdk.Hbar;
import java.nio.file.Path;
import java.util.Objects;
import org.hiero.base.data.ContractCallResult;
Expand All @@ -16,7 +21,6 @@
* 'operator account'.
*/
public interface SmartContractClient {

/**
* Create a new smart contract based on the file the given file ID. The file must contain the
* bytecode for the contract.
Expand All @@ -30,8 +34,12 @@ public interface SmartContractClient {
default ContractId createContract(
@NonNull String fileId, @Nullable ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(fileId, "fileId");
return createContract(FileId.fromString(fileId), constructorParams);
Objects.requireNonNull(fileId, "fileId must not be null");
return createContract(
FileId.fromString(fileId),
DEFAULT_CONTRACT_CREATE_TRANSACTION_FEE,
DEFAULT_GAS,
constructorParams);
}

/**
Expand All @@ -43,9 +51,13 @@ default ContractId createContract(
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
default @NonNull ContractId createContract(
@NonNull FileId fileId, @Nullable ContractParam<?>... constructorParams)
throws HieroException;
throws HieroException {
Objects.requireNonNull(fileId, "fileId must not be null");
return createContract(
fileId, DEFAULT_CONTRACT_CREATE_TRANSACTION_FEE, DEFAULT_GAS, constructorParams);
}

/**
* Create a new smart contract with the given contents. The contents must be the bytecode for the
Expand All @@ -56,9 +68,13 @@ default ContractId createContract(
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
default @NonNull ContractId createContract(
@NonNull byte[] contents, @Nullable ContractParam<?>... constructorParams)
throws HieroException;
throws HieroException {
Objects.requireNonNull(contents, "contents must not be null");
return createContract(
contents, DEFAULT_CONTRACT_CREATE_TRANSACTION_FEE, DEFAULT_GAS, constructorParams);
}

/**
* Create a new smart contract based on a file. The contents of the file must be the bytecode for
Expand All @@ -69,8 +85,89 @@ default ContractId createContract(
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
default @NonNull ContractId createContract(
@NonNull Path pathToBin, @Nullable ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(pathToBin, "pathToBin must not be null");
return createContract(
pathToBin, DEFAULT_CONTRACT_CREATE_TRANSACTION_FEE, DEFAULT_GAS, constructorParams);
}

/**
* Create a new smart contract based on the file the given file ID, using a custom max transaction
* fee and gas. The file must contain the bytecode for the contract.
*
* @param fileId the ID of the file containing the contract bytecode
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param constructorParams the parameters to pass to the contract constructor
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
default @NonNull ContractId createContract(
@NonNull String fileId,
@NonNull Hbar maxTransactionFee,
int gas,
@Nullable ContractParam<?>... constructorParams)
throws HieroException {
Objects.requireNonNull(fileId, "fileId must not be null");
Objects.requireNonNull(maxTransactionFee, "maxTransactionFee must not be null");

return createContract(FileId.fromString(fileId), maxTransactionFee, gas, constructorParams);
}

/**
* Create a new smart contract based on the file with the given file ID, using a custom max
* transaction fee and gas. The file must contain the bytecode for the contract.
*
* @param fileId the ID of the file containing the contract bytecode
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param constructorParams the parameters to pass to the contract constructor
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
@NonNull FileId fileId,
@NonNull Hbar maxTransactionFee,
int gas,
@Nullable ContractParam<?>... constructorParams)
throws HieroException;

/**
* Create a new smart contract with the given contents, using a custom maximum transaction fee and
* gas. The contents must be the bytecode for the contract.
*
* @param contents the contents of the contract
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param constructorParams the parameters to pass to the contract constructor
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
@NonNull byte[] contents,
@NonNull Hbar maxTransactionFee,
int gas,
@Nullable ContractParam<?>... constructorParams)
throws HieroException;

/**
* Create a new smart contract based on a local file, using a custom maximum transaction fee and
* gas. The contents of the file must be the bytecode for the contract.
*
* @param pathToBin the path to the file containing the contract bytecode
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param constructorParams the parameters to pass to the contract constructor
* @return the ID of the new contract
* @throws HieroException if the contract could not be created
*/
@NonNull ContractId createContract(
@NonNull Path pathToBin,
@NonNull Hbar maxTransactionFee,
int gas,
@Nullable ContractParam<?>... constructorParams)
throws HieroException;

/**
Expand All @@ -88,8 +185,15 @@ default ContractCallResult callContractFunction(
@NonNull String functionName,
@Nullable ContractParam<?>... params)
throws HieroException {
Objects.requireNonNull(contractId, "contractId");
return callContractFunction(ContractId.fromString(contractId), functionName, params);
Objects.requireNonNull(contractId, "contractId must not be null");
Objects.requireNonNull(functionName, "functionName must not be null");

return callContractFunction(
ContractId.fromString(contractId),
functionName,
DEFAULT_MAX_TRANSACTION_FEE,
DEFAULT_GAS,
params);
}

/**
Expand All @@ -101,9 +205,61 @@ default ContractCallResult callContractFunction(
* @return the result of the function call
* @throws HieroException if the function could not be called
*/
default @NonNull ContractCallResult callContractFunction(
@NonNull ContractId contractId,
@NonNull String functionName,
@Nullable ContractParam<?>... params)
throws HieroException {
Objects.requireNonNull(contractId, "contractId must not be null");
Objects.requireNonNull(functionName, "functionName must not be null");

return callContractFunction(
contractId, functionName, DEFAULT_MAX_TRANSACTION_FEE, DEFAULT_GAS, params);
}

/**
* Call a function on a smart contract with custom max transaction fee and gas.
*
* @param contractId the ID of the contract
* @param functionName the name of the function to call
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param params the parameters to pass to the function
* @return the result of the function call
* @throws HieroException if the function could not be called
*/
@NonNull
default ContractCallResult callContractFunction(
@NonNull String contractId,
@NonNull String functionName,
@NonNull Hbar maxTransactionFee,
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");

return callContractFunction(
ContractId.fromString(contractId), functionName, maxTransactionFee, gas, params);
}

/**
* Call a function on a smart contract with custom max transaction fee and gas.
*
* @param contractId the ID of the contract
* @param functionName the name of the function to call
* @param maxTransactionFee the custom max transaction fee in Hbar
* @param gas the custom max gas that can be spent
* @param params the parameters to pass to the function
* @return the result of the function call
* @throws HieroException if the function could not be called
*/
@NonNull ContractCallResult callContractFunction(
@NonNull ContractId contractId,
@NonNull String functionName,
@NonNull Hbar maxTransactionFee,
int gas,
@Nullable ContractParam<?>... params)
throws HieroException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public class ProtocolLayerClientImpl implements ProtocolLayerClient {
private static final Logger log = LoggerFactory.getLogger(ProtocolLayerClientImpl.class);

public static final int DEFAULT_GAS = 5_000_000;
public static final int MAX_GAS_LIMIT = 15_000_000;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Place shared gas policy in a neutral API/domain type. Defining it on ProtocolLayerClientImpl reverses dependencies throughout the request and public API layers.

  • hiero-enterprise-base/src/main/java/org/hiero/base/implementation/ProtocolLayerClientImpl.java#L123-L123: move MAX_GAS_LIMIT and DEFAULT_GAS to the neutral type.
  • hiero-enterprise-base/src/main/java/org/hiero/base/protocol/data/ContractCallRequest.java#L3-L4: import the limit from that neutral type.
  • hiero-enterprise-base/src/main/java/org/hiero/base/protocol/data/ContractCreateRequest.java#L3-L4: import the limit from that neutral type.
  • hiero-enterprise-base/src/main/java/org/hiero/base/SmartContractClient.java#L3-L5: import the default gas without depending on an implementation.
  • hiero-enterprise-base/src/main/java/org/hiero/base/implementation/SmartContractClientImpl.java#L3-L3: consume the same neutral policy.
📍 Affects 5 files
  • hiero-enterprise-base/src/main/java/org/hiero/base/implementation/ProtocolLayerClientImpl.java#L123-L123 (this comment)
  • hiero-enterprise-base/src/main/java/org/hiero/base/protocol/data/ContractCallRequest.java#L3-L4
  • hiero-enterprise-base/src/main/java/org/hiero/base/protocol/data/ContractCreateRequest.java#L3-L4
  • hiero-enterprise-base/src/main/java/org/hiero/base/SmartContractClient.java#L3-L5
  • hiero-enterprise-base/src/main/java/org/hiero/base/implementation/SmartContractClientImpl.java#L3-L3


private final List<TransactionListener> listeners;

Expand Down Expand Up @@ -282,7 +283,7 @@ public ContractCreateResult executeContractCreateTransaction(
.setMaxTransactionFee(request.maxTransactionFee())
.setTransactionValidDuration(request.transactionValidDuration())
.setBytecodeFileId(request.fileId())
.setGas(DEFAULT_GAS)
.setGas(request.gas())
.setConstructorParameters(constructorParams);
final TransactionReceipt receipt =
executeTransactionAndWaitOnReceipt(transaction, TransactionType.CONTRACT_CREATE);
Expand Down Expand Up @@ -321,7 +322,7 @@ public ContractCallResult executeContractCallTransaction(
.setTransactionValidDuration(request.transactionValidDuration())
.setContractId(request.contractId())
.setFunction(request.functionName(), functionParams)
.setGas(DEFAULT_GAS);
.setGas(request.gas());
final TransactionRecord record =
executeTransactionAndWaitOnRecord(transaction, TransactionType.CONTRACT_CALL);
return new ContractCallResult(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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;
Expand Down Expand Up @@ -39,14 +42,27 @@ public SmartContractClientImpl(
@NonNull
@Override
public ContractId createContract(
@NonNull final FileId fileId, @Nullable final ContractParam<?>... constructorParams)
@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);
request = ContractCreateRequest.of(fileId, maxTransactionFee, gas);
} else {
request = ContractCreateRequest.of(fileId, Arrays.asList(constructorParams));
request =
ContractCreateRequest.of(
fileId, maxTransactionFee, gas, Arrays.asList(constructorParams));
}
final ContractCreateResult result =
protocolLayerClient.executeContractCreateTransaction(request);
Expand All @@ -59,11 +75,17 @@ public ContractId createContract(
@NonNull
@Override
public ContractId createContract(
@NonNull final byte[] contents, @Nullable final ContractParam<?>... constructorParams)
@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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If createContract(...) throws, is deleteFile() ever called?

Should it be

final FileId fileId = fileClient.createFile(contents);
try {
    return createContract(fileId, maxTransactionFee, gas, constructorParams);
} finally {
    fileClient.deleteFile(fileId);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is to matche the existing flow we have, but happy to change the flow in this PR it needed :)

final ContractId contract = createContract(fileId, constructorParams);
final ContractId contract = createContract(fileId, maxTransactionFee, gas, constructorParams);
fileClient.deleteFile(fileId);
return contract;
Comment on lines 86 to 90

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Always delete the temporary bytecode file after creation attempts.

If delegated gas validation or contract creation fails, Line 89 is skipped, leaving a paid Hedera file orphaned. Validate before upload where possible and delete the file in finally; the Path overload inherits this leak.

Proposed cleanup
+    FileId fileId = null;
     try {
-      final FileId fileId = fileClient.createFile(contents);
-      final ContractId contract = createContract(fileId, maxTransactionFee, gas, constructorParams);
-      fileClient.deleteFile(fileId);
-      return contract;
+      fileId = fileClient.createFile(contents);
+      return createContract(fileId, maxTransactionFee, gas, constructorParams);
     } catch (Exception e) {
       throw new HieroException("Failed to create contract out of byte array", e);
+    } finally {
+      if (fileId != null) {
+        fileClient.deleteFile(fileId);
+      }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
final FileId fileId = fileClient.createFile(contents);
final ContractId contract = createContract(fileId, constructorParams);
final ContractId contract = createContract(fileId, maxTransactionFee, gas, constructorParams);
fileClient.deleteFile(fileId);
return contract;
FileId fileId = null;
try {
fileId = fileClient.createFile(contents);
return createContract(fileId, maxTransactionFee, gas, constructorParams);
} catch (Exception e) {
throw new HieroException("Failed to create contract out of byte array", e);
} finally {
if (fileId != null) {
fileClient.deleteFile(fileId);
}
}

} catch (Exception e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to catch more specific exceptions (e.g. IOException) here? Catching Exception also wraps programming errors like IllegalArgumentException, which may make debugging harder.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with this one too

Expand All @@ -74,11 +96,17 @@ public ContractId createContract(
@NonNull
@Override
public ContractId createContract(
@NonNull final Path pathToBin, @Nullable final ContractParam<?>... constructorParams)
@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, constructorParams);
return createContract(bytes, maxTransactionFee, gas, constructorParams);
} catch (Exception e) {
throw new HieroException("Failed to create contract from path " + pathToBin, e);
}
Expand All @@ -89,10 +117,23 @@ public ContractId createContract(
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, params);
final ContractCallRequest request =
ContractCallRequest.of(contractId, functionName, maxTransactionFee, gas, params);
final ContractFunctionResult result =
protocolLayerClient.executeContractCallTransaction(request).contractFunctionResult();
return new ContractCallResultImpl(result);
Expand Down
Loading