use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction in project hedera-mirror-node by hashgraph.
the class ScheduleCreateTransactionSupplierTest method createWithCustomData.
@Test
void createWithCustomData() {
PublicKey adminKey = PrivateKey.generate().getPublicKey();
ScheduleCreateTransactionSupplier scheduleCreateTransactionSupplier = new ScheduleCreateTransactionSupplier();
scheduleCreateTransactionSupplier.setAdminKey(adminKey.toString());
scheduleCreateTransactionSupplier.setMaxTransactionFee(1);
scheduleCreateTransactionSupplier.setOperatorAccountId(ACCOUNT_ID.toString());
scheduleCreateTransactionSupplier.setPayerAccount(ACCOUNT_ID_2.toString());
ScheduleCreateTransaction actual = scheduleCreateTransactionSupplier.get();
assertThat(actual).returns(adminKey, a -> a.getAdminKey()).returns(ONE_TINYBAR, ScheduleCreateTransaction::getMaxTransactionFee).returns(ACCOUNT_ID_2, ScheduleCreateTransaction::getPayerAccountId).extracting(ScheduleCreateTransaction::getScheduleMemo, STRING).contains("Mirror node created test schedule");
}
use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction in project hedera-sdk-java by hashgraph.
the class ScheduleCreateIntegrationTest method canGetTransactionSchedule.
@Test
@Disabled
@DisplayName("Can get Transaction")
void canGetTransactionSchedule() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generateED25519();
var transaction = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(10));
var response = new ScheduleCreateTransaction().setScheduledTransaction(transaction).setAdminKey(testEnv.operatorKey).setPayerAccountId(testEnv.operatorId).execute(testEnv.client);
var scheduleId = Objects.requireNonNull(response.getReceipt(testEnv.client).scheduleId);
var info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
assertThat(info.executedAt).isNotNull();
assertThat(info.getScheduledTransaction()).isNotNull();
testEnv.close();
}
use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction in project hedera-sdk-java by hashgraph.
the class ScheduleCreateIntegrationTest method canSignSchedule2.
@Test
@DisplayName("Can sign schedule")
void canSignSchedule2() throws Exception {
var testEnv = new IntegrationTestEnv(1);
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
PrivateKey key3 = PrivateKey.generateED25519();
KeyList keyList = new KeyList();
keyList.add(key1.getPublicKey());
keyList.add(key2.getPublicKey());
keyList.add(key3.getPublicKey());
// Creat the account with the `KeyList`
TransactionResponse response = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(testEnv.client);
// This will wait for the receipt to become available
@Var TransactionReceipt receipt = response.getReceipt(testEnv.client);
AccountId accountId = Objects.requireNonNull(receipt.accountId);
// Generate a `TransactionId`. This id is used to query the inner scheduled transaction
// after we expect it to have been executed
TransactionId transactionId = TransactionId.generate(testEnv.operatorId);
// Create a transfer transaction with 2/3 signatures.
TransferTransaction transfer = new TransferTransaction().setTransactionId(transactionId).addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(testEnv.operatorId, new Hbar(1));
// Schedule the transactoin
ScheduleCreateTransaction scheduled = transfer.schedule();
receipt = scheduled.execute(testEnv.client).getReceipt(testEnv.client);
// Get the schedule ID from the receipt
ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
// Get the schedule info to see if `signatories` is populated with 2/3 signatures
@Var ScheduleInfo info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
assertThat(info.executedAt).isNull();
// Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
// since all 3 signatures have been provided.
ScheduleSignTransaction signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
signTransaction.sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
assertThat(info.executedAt).isNotNull();
new AccountDeleteTransaction().setAccountId(accountId).setTransferAccountId(testEnv.operatorId).freezeWith(testEnv.client).sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close();
}
use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction in project hedera-sdk-java by hashgraph.
the class ScheduledTransferExample 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);
Objects.requireNonNull(client.getOperatorAccountId());
/*
* A scheduled transaction is a transaction that has been proposed by an account,
* but which requires more signatures before it will actually execute on the Hedera network.
*
* For example, if Alice wants to transfer an amount of Hbar to Bob, and Bob has
* receiverSignatureRequired set to true, then that transaction must be signed by
* both Alice and Bob before the transaction will be executed.
*
* To solve this problem, Alice can propose the transaction by creating a scheduled
* transaction on the Hedera network which, if executed, would transfer Hbar from
* Alice to Bob. That scheduled transaction will have a ScheduleId by which we can
* refer to that scheduled transaction. Alice can communicate the ScheduleId to Bob, and
* then Bob can use a ScheduleSignTransaction to sign that scheduled transaction.
*
* Bob has a 30 minute window in which to sign the scheduled transaction, starting at the
* moment that Alice creates the scheduled transaction. If a scheduled transaction
* is not signed by all of the necessary signatories within the 30 minute window,
* that scheduled transaction will expire, and will not be executed.
*
* Once a scheduled transaction has all of the signatures necessary to execute, it will
* be executed on the Hedera network automatically. If you create a scheduled transaction
* on the Hedera network, but that transaction only requires your signature in order to
* execute and no one else's, that scheduled transaction will be automatically
* executed immediately.
*/
PrivateKey bobsKey = PrivateKey.generateED25519();
AccountId bobsId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(bobsKey).setInitialBalance(new Hbar(10)).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client).accountId;
Objects.requireNonNull(bobsId);
System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client));
System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client));
AccountBalance bobsInitialBalance = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's initial balance:");
System.out.println(bobsInitialBalance);
TransferTransaction transferToSchedule = new TransferTransaction().addHbarTransfer(client.getOperatorAccountId(), new Hbar(-10)).addHbarTransfer(bobsId, new Hbar(10));
System.out.println("Transfer to be scheduled:");
System.out.println(transferToSchedule);
/*
* The payerAccountId is the account that will be charged the fee
* for executing the scheduled transaction if/when it is executed.
* That fee is separate from the fee that we will pay to execute the
* ScheduleCreateTransaction itself.
*
* To clarify: Alice pays a fee to execute the ScheduleCreateTransaction,
* which creates the scheduled transaction on the Hedera network.
* She specifies when creating the scheduled transaction that Bob will pay
* the fee for the scheduled transaction when it is executed.
*
* If payerAccountId is not specified, the account who creates the scheduled transaction
* will be charged for executing the scheduled transaction.
*/
ScheduleId scheduleId = new ScheduleCreateTransaction().setScheduledTransaction(transferToSchedule).setPayerAccountId(bobsId).execute(client).getReceipt(client).scheduleId;
Objects.requireNonNull(scheduleId);
System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client));
/*
* Bob's balance should be unchanged. The transfer has been scheduled, but it hasn't been executed yet
* because it requires Bob's signature.
*/
AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's balance after scheduling the transfer (should be unchanged):");
System.out.println(bobsBalanceAfterSchedule);
/*
* Once Alice has communicated the scheduleId to Bob, Bob can query for information about the
* scheduled transaction.
*/
ScheduleInfo scheduledTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
System.out.println("Info about scheduled transaction:");
System.out.println(scheduledTransactionInfo);
/*
* getScheduledTransaction() will return an SDK Transaction object identical to the transaction
* that was scheduled, which Bob can then inspect like a normal transaction.
*/
Transaction<?> scheduledTransaction = scheduledTransactionInfo.getScheduledTransaction();
// We happen to know that this transaction is (or certainly ought to be) a TransferTransaction
if (scheduledTransaction instanceof TransferTransaction) {
TransferTransaction scheduledTransfer = (TransferTransaction) scheduledTransaction;
System.out.println("The scheduled transfer transaction from Bob's POV:");
System.out.println(scheduledTransfer);
} else {
System.out.println("The scheduled transaction was not a transfer transaction.");
System.out.println("Something has gone horribly wrong. Crashing...");
System.exit(-1);
}
new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
AccountBalance balanceAfterSigning = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's balance after signing the scheduled transaction:");
System.out.println(balanceAfterSigning);
ScheduleInfo postTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
System.out.println("Info on the scheduled transaction, executedAt should no longer be null:");
System.out.println(postTransactionInfo);
// Clean up
new AccountDeleteTransaction().setTransferAccountId(client.getOperatorAccountId()).setAccountId(bobsId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
client.close();
}
use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction in project hedera-sdk-java by hashgraph.
the class ScheduleCreateIntegrationTest method canCreateSchedule.
@Test
@Disabled
@DisplayName("Can create schedule")
void canCreateSchedule() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generateED25519();
var transaction = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(10));
var response = new ScheduleCreateTransaction().setScheduledTransaction(transaction).setAdminKey(testEnv.operatorKey).setPayerAccountId(testEnv.operatorId).execute(testEnv.client);
var scheduleId = Objects.requireNonNull(response.getReceipt(testEnv.client).scheduleId);
var info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
assertThat(info.executedAt).isNotNull();
testEnv.close();
}
Aggregations