Search in sources :

Example 11 with TransferTransaction

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

the class CustomFeesExample 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);
    // Create three accounts, Alice, Bob, and Charlie.  Alice will be the treasury for our example token.
    // Fees only apply in transactions not involving the treasury, so we need two other accounts.
    PrivateKey aliceKey = PrivateKey.generateED25519();
    AccountId aliceId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(aliceKey).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client).accountId;
    PrivateKey bobKey = PrivateKey.generateED25519();
    AccountId bobId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(bobKey).freezeWith(client).sign(bobKey).execute(client).getReceipt(client).accountId;
    PrivateKey charlieKey = PrivateKey.generateED25519();
    AccountId charlieId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(charlieKey).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client).accountId;
    System.out.println("Alice: " + aliceId);
    System.out.println("Bob: " + bobId);
    System.out.println("Charlie: " + charlieId);
    // Let's start with a custom fee list of 1 fixed fee.  A custom fee list can be a list of up to
    // 10 custom fees, where each fee is a fixed fee or a fractional fee.
    // This fixed fee will mean that every time Bob transfers any number of tokens to Charlie,
    // Alice will collect 1 Hbar from each account involved in the transaction who is SENDING
    // the Token (in this case, Bob).
    CustomFixedFee customHbarFee = new CustomFixedFee().setHbarAmount(new Hbar(1)).setFeeCollectorAccountId(aliceId);
    List<CustomFee> hbarFeeList = Collections.singletonList(customHbarFee);
    // In this example the fee is in Hbar, but you can charge a fixed fee in a token if you'd like.
    // EG, you can make it so that each time an account transfers Foo tokens,
    // they must pay a fee in Bar tokens to the fee collecting account.
    // To charge a fixed fee in tokens, instead of calling setHbarAmount(), call
    // setDenominatingTokenId(tokenForFee) and setAmount(tokenFeeAmount).
    // Setting the feeScheduleKey to Alice's key will enable Alice to change the custom
    // fees list on this token later using the TokenFeeScheduleUpdateTransaction.
    // We will create an initial supply of 100 of these tokens.
    TokenId tokenId = new TokenCreateTransaction().setTokenName("Example Token").setTokenSymbol("EX").setAdminKey(aliceKey).setSupplyKey(aliceKey).setFeeScheduleKey(aliceKey).setTreasuryAccountId(aliceId).setCustomFees(hbarFeeList).setInitialSupply(100).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client).tokenId;
    System.out.println("Token: " + tokenId);
    TokenInfo tokenInfo1 = new TokenInfoQuery().setTokenId(tokenId).execute(client);
    System.out.println("Custom Fees according to TokenInfoQuery:");
    System.out.println(tokenInfo1.customFees);
    // We must associate the token with Bob and Charlie before they can trade in it.
    new TokenAssociateTransaction().setAccountId(bobId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
    new TokenAssociateTransaction().setAccountId(charlieId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client);
    // give all 100 tokens to Bob
    new TransferTransaction().addTokenTransfer(tokenId, bobId, 100).addTokenTransfer(tokenId, aliceId, -100).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    Hbar aliceHbar1 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars;
    System.out.println("Alice's Hbar balance before Bob transfers 20 tokens to Charlie: " + aliceHbar1);
    TransactionRecord record1 = new TransferTransaction().addTokenTransfer(tokenId, bobId, -20).addTokenTransfer(tokenId, charlieId, 20).freezeWith(client).sign(bobKey).execute(client).getRecord(client);
    Hbar aliceHbar2 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars;
    System.out.println("Alices's Hbar balance after Bob transfers 20 tokens to Charlie: " + aliceHbar2);
    System.out.println("Assessed fees according to transaction record:");
    System.out.println(record1.assessedCustomFees);
    // Let's use the TokenUpdateFeeScheduleTransaction with Alice's key to change the custom fees on our token.
    // TokenUpdateFeeScheduleTransaction will replace the list of fees that apply to the token with
    // an entirely new list.  Let's charge a 10% fractional fee.  This means that when Bob attempts to transfer
    // 20 tokens to Charlie, 10% of the tokens he attempts to transfer (2 in this case) will be transferred to
    // Alice instead.
    // Fractional fees default to FeeAssessmentMethod.INCLUSIVE, which is the behavior described above.
    // If you set the assessment method to EXCLUSIVE, then when Bob attempts to transfer 20 tokens to Charlie,
    // Charlie will receive all 20 tokens, and Bob will be charged an _additional_ 10% fee which
    // will be transferred to Alice.
    CustomFractionalFee customFractionalFee = new CustomFractionalFee().setNumerator(1).setDenominator(10).setMin(1).setMax(10).setFeeCollectorAccountId(aliceId);
    List<CustomFee> fractionalFeeList = Collections.singletonList(customFractionalFee);
    new TokenFeeScheduleUpdateTransaction().setTokenId(tokenId).setCustomFees(fractionalFeeList).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    TokenInfo tokenInfo2 = new TokenInfoQuery().setTokenId(tokenId).execute(client);
    System.out.println("Custom Fees according to TokenInfoQuery:");
    System.out.println(tokenInfo2.customFees);
    Map<TokenId, Long> aliceTokens3 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).tokens;
    System.out.println("Alice's token balance before Bob transfers 20 tokens to Charlie: " + aliceTokens3);
    TransactionRecord record2 = new TransferTransaction().addTokenTransfer(tokenId, bobId, -20).addTokenTransfer(tokenId, charlieId, 20).freezeWith(client).sign(bobKey).execute(client).getRecord(client);
    Map<TokenId, Long> aliceTokens4 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).tokens;
    System.out.println("Alices's token balance after Bob transfers 20 tokens to Charlie: " + aliceTokens4);
    System.out.println("Token transfers according to transaction record:");
    System.out.println(record2.tokenTransfers);
    System.out.println("Assessed fees according to transaction record:");
    System.out.println(record2.assessedCustomFees);
    // clean up
    new TokenDeleteTransaction().setTokenId(tokenId).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    new AccountDeleteTransaction().setAccountId(charlieId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client);
    new AccountDeleteTransaction().setAccountId(bobId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
    new AccountDeleteTransaction().setAccountId(aliceId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
    client.close();
}
Also used : CustomFractionalFee(com.hedera.hashgraph.sdk.CustomFractionalFee) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) TokenDeleteTransaction(com.hedera.hashgraph.sdk.TokenDeleteTransaction) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TokenFeeScheduleUpdateTransaction(com.hedera.hashgraph.sdk.TokenFeeScheduleUpdateTransaction) TokenInfoQuery(com.hedera.hashgraph.sdk.TokenInfoQuery) CustomFee(com.hedera.hashgraph.sdk.CustomFee) CustomFixedFee(com.hedera.hashgraph.sdk.CustomFixedFee) TokenInfo(com.hedera.hashgraph.sdk.TokenInfo) Client(com.hedera.hashgraph.sdk.Client) TokenId(com.hedera.hashgraph.sdk.TokenId) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 12 with TransferTransaction

