use of com.hedera.hashgraph.sdk.AccountDeleteTransaction 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);
}
use of com.hedera.hashgraph.sdk.AccountDeleteTransaction in project hedera-sdk-java by hashgraph.
the class TransferTokensExample 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);
// Generate a Ed25519 private, public key pair
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
System.out.println("private key = " + key1);
System.out.println("public key = " + key1.getPublicKey());
System.out.println("private key = " + key2);
System.out.println("public key = " + key2.getPublicKey());
@Var TransactionResponse response = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
@Var TransactionReceipt receipt = response.getReceipt(client);
AccountId accountId1 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId1 = " + accountId1);
response = new AccountCreateTransaction().setKey(key2.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
receipt = response.getReceipt(client);
AccountId accountId2 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId2 = " + accountId1);
response = new TokenCreateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(OPERATOR_ID).setAdminKey(OPERATOR_KEY.getPublicKey()).setFreezeKey(OPERATOR_KEY.getPublicKey()).setWipeKey(OPERATOR_KEY.getPublicKey()).setKycKey(OPERATOR_KEY.getPublicKey()).setSupplyKey(OPERATOR_KEY.getPublicKey()).setFreezeDefault(false).execute(client);
TokenId tokenId = Objects.requireNonNull(response.getReceipt(client).tokenId);
System.out.println("token = " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId1 + " with token " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId2 + " with token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId1 + " on token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, OPERATOR_ID, -10).addTokenTransfer(tokenId, accountId1, 10).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + OPERATOR_ID + " to account " + accountId1 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId1, -10).addTokenTransfer(tokenId, accountId2, 10).freezeWith(client).sign(key1).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId1 + " to account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId2, -10).addTokenTransfer(tokenId, accountId1, 10).freezeWith(client).sign(key2).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId2 + " to account " + accountId1 + " on token " + tokenId);
new TokenWipeTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).setAccountId(accountId1).setAmount(10).execute(client).getReceipt(client);
System.out.println("Wiped balance of account " + accountId1);
new TokenDeleteTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Deleted token " + tokenId);
new AccountDeleteTransaction().setAccountId(accountId1).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Deleted accountId1 " + accountId1);
new AccountDeleteTransaction().setAccountId(accountId2).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Deleted accountId2" + accountId2);
}
use of com.hedera.hashgraph.sdk.AccountDeleteTransaction 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();
}
use of com.hedera.hashgraph.sdk.AccountDeleteTransaction in project hedera-sdk-java by hashgraph.
the class DeleteAccountExample 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);
// Generate a Ed25519 private, public key pair
PrivateKey newKey = PrivateKey.generateED25519();
PublicKey newPublicKey = newKey.getPublicKey();
System.out.println("private key = " + newKey);
System.out.println("public key = " + newPublicKey);
TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(2)).execute(client);
// This will wait for the receipt to become available
TransactionReceipt receipt = transactionResponse.getReceipt(client);
AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
System.out.println("account = " + newAccountId);
new AccountDeleteTransaction().setTransactionId(TransactionId.generate(newAccountId)).setAccountId(newAccountId).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(newKey).execute(client).getReceipt(client);
}
use of com.hedera.hashgraph.sdk.AccountDeleteTransaction 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);
}
Aggregations