Search in sources :

Example 1 with TransactionRecord

use of com.hedera.hashgraph.sdk.TransactionRecord 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)

Example 2 with TransactionRecord

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

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

the class TopicClient method publishMessageToTopic.

public TransactionId publishMessageToTopic(TopicId topicId, byte[] message, KeyList submitKeys) {
    TopicMessageSubmitTransaction consensusMessageSubmitTransaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).setTransactionMemo(getMemo("Publish topic message"));
    TransactionId transactionId = executeTransaction(consensusMessageSubmitTransaction, submitKeys);
    TransactionRecord transactionRecord = getTransactionRecord(transactionId);
    // get only the 1st sequence number
    if (recordPublishInstants.size() == 0) {
        recordPublishInstants.put(0L, transactionRecord.consensusTimestamp);
    }
    if (log.isTraceEnabled()) {
        log.trace("Published message : '{}' to topicId : {} with consensusTimestamp: {}", new String(message, StandardCharsets.UTF_8), topicId, transactionRecord.consensusTimestamp);
    }
    return transactionId;
}
Also used : TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Example 4 with TransactionRecord

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

the class ContractClient method createContract.

public NetworkTransactionResponse createContract(FileId fileId, long gas, Hbar payableAmount, ContractFunctionParameters contractFunctionParameters) {
    log.debug("Create new contract");
    String memo = getMemo("Create contract");
    ContractCreateTransaction contractCreateTransaction = new ContractCreateTransaction().setAdminKey(sdkClient.getExpandedOperatorAccountId().getPublicKey()).setBytecodeFileId(fileId).setContractMemo(memo).setGas(gas).setTransactionMemo(memo);
    if (contractFunctionParameters != null) {
        contractCreateTransaction.setConstructorParameters(contractFunctionParameters);
    }
    if (payableAmount != null) {
        contractCreateTransaction.setInitialBalance(payableAmount);
    }
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(contractCreateTransaction);
    ContractId contractId = networkTransactionResponse.getReceipt().contractId;
    log.debug("Created new contract {}", contractId);
    TransactionRecord transactionRecord = getTransactionRecord(networkTransactionResponse.getTransactionId());
    logContractFunctionResult("constructor", transactionRecord.contractFunctionResult);
    return networkTransactionResponse;
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) ContractId(com.hedera.hashgraph.sdk.ContractId) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 5 with TransactionRecord

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

the class ContractClient method executeContract.

public NetworkTransactionResponse executeContract(ContractId contractId, long gas, String functionName, ContractFunctionParameters parameters, Hbar payableAmount) {
    log.debug("Call contract {}'s function {}", contractId, functionName);
    ContractExecuteTransaction contractExecuteTransaction = new ContractExecuteTransaction().setContractId(contractId).setGas(gas).setTransactionMemo(getMemo("Execute contract")).setMaxTransactionFee(Hbar.from(100));
    if (parameters == null) {
        contractExecuteTransaction.setFunction(functionName);
    } else {
        contractExecuteTransaction.setFunction(functionName, parameters);
    }
    if (payableAmount != null) {
        contractExecuteTransaction.setPayableAmount(payableAmount);
    }
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(contractExecuteTransaction);
    TransactionRecord transactionRecord = getTransactionRecord(networkTransactionResponse.getTransactionId());
    logContractFunctionResult(functionName, transactionRecord.contractFunctionResult);
    return networkTransactionResponse;
}
Also used : ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Aggregations

TransactionRecord (com.hedera.hashgraph.sdk.TransactionRecord)6 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)3 AccountId (com.hedera.hashgraph.sdk.AccountId)3 Client (com.hedera.hashgraph.sdk.Client)3 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)3 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)2 Hbar (com.hedera.hashgraph.sdk.Hbar)2 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)2 TransactionId (com.hedera.hashgraph.sdk.TransactionId)2 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)2 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)2 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)1 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)1 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)1 ContractExecuteTransaction (com.hedera.hashgraph.sdk.ContractExecuteTransaction)1 ContractId (com.hedera.hashgraph.sdk.ContractId)1 CustomFee (com.hedera.hashgraph.sdk.CustomFee)1 CustomFixedFee (com.hedera.hashgraph.sdk.CustomFixedFee)1 CustomFractionalFee (com.hedera.hashgraph.sdk.CustomFractionalFee)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1