Skip to content

Commit

Permalink
feat(TCK): Implement Account Update/Delete (#1988)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov authored Sep 17, 2024
1 parent 13927b8 commit 6af082f
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
import com.hedera.hashgraph.sdk.AccountDeleteTransaction;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.AccountUpdateTransaction;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.HbarUnit;
import com.hedera.hashgraph.sdk.Status;
Expand All @@ -30,9 +32,12 @@
import com.hedera.hashgraph.tck.annotation.JSONRPC2Service;
import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service;
import com.hedera.hashgraph.tck.methods.sdk.param.AccountCreateParams;
import com.hedera.hashgraph.tck.methods.sdk.response.AccountCreateResponse;
import com.hedera.hashgraph.tck.methods.sdk.param.AccountDeleteParams;
import com.hedera.hashgraph.tck.methods.sdk.param.AccountUpdateParams;
import com.hedera.hashgraph.tck.methods.sdk.response.AccountResponse;
import com.hedera.hashgraph.tck.util.KeyUtils;
import java.time.Duration;
import java.time.Instant;

/**
* AccountCreateService for account related methods
Expand All @@ -46,59 +51,123 @@ public AccountService(SdkService sdkService) {
}

@JSONRPC2Method("createAccount")
public AccountCreateResponse createAccount(final AccountCreateParams params) throws Exception {
try {
AccountCreateTransaction accountCreateTransaction = new AccountCreateTransaction();
params.getKey().ifPresent(key -> {
try {
accountCreateTransaction.setKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});
public AccountResponse createAccount(final AccountCreateParams params) throws Exception {
AccountCreateTransaction accountCreateTransaction = new AccountCreateTransaction();
params.getKey().ifPresent(key -> {
try {
accountCreateTransaction.setKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getInitialBalance()
.ifPresent(initialBalanceTinybars -> accountCreateTransaction.setInitialBalance(
Hbar.from(initialBalanceTinybars, HbarUnit.TINYBAR)));

params.getReceiverSignatureRequired().ifPresent(accountCreateTransaction::setReceiverSignatureRequired);

params.getInitialBalance()
.ifPresent(initialBalanceTinybars -> accountCreateTransaction.setInitialBalance(
Hbar.from(initialBalanceTinybars, HbarUnit.TINYBAR)));
params.getAutoRenewPeriod()
.ifPresent(autoRenewPeriodSeconds ->
accountCreateTransaction.setAutoRenewPeriod(Duration.ofSeconds(autoRenewPeriodSeconds)));

params.getReceiverSignatureRequired().ifPresent(accountCreateTransaction::setReceiverSignatureRequired);
params.getMemo().ifPresent(accountCreateTransaction::setAccountMemo);

params.getAutoRenewPeriod()
.ifPresent(autoRenewPeriodSeconds ->
accountCreateTransaction.setAutoRenewPeriod(Duration.ofSeconds(autoRenewPeriodSeconds)));
params.getMaxAutoTokenAssociations()
.ifPresent(autoAssociations ->
accountCreateTransaction.setMaxAutomaticTokenAssociations(autoAssociations.intValue()));

params.getMemo().ifPresent(accountCreateTransaction::setAccountMemo);
params.getStakedAccountId()
.ifPresent(stakedAccountId ->
accountCreateTransaction.setStakedAccountId(AccountId.fromString(stakedAccountId)));

params.getMaxAutoTokenAssociations()
.ifPresent(autoAssociations ->
accountCreateTransaction.setMaxAutomaticTokenAssociations(autoAssociations.intValue()));
params.getStakedNodeId().ifPresent(accountCreateTransaction::setStakedNodeId);

params.getStakedAccountId()
.ifPresent(stakedAccountId ->
accountCreateTransaction.setStakedAccountId(AccountId.fromString(stakedAccountId)));
params.getDeclineStakingReward().ifPresent(accountCreateTransaction::setDeclineStakingReward);

params.getStakedNodeId().ifPresent(accountCreateTransaction::setStakedNodeId);
params.getAlias().ifPresent(accountCreateTransaction::setAlias);

params.getDeclineStakingReward().ifPresent(accountCreateTransaction::setDeclineStakingReward);
params.getCommonTransactionParams()
.ifPresent(commonTransactionParams ->
commonTransactionParams.fillOutTransaction(accountCreateTransaction, sdkService.getClient()));

TransactionReceipt transactionReceipt =
accountCreateTransaction.execute(sdkService.getClient()).getReceipt(sdkService.getClient());

String stringAccountId = "";
if (transactionReceipt.status == Status.SUCCESS) {
stringAccountId = transactionReceipt.accountId.toString();
}

params.getAlias().ifPresent(accountCreateTransaction::setAlias);
return new AccountResponse(stringAccountId, transactionReceipt.status);
}

params.getCommonTransactionParams()
.ifPresent(commonTransactionParams -> commonTransactionParams.fillOutTransaction(
accountCreateTransaction, sdkService.getClient()));
@JSONRPC2Method("updateAccount")
public AccountResponse updateAccount(final AccountUpdateParams params) throws Exception {
AccountUpdateTransaction accountUpdateTransaction = new AccountUpdateTransaction();

TransactionReceipt transactionReceipt =
accountCreateTransaction.execute(sdkService.getClient()).getReceipt(sdkService.getClient());
params.getAccountId()
.ifPresent(accountId -> accountUpdateTransaction.setAccountId(AccountId.fromString(accountId)));

String stringAccountId = "";
if (transactionReceipt.status == Status.SUCCESS) {
stringAccountId = transactionReceipt.accountId.toString();
params.getKey().ifPresent(key -> {
try {
accountUpdateTransaction.setKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

return new AccountCreateResponse(stringAccountId, transactionReceipt.status);
params.getReceiverSignatureRequired().ifPresent(accountUpdateTransaction::setReceiverSignatureRequired);

} finally {
sdkService.getClient().close();
}
params.getAutoRenewPeriod()
.ifPresent(autoRenewPeriodSeconds ->
accountUpdateTransaction.setAutoRenewPeriod(Duration.ofSeconds(autoRenewPeriodSeconds)));

params.getMemo().ifPresent(accountUpdateTransaction::setAccountMemo);

params.getExpirationTime()
.ifPresent(expirationTime ->
accountUpdateTransaction.setExpirationTime(Instant.ofEpochSecond(expirationTime)));

params.getMaxAutoTokenAssociations()
.ifPresent(autoAssociations ->
accountUpdateTransaction.setMaxAutomaticTokenAssociations(autoAssociations.intValue()));

params.getStakedAccountId()
.ifPresent(stakedAccountId ->
accountUpdateTransaction.setStakedAccountId(AccountId.fromString(stakedAccountId)));

params.getStakedNodeId().ifPresent(accountUpdateTransaction::setStakedNodeId);

params.getDeclineStakingReward().ifPresent(accountUpdateTransaction::setDeclineStakingReward);

params.getCommonTransactionParams()
.ifPresent(commonTransactionParams ->
commonTransactionParams.fillOutTransaction(accountUpdateTransaction, sdkService.getClient()));

TransactionReceipt transactionReceipt =
accountUpdateTransaction.execute(sdkService.getClient()).getReceipt(sdkService.getClient());

return new AccountResponse(null, transactionReceipt.status);
}

@JSONRPC2Method("deleteAccount")
public AccountResponse deleteAccount(final AccountDeleteParams params) throws Exception {
AccountDeleteTransaction accountDeleteTransaction = new AccountDeleteTransaction();

params.getDeleteAccountId()
.ifPresent(accountId -> accountDeleteTransaction.setAccountId(AccountId.fromString(accountId)));

params.getTransferAccountId()
.ifPresent(accountId -> accountDeleteTransaction.setTransferAccountId(AccountId.fromString(accountId)));

params.getCommonTransactionParams()
.ifPresent(commonTransactionParams ->
commonTransactionParams.fillOutTransaction(accountDeleteTransaction, sdkService.getClient()));

TransactionReceipt transactionReceipt =
accountDeleteTransaction.execute(sdkService.getClient()).getReceipt(sdkService.getClient());

return new AccountResponse(null, transactionReceipt.status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hedera.hashgraph.tck.methods.sdk.param;

import com.hedera.hashgraph.tck.methods.JSONRPC2Param;
import java.util.Map;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.minidev.json.JSONObject;

/**
* AccountDeleteParams for account delete method
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class AccountDeleteParams extends JSONRPC2Param {
private Optional<String> deleteAccountId;
private Optional<String> transferAccountId;
private Optional<CommonTransactionParams> commonTransactionParams;

@Override
public AccountDeleteParams parse(Map<String, Object> jrpcParams) throws ClassCastException {
var parsedDeleteAccountId = Optional.ofNullable((String) jrpcParams.get("deleteAccountId"));
var parsedTransferAccountId = Optional.ofNullable((String) jrpcParams.get("transferAccountId"));

Optional<CommonTransactionParams> parsedCommonTransactionParams = Optional.empty();
if (jrpcParams.containsKey("commonTransactionParams")) {
JSONObject jsonObject = (JSONObject) jrpcParams.get("commonTransactionParams");
parsedCommonTransactionParams = Optional.of(CommonTransactionParams.parse(jsonObject));
}

return new AccountDeleteParams(parsedDeleteAccountId, parsedTransferAccountId, parsedCommonTransactionParams);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.hedera.hashgraph.tck.methods.sdk.param;

import com.hedera.hashgraph.tck.methods.JSONRPC2Param;
import java.util.Map;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.minidev.json.JSONObject;

/**
* AccountUpdateParams for account update method
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class AccountUpdateParams extends JSONRPC2Param {
private Optional<String> key;
private Optional<Boolean> receiverSignatureRequired;
private Optional<Long> autoRenewPeriod;
private Optional<String> memo;
private Optional<Long> expirationTime;
private Optional<Long> maxAutoTokenAssociations;
private Optional<String> stakedAccountId;
private Optional<String> accountId;
private Optional<Long> stakedNodeId;
private Optional<Boolean> declineStakingReward;
private Optional<CommonTransactionParams> commonTransactionParams;

@Override
public AccountUpdateParams parse(Map<String, Object> jrpcParams) throws ClassCastException {
var parsedKey = Optional.ofNullable((String) jrpcParams.get("key"));
var parsedReceiverSignatureRequired =
Optional.ofNullable((Boolean) jrpcParams.get("receiverSignatureRequired"));
var parsedAutoRenewPeriod = Optional.ofNullable((Long) jrpcParams.get("autoRenewPeriod"));
var parsedMemo = Optional.ofNullable((String) jrpcParams.get("memo"));
var parsedMaxAutoTokenAssociations = Optional.ofNullable((Long) jrpcParams.get("maxAutoTokenAssociations"));
var parsedStakedAccountId = Optional.ofNullable((String) jrpcParams.get("stakedAccountId"));
var parsedAccountId = Optional.ofNullable((String) jrpcParams.get("accountId"));
var parsedStakedNodeId = Optional.ofNullable((Long) jrpcParams.get("stakedNodeId"));
var parsedExpirationTime = Optional.ofNullable((Long) jrpcParams.get("expirationTime"));
var parsedDeclineStakingReward = Optional.ofNullable((Boolean) jrpcParams.get("declineStakingReward"));
Optional<CommonTransactionParams> parsedCommonTransactionParams = Optional.empty();
if (jrpcParams.containsKey("commonTransactionParams")) {
JSONObject jsonObject = (JSONObject) jrpcParams.get("commonTransactionParams");
parsedCommonTransactionParams = Optional.of(CommonTransactionParams.parse(jsonObject));
}

return new AccountUpdateParams(
parsedKey,
parsedReceiverSignatureRequired,
parsedAutoRenewPeriod,
parsedMemo,
parsedExpirationTime,
parsedMaxAutoTokenAssociations,
parsedStakedAccountId,
parsedAccountId,
parsedStakedNodeId,
parsedDeclineStakingReward,
parsedCommonTransactionParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import lombok.Data;

@Data
public class AccountCreateResponse {
public class AccountResponse {
private final String accountId;
private final Status status;
}

0 comments on commit 6af082f

Please sign in to comment.