Search in sources :

Example 16 with Hbar

use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.

the class TokenUnfreezeIntegrationTest method canUnfreezeAccountWithToken.

@Test
@DisplayName("Can unfreeze account with token")
void canUnfreezeAccountWithToken() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    var key = PrivateKey.generateED25519();
    var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
    var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
    var tokenId = Objects.requireNonNull(new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setKycKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client).getReceipt(testEnv.client).tokenId);
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenUnfreezeTransaction().setAccountId(accountId).setTokenId(tokenId).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(tokenId, accountId, key);
}
Also used : TokenUnfreezeTransaction(com.hedera.hashgraph.sdk.TokenUnfreezeTransaction) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 17 with Hbar

use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.

the class ScheduleIdenticalTransactionExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    System.out.println("threshold key example");
    System.out.println("Keys:");
    PrivateKey[] privKeys = new PrivateKey[3];
    PublicKey[] pubKeys = new PublicKey[3];
    Client[] clients = new Client[3];
    AccountId[] accounts = new AccountId[3];
    @Var ScheduleId scheduleID = null;
    // Loop to generate keys, clients, and accounts
    for (int i = 0; i < 3; i++) {
        PrivateKey newKey = PrivateKey.generateED25519();
        privKeys[i] = newKey;
        pubKeys[i] = newKey.getPublicKey();
        System.out.println("Key #" + i + ":");
        System.out.println("private = " + privKeys[i]);
        System.out.println("public = " + pubKeys[i]);
        TransactionResponse createResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(1)).execute(client);
        // Make sure the transaction succeeded
        TransactionReceipt transactionReceipt = createResponse.getReceipt(client);
        Client newClient = Client.forName(HEDERA_NETWORK);
        newClient.setOperator(Objects.requireNonNull(transactionReceipt.accountId), newKey);
        clients[i] = newClient;
        accounts[i] = transactionReceipt.accountId;
        System.out.println("account = " + accounts[i]);
    }
    // Loop to generate keys, clients, and accounts
    // A threshold key with a threshold of 2 and length of 3 requires
    // at least 2 of the 3 keys to sign anything modifying the account
    KeyList keyList = KeyList.withThreshold(2);
    Collections.addAll(keyList, pubKeys);
    // We are using all of these keys, so the scheduled transaction doesn't automatically go through
    // It works perfectly fine with just one key
    TransactionResponse createResponse = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(client);
    // Make sure the transaction succeeded
    TransactionReceipt receipt = createResponse.getReceipt(client);
    AccountId thresholdAccount = receipt.accountId;
    System.out.println("threshold account = " + thresholdAccount);
    for (Client loopClient : clients) {
        AccountId operatorId = loopClient.getOperatorAccountId();
        // Each loopClient creates an identical transaction, sending 1 hbar to each of the created accounts,
        // sent from the threshold Account
        TransferTransaction tx = new TransferTransaction();
        for (AccountId account : accounts) {
            tx.addHbarTransfer(account, new Hbar(1));
        }
        tx.addHbarTransfer(Objects.requireNonNull(thresholdAccount), new Hbar(3).negated());
        ScheduleCreateTransaction scheduledTx = new ScheduleCreateTransaction().setScheduledTransaction(tx);
        scheduledTx.setPayerAccountId(thresholdAccount);
        TransactionResponse response = scheduledTx.execute(loopClient);
        TransactionReceipt loopReceipt = new TransactionReceiptQuery().setTransactionId(response.transactionId).setNodeAccountIds(Collections.singletonList(response.nodeId)).execute(loopClient);
        System.out.println("operator [" + operatorId + "]: scheduleID = " + loopReceipt.scheduleId);
        // Save the schedule ID, so that it can be asserted for each loopClient submission
        if (scheduleID == null) {
            scheduleID = loopReceipt.scheduleId;
        }
        if (!scheduleID.equals(Objects.requireNonNull(loopReceipt.scheduleId))) {
            System.out.println("invalid generated schedule id, expected " + scheduleID + ", got " + loopReceipt.scheduleId);
            return;
        }
        // If the status return by the receipt is related to already created, execute a schedule sign transaction
        if (loopReceipt.status == Status.IDENTICAL_SCHEDULE_ALREADY_CREATED) {
            TransactionResponse signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleID).setNodeAccountIds(Collections.singletonList(createResponse.nodeId)).setScheduleId(loopReceipt.scheduleId).execute(loopClient);
            TransactionReceipt signReceipt = new TransactionReceiptQuery().setTransactionId(signTransaction.transactionId).execute(client);
            if (signReceipt.status != Status.SUCCESS && signReceipt.status != Status.SCHEDULE_ALREADY_EXECUTED) {
                System.out.println("Bad status while getting receipt of schedule sign with operator " + operatorId + ": " + signReceipt.status);
                return;
            }
        }
    }
    System.out.println(new ScheduleInfoQuery().setScheduleId(scheduleID).execute(client));
    AccountDeleteTransaction thresholdDeleteTx = new AccountDeleteTransaction().setAccountId(thresholdAccount).setTransferAccountId(OPERATOR_ID).freezeWith(client);
    for (int i = 0; i < 3; i++) {
        thresholdDeleteTx.sign(privKeys[i]);
        new AccountDeleteTransaction().setAccountId(accounts[i]).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(privKeys[i]).execute(client).getReceipt(client);
    }
    thresholdDeleteTx.execute(client).getReceipt(client);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 18 with Hbar

use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.

the class ScheduleMultiSigTransactionExample method main.

