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);
}
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);
}
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;
}
Aggregations