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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations