use of com.hedera.hashgraph.sdk.FileCreateTransaction in project hedera-sdk-java by hashgraph.
the class CreateSimpleContractExample method main.
public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException {
ClassLoader cl = CreateSimpleContractExample.class.getClassLoader();
Gson gson = new Gson();
JsonObject jsonObject;
try (InputStream jsonStream = cl.getResourceAsStream("hello_world.json")) {
if (jsonStream == null) {
throw new RuntimeException("failed to get hello_world.json");
}
jsonObject = gson.fromJson(new InputStreamReader(jsonStream, StandardCharsets.UTF_8), JsonObject.class);
}
String byteCodeHex = jsonObject.getAsJsonPrimitive("object").getAsString();
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// create the contract's bytecode file
TransactionResponse fileTransactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(byteCodeHex.getBytes(StandardCharsets.UTF_8)).setMaxTransactionFee(new Hbar(2)).execute(client);
TransactionReceipt fileReceipt = fileTransactionResponse.getReceipt(client);
FileId newFileId = Objects.requireNonNull(fileReceipt.fileId);
System.out.println("contract bytecode file: " + newFileId);
// create the contract itself
TransactionResponse contractTransactionResponse = new ContractCreateTransaction().setGas(500000).setBytecodeFileId(newFileId).setAdminKey(OPERATOR_KEY).setMaxTransactionFee(new Hbar(16)).execute(client);
TransactionReceipt contractReceipt = contractTransactionResponse.getReceipt(client);
System.out.println(contractReceipt);
ContractId newContractId = Objects.requireNonNull(contractReceipt.contractId);
System.out.println("new contract ID: " + newContractId);
ContractFunctionResult contractCallResult = new ContractCallQuery().setGas(500000).setContractId(newContractId).setFunction("greet").setQueryPayment(new Hbar(1)).execute(client);
if (contractCallResult.errorMessage != null) {
System.out.println("error calling contract: " + contractCallResult.errorMessage);
return;
}
String message = contractCallResult.getString(0);
System.out.println("contract message: " + message);
// now delete the contract
TransactionReceipt contractDeleteResult = new ContractDeleteTransaction().setContractId(newContractId).setTransferAccountId(contractTransactionResponse.transactionId.accountId).setMaxTransactionFee(new Hbar(1)).execute(client).getReceipt(client);
if (contractDeleteResult.status != Status.SUCCESS) {
System.out.println("error deleting contract: " + contractDeleteResult.status);
return;
}
System.out.println("Contract successfully deleted");
}
use of com.hedera.hashgraph.sdk.FileCreateTransaction in project hedera-sdk-java by hashgraph.
the class CreateStatefulContractExample method main.
public static void main(String[] args) throws PrecheckStatusException, TimeoutException, IOException, ReceiptStatusException {
ClassLoader cl = CreateStatefulContractExample.class.getClassLoader();
Gson gson = new Gson();
JsonObject jsonObject;
try (InputStream jsonStream = cl.getResourceAsStream("stateful.json")) {
if (jsonStream == null) {
throw new RuntimeException("failed to get stateful.json");
}
jsonObject = gson.fromJson(new InputStreamReader(jsonStream, StandardCharsets.UTF_8), JsonObject.class);
}
String byteCodeHex = jsonObject.getAsJsonPrimitive("object").getAsString();
byte[] byteCode = byteCodeHex.getBytes(StandardCharsets.UTF_8);
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// default max fee for all transactions executed by this client
client.setDefaultMaxTransactionFee(new Hbar(100));
client.setDefaultMaxQueryPayment(new Hbar(10));
// create the contract's bytecode file
TransactionResponse fileTransactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(byteCode).execute(client);
TransactionReceipt fileReceipt = fileTransactionResponse.getReceipt(client);
FileId newFileId = Objects.requireNonNull(fileReceipt.fileId);
System.out.println("contract bytecode file: " + newFileId);
TransactionResponse contractTransactionResponse = new ContractCreateTransaction().setBytecodeFileId(newFileId).setGas(500000).setConstructorParameters(new ContractFunctionParameters().addString("hello from hedera!")).execute(client);
TransactionReceipt contractReceipt = contractTransactionResponse.getReceipt(client);
ContractId newContractId = Objects.requireNonNull(contractReceipt.contractId);
System.out.println("new contract ID: " + newContractId);
ContractFunctionResult contractCallResult = new ContractCallQuery().setContractId(newContractId).setGas(500000).setFunction("get_message").setQueryPayment(new Hbar(1)).execute(client);
if (contractCallResult.errorMessage != null) {
System.out.println("error calling contract: " + contractCallResult.errorMessage);
return;
}
String message = contractCallResult.getString(0);
System.out.println("contract returned message: " + message);
TransactionResponse contractExecTransactionResponse = new ContractExecuteTransaction().setContractId(newContractId).setGas(500000).setFunction("set_message", new ContractFunctionParameters().addString("hello from hedera again!")).execute(client);
// if this doesn't throw then we know the contract executed successfully
contractExecTransactionResponse.getReceipt(client);
// now query contract
ContractFunctionResult contractUpdateResult = new ContractCallQuery().setContractId(newContractId).setGas(500000).setFunction("get_message").setQueryPayment(new Hbar(1)).execute(client);
if (contractUpdateResult.errorMessage != null) {
System.out.println("error calling contract: " + contractUpdateResult.errorMessage);
return;
}
String message2 = contractUpdateResult.getString(0);
System.out.println("contract returned message: " + message2);
}
use of com.hedera.hashgraph.sdk.FileCreateTransaction in project hedera-sdk-java by hashgraph.
the class FileAppendChunkedExample method main.
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY.getPublicKey()).setContents("Hello from Hedera.").setMaxTransactionFee(// 2 HBAR
new Hbar(2)).execute(client);
TransactionReceipt receipt = transactionResponse.getReceipt(client);
FileId newFileId = Objects.requireNonNull(receipt.fileId);
System.out.println("fileId: " + newFileId);
StringBuilder contents = new StringBuilder();
for (int i = 0; i <= 4096 * 9; i++) {
contents.append("1");
}
TransactionReceipt fileAppendReceipt = new FileAppendTransaction().setNodeAccountIds(Collections.singletonList(transactionResponse.nodeId)).setFileId(newFileId).setContents(contents.toString()).setMaxChunks(40).setMaxTransactionFee(new Hbar(1000)).freezeWith(client).execute(client).getReceipt(client);
System.out.println(fileAppendReceipt.toString());
FileInfo info = new FileInfoQuery().setFileId(newFileId).execute(client);
System.out.println("File size according to `FileInfoQuery`: " + info.size);
}
use of com.hedera.hashgraph.sdk.FileCreateTransaction in project hedera-sdk-java by hashgraph.
the class GetFileContentsExample method main.
public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// Content to be stored in the file
byte[] fileContents = "Hedera is great!".getBytes(StandardCharsets.UTF_8);
// Create the new file and set its properties
TransactionResponse newFileTransactionResponse = new FileCreateTransaction().setKeys(// The public key of the owner of the file
OPERATOR_KEY).setContents(// Contents of the file
fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
FileId newFileId = Objects.requireNonNull(newFileTransactionResponse.getReceipt(client).fileId);
// Print the file ID to console
System.out.println("The new file ID is " + newFileId.toString());
// Get file contents
ByteString contents = new FileContentsQuery().setFileId(newFileId).execute(client);
// Prints query results to console
System.out.println("File content query results: " + contents.toStringUtf8());
}
Aggregations