Search in sources :

Example 1 with AccountAllowanceApproveTransaction

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

the class AccountAllowanceExample method demonstrateAllowances.

private void demonstrateAllowances() throws PrecheckStatusException, TimeoutException, ReceiptStatusException {
    System.out.println("Approving an allowance of 2 Hbar with owner Alice and spender Bob");
    new AccountAllowanceApproveTransaction().approveHbarAllowance(aliceId, bobId, Hbar.from(2)).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    printBalances();
    System.out.println("Transferring 1 Hbar from Alice to Charlie, but the transaction is signed _only_ by Bob (Bob is dipping into his allowance from Alice)");
    new TransferTransaction().addApprovedHbarTransfer(aliceId, Hbar.from(1).negated()).addHbarTransfer(charlieId, Hbar.from(1)).setTransactionId(TransactionId.generate(bobId)).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
    System.out.println("Transfer succeeded.  Bob should now have 1 Hbar left in his allowance.");
    printBalances();
    try {
        System.out.println("Attempting to transfer 2 Hbar from Alice to Charlie using Bob's allowance.");
        System.out.println("This should fail, because there is only 1 Hbar left in Bob's allowance.");
        new TransferTransaction().addApprovedHbarTransfer(aliceId, Hbar.from(2).negated()).addHbarTransfer(charlieId, Hbar.from(2)).setTransactionId(TransactionId.generate(bobId)).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
        System.out.println("The transfer succeeded.  This should not happen.");
    } catch (Throwable error) {
        System.out.println("The transfer failed as expected.");
        System.out.println(error.getMessage());
    }
    System.out.println("Adjusting Bob's allowance to 3 Hbar.");
    new AccountAllowanceApproveTransaction().approveHbarAllowance(aliceId, bobId, Hbar.from(3)).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    System.out.println("Attempting to transfer 2 Hbar from Alice to Charlie using Bob's allowance again.");
    System.out.println("This time it should succeed.");
    new TransferTransaction().addApprovedHbarTransfer(aliceId, Hbar.from(2).negated()).addHbarTransfer(charlieId, Hbar.from(2)).setTransactionId(TransactionId.generate(bobId)).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
    System.out.println("Transfer succeeded.");
    printBalances();
    System.out.println("Deleting Bob's allowance");
    new AccountAllowanceApproveTransaction().approveHbarAllowance(aliceId, bobId, Hbar.ZERO).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
}
Also used : TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountAllowanceApproveTransaction(com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction)

Example 2 with AccountAllowanceApproveTransaction

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

the class AccountAllowanceIntegrationTest method canSpendHbarAllowance.

@Test
@DisplayName("Can spend hbar allowance")
void canSpendHbarAllowance() throws Throwable {
    var testEnv = new IntegrationTestEnv(1);
    var aliceKey = PrivateKey.generateED25519();
    var aliceId = new AccountCreateTransaction().setKey(aliceKey).setInitialBalance(new Hbar(10)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    var bobKey = PrivateKey.generateED25519();
    var bobId = new AccountCreateTransaction().setKey(bobKey).setInitialBalance(new Hbar(10)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    Objects.requireNonNull(aliceId);
    Objects.requireNonNull(bobId);
    new AccountAllowanceApproveTransaction().approveHbarAllowance(bobId, aliceId, new Hbar(10)).freezeWith(testEnv.client).sign(bobKey).execute(testEnv.client).getReceipt(testEnv.client);
    var transferRecord = new TransferTransaction().addHbarTransfer(testEnv.operatorId, new Hbar(5)).addApprovedHbarTransfer(bobId, new Hbar(5).negated()).setTransactionId(TransactionId.generate(aliceId)).freezeWith(testEnv.client).sign(aliceKey).execute(testEnv.client).getRecord(testEnv.client);
    var transferFound = false;
    for (var transfer : transferRecord.transfers) {
        if (transfer.accountId.equals(testEnv.operatorId) && transfer.amount.equals(new Hbar(5))) {
            transferFound = true;
            break;
        }
    }
    assertThat(transferFound).isTrue();
    new AccountDeleteTransaction().setAccountId(bobId).setTransferAccountId(testEnv.operatorId).freezeWith(testEnv.client).sign(bobKey).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(aliceId, aliceKey);
}
Also used : AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) AccountAllowanceApproveTransaction(com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with AccountAllowanceApproveTransaction

use of com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction in project hedera-mirror-node by hashgraph.

the class AccountClient method approveCryptoAllowance.

public NetworkTransactionResponse approveCryptoAllowance(AccountId spender, Hbar hbarAmount) {
    var ownerAccountId = sdkClient.getExpandedOperatorAccountId().getAccountId();
    log.debug("Approve spender {} an allowance of {} tℏ on {}'s account", spender, hbarAmount.toTinybars(), ownerAccountId);
    var transaction = new AccountAllowanceApproveTransaction().approveHbarAllowance(null, spender, hbarAmount);
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(transaction);
    log.debug("Sent Account Allowance Approval");
    return networkTransactionResponse;
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) AccountAllowanceApproveTransaction(com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction)

Aggregations

AccountAllowanceApproveTransaction (com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction)3 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)2 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)1 Hbar (com.hedera.hashgraph.sdk.Hbar)1 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1