Skip to content

Commit

Permalink
feat(TCK): updateToken method (#2084)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov authored Nov 18, 2024
1 parent 4abf8a4 commit b8ae85c
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public class TokenUpdateTransaction extends Transaction<TokenUpdateTransaction>
*/
@Nullable
private Instant expirationTime = null;
private Duration expirationTimeDuration = null;

/**
* The new interval at which the auto-renew account will be charged to
* extend the token's expiry.
Expand Down Expand Up @@ -488,6 +490,16 @@ public TokenUpdateTransaction setExpirationTime(Instant expirationTime) {
requireNotFrozen();
autoRenewPeriod = null;
this.expirationTime = expirationTime;
this.expirationTimeDuration = null;
return this;
}

public TokenUpdateTransaction setExpirationTime(Duration expirationTime) {
Objects.requireNonNull(expirationTime);
requireNotFrozen();
autoRenewPeriod = null;
this.expirationTime = null;
this.expirationTimeDuration = expirationTime;
return this;
}

Expand Down Expand Up @@ -717,6 +729,9 @@ TokenUpdateTransactionBody.Builder build() {
if (expirationTime != null) {
builder.setExpiry(InstantConverter.toProtobuf(expirationTime));
}
if (expirationTimeDuration != null) {
builder.setExpiry(InstantConverter.toProtobuf(expirationTimeDuration));
}
if (autoRenewPeriod != null) {
builder.setAutoRenewPeriod(DurationConverter.toProtobuf(autoRenewPeriod));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import com.hedera.hashgraph.sdk.TokenId;
import com.hedera.hashgraph.sdk.TokenSupplyType;
import com.hedera.hashgraph.sdk.TokenType;
import com.hedera.hashgraph.sdk.TokenUpdateTransaction;
import com.hedera.hashgraph.sdk.TransactionReceipt;
import com.hedera.hashgraph.tck.annotation.JSONRPC2Method;
import com.hedera.hashgraph.tck.annotation.JSONRPC2Service;
import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service;
import com.hedera.hashgraph.tck.methods.sdk.param.TokenCreateParams;
import com.hedera.hashgraph.tck.methods.sdk.param.TokenUpdateParams;
import com.hedera.hashgraph.tck.methods.sdk.response.TokenResponse;
import com.hedera.hashgraph.tck.util.KeyUtils;
import java.time.Duration;
Expand Down Expand Up @@ -233,4 +235,107 @@ public TokenResponse createToken(final TokenCreateParams params) throws Exceptio

return new TokenResponse(tokenId, transactionReceipt.status);
}

@JSONRPC2Method("updateToken")
public TokenResponse updateToken(final TokenUpdateParams params) throws Exception {
TokenUpdateTransaction tokenUpdateTransaction = new TokenUpdateTransaction();

params.getTokenId().ifPresent(tokenId -> tokenUpdateTransaction.setTokenId(TokenId.fromString(tokenId)));

params.getAdminKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setAdminKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getKycKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setKycKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getFreezeKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setFreezeKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getWipeKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setWipeKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getSupplyKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setSupplyKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getFeeScheduleKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setFeeScheduleKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getPauseKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setPauseKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getMetadataKey().ifPresent(key -> {
try {
tokenUpdateTransaction.setMetadataKey(KeyUtils.getKeyFromString(key));
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
});

params.getName().ifPresent(tokenUpdateTransaction::setTokenName);
params.getSymbol().ifPresent(tokenUpdateTransaction::setTokenSymbol);

params.getTreasuryAccountId()
.ifPresent(treasuryAccountId ->
tokenUpdateTransaction.setTreasuryAccountId(AccountId.fromString(treasuryAccountId)));

params.getExpirationTime()
.ifPresent(expirationTime ->
tokenUpdateTransaction.setExpirationTime(Duration.ofSeconds(Long.parseLong(expirationTime))));

params.getAutoRenewAccountId()
.ifPresent(autoRenewAccountId ->
tokenUpdateTransaction.setAutoRenewAccountId(AccountId.fromString(autoRenewAccountId)));

params.getAutoRenewPeriod()
.ifPresent(autoRenewPeriodSeconds -> tokenUpdateTransaction.setAutoRenewPeriod(
Duration.ofSeconds(Long.parseLong(autoRenewPeriodSeconds))));

params.getMemo().ifPresent(tokenUpdateTransaction::setTokenMemo);

params.getMetadata().ifPresent(metadata -> tokenUpdateTransaction.setTokenMetadata(metadata.getBytes()));

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

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

return new TokenResponse("", transactionReceipt.status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*-
*
* Hedera Java SDK
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

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;

/**
* TokenUpdateParams for token update method
*/
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class TokenUpdateParams extends JSONRPC2Param {
private Optional<String> tokenId;
private Optional<String> name;
private Optional<String> symbol;
private Optional<String> treasuryAccountId;
private Optional<String> adminKey;
private Optional<String> kycKey;
private Optional<String> freezeKey;
private Optional<String> wipeKey;
private Optional<String> supplyKey;
private Optional<String> feeScheduleKey;
private Optional<String> pauseKey;
private Optional<String> metadataKey;
private Optional<String> expirationTime;
private Optional<String> autoRenewAccountId;
private Optional<String> autoRenewPeriod;
private Optional<String> memo;
private Optional<String> metadata;
private Optional<CommonTransactionParams> commonTransactionParams;

@Override
public JSONRPC2Param parse(Map<String, Object> jrpcParams) throws Exception {
var parsedTokenId = Optional.ofNullable((String) jrpcParams.get("tokenId"));
var parsedName = Optional.ofNullable((String) jrpcParams.get("name"));
var parsedSymbol = Optional.ofNullable((String) jrpcParams.get("symbol"));
var parsedTreasuryAccountId = Optional.ofNullable((String) jrpcParams.get("treasuryAccountId"));
var parsedAdminKey = Optional.ofNullable((String) jrpcParams.get("adminKey"));
var parsedKycKey = Optional.ofNullable((String) jrpcParams.get("kycKey"));
var parsedFreezeKey = Optional.ofNullable((String) jrpcParams.get("freezeKey"));
var parsedWipeKey = Optional.ofNullable((String) jrpcParams.get("wipeKey"));
var parsedSupplyKey = Optional.ofNullable((String) jrpcParams.get("supplyKey"));
var parsedFeeScheduleKey = Optional.ofNullable((String) jrpcParams.get("feeScheduleKey"));
var parsedPauseKey = Optional.ofNullable((String) jrpcParams.get("pauseKey"));
var parsedMetadataKey = Optional.ofNullable((String) jrpcParams.get("metadataKey"));
var parsedExpirationTime = Optional.ofNullable((String) jrpcParams.get("expirationTime"));
var parsedAutoRenewAccountId = Optional.ofNullable((String) jrpcParams.get("autoRenewAccountId"));
var parsedAutoRenewPeriod = Optional.ofNullable((String) jrpcParams.get("autoRenewPeriod"));
var parsedMemo = Optional.ofNullable((String) jrpcParams.get("memo"));
var parsedMetadata = Optional.ofNullable((String) jrpcParams.get("metadata"));

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

return new TokenUpdateParams(
parsedTokenId,
parsedName,
parsedSymbol,
parsedTreasuryAccountId,
parsedAdminKey,
parsedKycKey,
parsedFreezeKey,
parsedWipeKey,
parsedSupplyKey,
parsedFeeScheduleKey,
parsedPauseKey,
parsedMetadataKey,
parsedExpirationTime,
parsedAutoRenewAccountId,
parsedAutoRenewPeriod,
parsedMemo,
parsedMetadata,
parsedCommonTransactionParams);
}
}

0 comments on commit b8ae85c

Please sign in to comment.