use of com.hedera.hashgraph.sdk.Hbar 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.Hbar 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.Hbar in project hedera-sdk-java by hashgraph.
the class CryptoTransferFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recipientAccountId = view.findViewById(R.id.recipientAccountId);
amountToSend = view.findViewById(R.id.amountToSend);
resultText = view.findViewById(R.id.transferResult);
view.findViewById(R.id.sendHbar).setOnClickListener(v -> {
try {
final AccountId operatorId = AccountId.fromString(view.getResources().getString(R.string.operator_id));
final Ed25519PrivateKey operatorKey = Ed25519PrivateKey.fromString(view.getResources().getString(R.string.operator_key));
Client client = Client.forTestnet().setOperator(operatorId, operatorKey);
final AccountId recipientId = AccountId.fromString(recipientAccountId.getText().toString());
final Hbar amount = new Hbar(new BigDecimal(amountToSend.getText().toString()).longValueExact());
new TransferAsyncTask(operatorId, recipientId, amount).execute(client);
} catch (Throwable e) {
resultText.setText("Error: " + e.getMessage());
}
});
}
use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class AccountAliasExample 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);
/*
* Hedera supports a form of lazy account creation.
*
* You can "create" an account by generating a private key, and then deriving the public key,
* without any need to interact with the Hedera network. The public key more or less acts as the user's
* account ID. This public key is an account's aliasKey: a public key that aliases (or will eventually alias)
* to a Hedera account.
*
* An AccountId takes one of two forms: a normal AccountId with a null aliasKey member takes the form 0.0.123,
* while an account ID with a non-null aliasKey member takes the form
* 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
* Note the prefix of "0.0." indicating the shard and realm. Also note that the aliasKey is stringified
* as a hex-encoded ASN1 DER representation of the key.
*
* An AccountId with an aliasKey can be used just like a normal AccountId for the purposes of queries and
* transactions, however most queries and transactions involving such an AccountId won't work until Hbar has
* been transferred to the aliasKey account.
*
* There is no record in the Hedera network of an account associated with a given aliasKey
* until an amount of Hbar is transferred to the account. The moment that Hbar is transferred to that aliasKey
* AccountId is the moment that that account actually begins to exist in the Hedera ledger.
*/
System.out.println("\"Creating\" a new account");
PrivateKey privateKey = PrivateKey.generateED25519();
PublicKey publicKey = privateKey.getPublicKey();
// Assuming that the target shard and realm are known.
// For now they are virtually always 0 and 0.
AccountId aliasAccountId = publicKey.toAccountId(0, 0);
System.out.println("New account ID: " + aliasAccountId);
System.out.println("Just the aliasKey: " + aliasAccountId.aliasKey);
/*
* Note that no queries or transactions have taken place yet.
* This account "creation" process is entirely local.
*
* AccountId.fromString() can construct an AccountId with an aliasKey.
* It expects a string of the form 0.0.123 in the case of a normal AccountId, or of the form
* 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
* in the case of an AccountId with aliasKey. Note the prefix of "0.0." to indicate the shard and realm.
*
* If the shard and realm are known, you may use PublicKey.fromString().toAccountId() to construct the
* aliasKey AccountId
*/
AccountId fromString = AccountId.fromString("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777");
AccountId fromKeyString = PublicKey.fromString("302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").toAccountId(0, 0);
System.out.println("Transferring some Hbar to the new account");
new TransferTransaction().addHbarTransfer(OPERATOR_ID, new Hbar(10).negated()).addHbarTransfer(aliasAccountId, new Hbar(10)).execute(client).getReceipt(client);
AccountBalance balance = new AccountBalanceQuery().setAccountId(aliasAccountId).execute(client);
System.out.println("Balances of the new account: " + balance);
AccountInfo info = new AccountInfoQuery().setAccountId(aliasAccountId).execute(client);
System.out.println("Info about the new account: " + info);
/*
* Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
* via an AccountInfoQuery.
*
* Users may continue to refer to the account by its aliasKey AccountId, but they may also
* now refer to it by its normal AccountId
*/
System.out.println("The normal account ID: " + info.accountId);
System.out.println("The alias key: " + info.aliasKey);
System.out.println("Example complete!");
client.close();
}
use of com.hedera.hashgraph.sdk.Hbar 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);
}
Aggregations