use of com.hedera.hashgraph.sdk.TransferTransaction 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 13 with TransferTransaction

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

the class MultiSigOfflineExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException {
    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();
    System.out.println("private key for user 1 = " + user1Key);
    System.out.println("public key for user 1 = " + user1Key.getPublicKey());
    System.out.println("private key for user 2 = " + user2Key);
    System.out.println("public key for user 2 = " + user2Key.getPublicKey());
    // create a multi-sig account
    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);
    // create a transfer from new account to 0.0.3
    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);
    // convert transaction to bytes to send to signatories
    byte[] transactionBytes = transferTransaction.toBytes();
    Transaction<?> transactionToExecute = Transaction.fromBytes(transactionBytes);
    // ask users to sign and return signature
    byte[] user1Signature = user1Key.signTransaction(Transaction.fromBytes(transactionBytes));
    byte[] user2Signature = user2Key.signTransaction(Transaction.fromBytes(transactionBytes));
    // recreate the transaction from bytes
    transactionToExecute.signWithOperator(client);
    transactionToExecute.addSignature(user1Key.getPublicKey(), user1Signature);
    transactionToExecute.addSignature(user2Key.getPublicKey(), user2Signature);
    TransactionResponse result = transactionToExecute.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 14 with TransferTransaction

use of com.hedera.hashgraph.sdk.TransferTransaction 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 15 with TransferTransaction

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

the class AccountCreateIntegrationTest method canCreateWithAliasKey.

@Test
@DisplayName("Can create account using aliasKey")
void canCreateWithAliasKey() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var key = PrivateKey.generateED25519();
    var aliasId = key.toAccountId(0, 0);
    new TransferTransaction().addHbarTransfer(testEnv.operatorId, new Hbar(10).negated()).addHbarTransfer(aliasId, new Hbar(10)).execute(testEnv.client).getReceipt(testEnv.client);
    var info = new AccountInfoQuery().setAccountId(aliasId).execute(testEnv.client);
    assertThat(key.getPublicKey()).isEqualTo(info.aliasKey);
    testEnv.close(info.accountId, key);
}
Also used : AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)42 Hbar (com.hedera.hashgraph.sdk.Hbar)31 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)26 Test (org.junit.jupiter.api.Test)23 DisplayName (org.junit.jupiter.api.DisplayName)18 AccountId (com.hedera.hashgraph.sdk.AccountId)15 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)14 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)13 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)13 Client (com.hedera.hashgraph.sdk.Client)12 Var (com.google.errorprone.annotations.Var)10 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)10 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)8 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)8 KeyList (com.hedera.hashgraph.sdk.KeyList)7 TokenWipeTransaction (com.hedera.hashgraph.sdk.TokenWipeTransaction)7 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)6 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)6 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)6