use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class TokenAssociateIntegrationTest method canAssociateAccountWithToken.
@Test
@DisplayName("Can associate account with token")
void canAssociateAccountWithToken() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
var tokenId = Objects.requireNonNull(new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setKycKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client).getReceipt(testEnv.client).tokenId);
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class TokenBurnIntegrationTest method cannotBurnNftsWhenNftIsNotOwned.
@Test
@DisplayName("Cannot burn NFTs when NFT is not owned by treasury")
void cannotBurnNftsWhenNftIsNotOwned() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var createReceipt = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTokenType(TokenType.NON_FUNGIBLE_UNIQUE).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client).getReceipt(testEnv.client);
var tokenId = Objects.requireNonNull(createReceipt.tokenId);
var serials = new TokenMintTransaction().setTokenId(tokenId).setMetadata(NftMetadataGenerator.generate((byte) 1)).execute(testEnv.client).getReceipt(testEnv.client).serials;
var key = PrivateKey.generateED25519();
var accountId = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
new TransferTransaction().addNftTransfer(tokenId.nft(serials.get(0)), testEnv.operatorId, accountId).execute(testEnv.client).getReceipt(testEnv.client);
assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
new TokenBurnTransaction().setSerials(serials).setTokenId(tokenId).execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.TREASURY_MUST_OWN_BURNED_NFT.toString());
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class TokenCreateIntegrationTest method canCreateTokenWithMinimalPropertiesSet.
@Test
@DisplayName("Can create token with minimal properties set")
@SuppressWarnings("UnusedVariable")
void canCreateTokenWithMinimalPropertiesSet() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount(new Hbar(10));
var tokenId = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTreasuryAccountId(testEnv.operatorId).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
testEnv.close();
}
use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class TokenCreateIntegrationTest method canCreateRoyaltyFee.
@Test
@DisplayName("Can create NFT with royalty fee")
void canCreateRoyaltyFee() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var tokenId = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTreasuryAccountId(testEnv.operatorId).setSupplyKey(testEnv.operatorKey).setAdminKey(testEnv.operatorKey).setTokenType(TokenType.NON_FUNGIBLE_UNIQUE).setCustomFees(Collections.singletonList(new CustomRoyaltyFee().setNumerator(1).setDenominator(10).setFallbackFee(new CustomFixedFee().setHbarAmount(new Hbar(1))).setFeeCollectorAccountId(testEnv.operatorId))).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
testEnv.close(tokenId);
}
use of com.hedera.hashgraph.sdk.Hbar in project hedera-sdk-java by hashgraph.
the class ScheduledTransferExample 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);
Objects.requireNonNull(client.getOperatorAccountId());
/*
* A scheduled transaction is a transaction that has been proposed by an account,
* but which requires more signatures before it will actually execute on the Hedera network.
*
* For example, if Alice wants to transfer an amount of Hbar to Bob, and Bob has
* receiverSignatureRequired set to true, then that transaction must be signed by
* both Alice and Bob before the transaction will be executed.
*
* To solve this problem, Alice can propose the transaction by creating a scheduled
* transaction on the Hedera network which, if executed, would transfer Hbar from
* Alice to Bob. That scheduled transaction will have a ScheduleId by which we can
* refer to that scheduled transaction. Alice can communicate the ScheduleId to Bob, and
* then Bob can use a ScheduleSignTransaction to sign that scheduled transaction.
*
* Bob has a 30 minute window in which to sign the scheduled transaction, starting at the
* moment that Alice creates the scheduled transaction. If a scheduled transaction
* is not signed by all of the necessary signatories within the 30 minute window,
* that scheduled transaction will expire, and will not be executed.
*
* Once a scheduled transaction has all of the signatures necessary to execute, it will
* be executed on the Hedera network automatically. If you create a scheduled transaction
* on the Hedera network, but that transaction only requires your signature in order to
* execute and no one else's, that scheduled transaction will be automatically
* executed immediately.
*/
PrivateKey bobsKey = PrivateKey.generateED25519();
AccountId bobsId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(bobsKey).setInitialBalance(new Hbar(10)).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client).accountId;
Objects.requireNonNull(bobsId);
System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client));
System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client));
AccountBalance bobsInitialBalance = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's initial balance:");
System.out.println(bobsInitialBalance);
TransferTransaction transferToSchedule = new TransferTransaction().addHbarTransfer(client.getOperatorAccountId(), new Hbar(-10)).addHbarTransfer(bobsId, new Hbar(10));
System.out.println("Transfer to be scheduled:");
System.out.println(transferToSchedule);
/*
* The payerAccountId is the account that will be charged the fee
* for executing the scheduled transaction if/when it is executed.
* That fee is separate from the fee that we will pay to execute the
* ScheduleCreateTransaction itself.
*
* To clarify: Alice pays a fee to execute the ScheduleCreateTransaction,
* which creates the scheduled transaction on the Hedera network.
* She specifies when creating the scheduled transaction that Bob will pay
* the fee for the scheduled transaction when it is executed.
*
* If payerAccountId is not specified, the account who creates the scheduled transaction
* will be charged for executing the scheduled transaction.
*/
ScheduleId scheduleId = new ScheduleCreateTransaction().setScheduledTransaction(transferToSchedule).setPayerAccountId(bobsId).execute(client).getReceipt(client).scheduleId;
Objects.requireNonNull(scheduleId);
System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client));
/*
* Bob's balance should be unchanged. The transfer has been scheduled, but it hasn't been executed yet
* because it requires Bob's signature.
*/
AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's balance after scheduling the transfer (should be unchanged):");
System.out.println(bobsBalanceAfterSchedule);
/*
* Once Alice has communicated the scheduleId to Bob, Bob can query for information about the
* scheduled transaction.
*/
ScheduleInfo scheduledTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
System.out.println("Info about scheduled transaction:");
System.out.println(scheduledTransactionInfo);
/*
* getScheduledTransaction() will return an SDK Transaction object identical to the transaction
* that was scheduled, which Bob can then inspect like a normal transaction.
*/
Transaction<?> scheduledTransaction = scheduledTransactionInfo.getScheduledTransaction();
// We happen to know that this transaction is (or certainly ought to be) a TransferTransaction
if (scheduledTransaction instanceof TransferTransaction) {
TransferTransaction scheduledTransfer = (TransferTransaction) scheduledTransaction;
System.out.println("The scheduled transfer transaction from Bob's POV:");
System.out.println(scheduledTransfer);
} else {
System.out.println("The scheduled transaction was not a transfer transaction.");
System.out.println("Something has gone horribly wrong. Crashing...");
System.exit(-1);
}
new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
AccountBalance balanceAfterSigning = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
System.out.println("Bob's balance after signing the scheduled transaction:");
System.out.println(balanceAfterSigning);
ScheduleInfo postTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
System.out.println("Info on the scheduled transaction, executedAt should no longer be null:");
System.out.println(postTransactionInfo);
// Clean up
new AccountDeleteTransaction().setTransferAccountId(client.getOperatorAccountId()).setAccountId(bobsId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
client.close();
}
Aggregations