Search in sources :

Example 31 with Hbar

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

the class ContractCallIntegrationTest method canCallContractFunction.

@Test
@DisplayName("Can call contract function")
void canCallContractFunction() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    @Var var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents(SMART_CONTRACT_BYTECODE).execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    response = new ContractCreateTransaction().setAdminKey(testEnv.operatorKey).setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client);
    var contractId = Objects.requireNonNull(response.getReceipt(testEnv.client).contractId);
    var callQuery = new ContractCallQuery().setContractId(contractId).setGas(100000).setFunction("getMessage").setQueryPayment(new Hbar(1));
    var result = callQuery.execute(testEnv.client);
    assertThat(result.getString(0)).isEqualTo("Hello from Hedera.");
    new ContractDeleteTransaction().setTransferAccountId(testEnv.operatorId).setContractId(contractId).execute(testEnv.client).getReceipt(testEnv.client);
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) Var(com.google.errorprone.annotations.Var) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) ContractCallQuery(com.hedera.hashgraph.sdk.ContractCallQuery) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 32 with Hbar

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

the class ContractInfoIntegrationTest method getCostBigMaxContractInfoFunction.

@Test
@DisplayName("Can get cost, even with a big max")
@SuppressWarnings("UnusedVariable")
void getCostBigMaxContractInfoFunction() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    @Var var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents(SMART_CONTRACT_BYTECODE).execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    response = new ContractCreateTransaction().setAdminKey(testEnv.operatorKey).setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client);
    var contractId = Objects.requireNonNull(response.getReceipt(testEnv.client).contractId);
    var infoQuery = new ContractInfoQuery().setContractId(contractId).setMaxQueryPayment(new Hbar(10000));
    var cost = infoQuery.getCost(testEnv.client);
    var result = infoQuery.execute(testEnv.client);
    new ContractDeleteTransaction().setTransferAccountId(testEnv.operatorId).setContractId(contractId).execute(testEnv.client).getReceipt(testEnv.client);
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) Var(com.google.errorprone.annotations.Var) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ContractInfoQuery(com.hedera.hashgraph.sdk.ContractInfoQuery) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 33 with Hbar

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

the class FileContentsIntegrationTest method getCostBigMaxQueryFileContents.

@Test
@DisplayName("Can get cost, even with a big max")
void getCostBigMaxQueryFileContents() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents("[e2e::FileCreateTransaction]").execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    var contentsQuery = new FileContentsQuery().setFileId(fileId).setMaxQueryPayment(new Hbar(1000));
    var contents = contentsQuery.execute(testEnv.client);
    assertThat(contents.toStringUtf8()).isEqualTo("[e2e::FileCreateTransaction]");
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 34 with Hbar

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

the class FileContentsIntegrationTest method getCostInsufficientTxFeeQueryFileContents.

@Test
@DisplayName("Insufficient tx fee error.")
void getCostInsufficientTxFeeQueryFileContents() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents("[e2e::FileCreateTransaction]").execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    var contentsQuery = new FileContentsQuery().setFileId(fileId).setMaxQueryPayment(new Hbar(100));
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        contentsQuery.setQueryPayment(Hbar.fromTinybars(1)).execute(testEnv.client);
    }).satisfies(error -> assertThat(error.status.toString()).isEqualTo("INSUFFICIENT_TX_FEE"));
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 35 with Hbar

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

Aggregations

Hbar (com.hedera.hashgraph.sdk.Hbar)106 Test (org.junit.jupiter.api.Test)77 DisplayName (org.junit.jupiter.api.DisplayName)75 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)62 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)34 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)31 Client (com.hedera.hashgraph.sdk.Client)22 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)22 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)19 Var (com.google.errorprone.annotations.Var)18 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)16 AccountId (com.hedera.hashgraph.sdk.AccountId)15 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)12 AccountInfoQuery (com.hedera.hashgraph.sdk.AccountInfoQuery)11 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)11 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)11 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)9 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)9