public static void main(String[] args) throws Exception {
    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);
    AccountId operatorId = Objects.requireNonNull(client.getOperatorAccountId());
    // Generate 3 random keys
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    PrivateKey key3 = PrivateKey.generateED25519();
    // Create a keylist from those keys. This key will be used as the new account's key
    // The reason we want to use a `KeyList` is to simulate a multi-party system where
    // multiple keys are required to sign.
    KeyList keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    System.out.println("key1 private = " + key1);
    System.out.println("key1 public = " + key1.getPublicKey());
    System.out.println("key1 private = " + key2);
    System.out.println("key2 public = " + key2.getPublicKey());
    System.out.println("key1 private = " + key3);
    System.out.println("key3 public = " + key3.getPublicKey());
    System.out.println("keyList = " + keyList);
    // Creat the account with the `KeyList`
    TransactionResponse response = new AccountCreateTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).setKey(keyList).setInitialBalance(new Hbar(10)).execute(client);
    // This will wait for the receipt to become available
    @Var TransactionReceipt receipt = response.getReceipt(client);
    AccountId accountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("accountId = " + 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(operatorId);
    System.out.println("transactionId for scheduled transaction = " + transactionId);
    // Create a transfer transaction with 2/3 signatures.
    @Var TransferTransaction transfer = new TransferTransaction().addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(operatorId, new Hbar(1));
    // Schedule the transactoin
    ScheduleCreateTransaction scheduled = transfer.schedule().setPayerAccountId(client.getOperatorAccountId()).setAdminKey(client.getOperatorPublicKey()).freezeWith(client).sign(key2);
    receipt = scheduled.execute(client).getReceipt(client);
    // Get the schedule ID from the receipt
    ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
    System.out.println("scheduleId = " + scheduleId);
    // Get the schedule info to see if `signatories` is populated with 2/3 signatures
    ScheduleInfo info = new ScheduleInfoQuery().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).execute(client);
    System.out.println("Schedule Info = " + info);
    transfer = (TransferTransaction) info.getScheduledTransaction();
    Map<AccountId, Hbar> transfers = transfer.getHbarTransfers();
    // Make sure the transfer transaction is what we expect
    if (transfers.size() != 2) {
        throw new Exception("more transfers than expected");
    }
    if (!transfers.get(accountId).equals(new Hbar(1).negated())) {
        throw new Exception("transfer for " + accountId + " is not what is expected " + transfers.get(accountId));
    }
    if (!transfers.get(operatorId).equals(new Hbar(1))) {
        throw new Exception("transfer for " + operatorId + " is not what is expected " + transfers.get(operatorId));
    }
    System.out.println("sending schedule sign transaction");
    // Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
    // since all 3 signatures have been provided.
    new ScheduleSignTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).freezeWith(client).sign(key3).execute(client).getReceipt(client);
    // Query the schedule info again
    new ScheduleInfoQuery().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).execute(client);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 19 with Hbar

use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.

the class SignTransactionExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    PrivateKey user1Key = PrivateKey.generateED25519();
    PrivateKey user2Key = PrivateKey.generateED25519();
    KeyList keylist = new KeyList();
    keylist.add(user1Key);
    keylist.add(user2Key);
    TransactionResponse createAccountTransaction = new AccountCreateTransaction().setInitialBalance(new Hbar(2)).setKey(keylist).execute(client);
    @Var TransactionReceipt receipt = createAccountTransaction.getReceipt(client);
    System.out.println("account id = " + receipt.accountId);
    TransferTransaction transferTransaction = new TransferTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).addHbarTransfer(Objects.requireNonNull(receipt.accountId), Hbar.from(-1)).addHbarTransfer(new AccountId(3), new Hbar(1)).freezeWith(client);
    transferTransaction.signWithOperator(client);
    user1Key.signTransaction(transferTransaction);
    user2Key.signTransaction(transferTransaction);
    TransactionResponse result = transferTransaction.execute(client);
    receipt = result.getReceipt(client);
    System.out.println(receipt.status);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 20 with Hbar

use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.

the class TransferCryptoExample 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);
    AccountId recipientId = AccountId.fromString("0.0.3");
    Hbar amount = Hbar.fromTinybars(10_000);
    Hbar senderBalanceBefore = new AccountBalanceQuery().setAccountId(OPERATOR_ID).execute(client).hbars;
    Hbar receiptBalanceBefore = new AccountBalanceQuery().setAccountId(recipientId).execute(client).hbars;
    System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceBefore);
    System.out.println("" + recipientId + " balance = " + receiptBalanceBefore);
    TransactionResponse transactionResponse = new TransferTransaction().addHbarTransfer(OPERATOR_ID, amount.negated()).addHbarTransfer(recipientId, amount).setTransactionMemo("transfer test").execute(client);
    System.out.println("transaction ID: " + transactionResponse);
    TransactionRecord record = transactionResponse.getRecord(client);
    System.out.println("transferred " + amount + "...");
    Hbar senderBalanceAfter = new AccountBalanceQuery().setAccountId(OPERATOR_ID).execute(client).hbars;
    Hbar receiptBalanceAfter = new AccountBalanceQuery().setAccountId(recipientId).execute(client).hbars;
    System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceAfter);
    System.out.println("" + recipientId + " balance = " + receiptBalanceAfter);
    System.out.println("Transfer memo: " + record.transactionMemo);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Aggregations

Hbar (com.hedera.hashgraph.sdk.Hbar)106 Test (org.junit.jupiter.api.Test)77 DisplayName (org.junit.jupiter.api.DisplayName)75 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)62 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)34 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)31 Client (com.hedera.hashgraph.sdk.Client)22 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)22 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)19 Var (com.google.errorprone.annotations.Var)18 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)16 AccountId (com.hedera.hashgraph.sdk.AccountId)15 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)12 AccountInfoQuery (com.hedera.hashgraph.sdk.AccountInfoQuery)11 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)11 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)11 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)9 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)9