|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package com.hedera.hashgraph.tck.methods.sdk; |
| 3 | + |
| 4 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 5 | +import com.hedera.hashgraph.sdk.*; |
| 6 | +import com.hedera.hashgraph.tck.annotation.JSONRPC2Method; |
| 7 | +import com.hedera.hashgraph.tck.annotation.JSONRPC2Service; |
| 8 | +import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service; |
| 9 | +import com.hedera.hashgraph.tck.methods.sdk.param.contract.CreateContractParams; |
| 10 | +import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse; |
| 11 | +import com.hedera.hashgraph.tck.util.KeyUtils; |
| 12 | +import java.time.Duration; |
| 13 | +import org.bouncycastle.util.encoders.Hex; |
| 14 | + |
| 15 | +@JSONRPC2Service |
| 16 | +public class ContractService extends AbstractJSONRPC2Service { |
| 17 | + private static final Duration DEFAULT_GRPC_DEADLINE = Duration.ofSeconds(10L); |
| 18 | + private final SdkService sdkService; |
| 19 | + |
| 20 | + public ContractService(SdkService sdkService) { |
| 21 | + this.sdkService = sdkService; |
| 22 | + } |
| 23 | + |
| 24 | + @JSONRPC2Method("createContract") |
| 25 | + public ContractResponse createContract(final CreateContractParams params) throws Exception { |
| 26 | + ContractCreateTransaction transaction = new ContractCreateTransaction().setGrpcDeadline(DEFAULT_GRPC_DEADLINE); |
| 27 | + |
| 28 | + params.getAdminKey().ifPresent(key -> { |
| 29 | + try { |
| 30 | + transaction.setAdminKey(KeyUtils.getKeyFromString(key)); |
| 31 | + } catch (InvalidProtocolBufferException e) { |
| 32 | + throw new IllegalArgumentException(e); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + params.getAutoRenewPeriod() |
| 37 | + .ifPresent(periodStr -> transaction.setAutoRenewPeriod(Duration.ofSeconds(Long.parseLong(periodStr)))); |
| 38 | + |
| 39 | + params.getGas().ifPresent(gasStr -> transaction.setGas(Long.parseLong(gasStr))); |
| 40 | + |
| 41 | + params.getAutoRenewAccountId() |
| 42 | + .ifPresent(accountIdStr -> transaction.setAutoRenewAccountId(AccountId.fromString(accountIdStr))); |
| 43 | + |
| 44 | + params.getInitialBalance() |
| 45 | + .ifPresent(balanceStr -> transaction.setInitialBalance(Hbar.fromTinybars(Long.parseLong(balanceStr)))); |
| 46 | + |
| 47 | + params.getInitcode().ifPresent(hex -> transaction.setBytecode(Hex.decode(hex))); |
| 48 | + |
| 49 | + params.getBytecodeFileId().ifPresent(fileIdStr -> transaction.setBytecodeFileId(FileId.fromString(fileIdStr))); |
| 50 | + |
| 51 | + params.getStakedAccountId() |
| 52 | + .ifPresent(accountIdStr -> transaction.setStakedAccountId(AccountId.fromString(accountIdStr))); |
| 53 | + |
| 54 | + params.getStakedNodeId().ifPresent(nodeIdStr -> transaction.setStakedNodeId(Long.parseLong(nodeIdStr))); |
| 55 | + |
| 56 | + params.getDeclineStakingReward().ifPresent(transaction::setDeclineStakingReward); |
| 57 | + |
| 58 | + params.getMemo().ifPresent(transaction::setContractMemo); |
| 59 | + |
| 60 | + params.getMaxAutomaticTokenAssociations() |
| 61 | + .ifPresent(maxAuto -> transaction.setMaxAutomaticTokenAssociations(maxAuto.intValue())); |
| 62 | + |
| 63 | + params.getConstructorParameters().ifPresent(hex -> transaction.setConstructorParameters(Hex.decode(hex))); |
| 64 | + |
| 65 | + params.getCommonTransactionParams() |
| 66 | + .ifPresent(common -> common.fillOutTransaction(transaction, sdkService.getClient())); |
| 67 | + |
| 68 | + TransactionReceipt receipt = transaction.execute(sdkService.getClient()).getReceipt(sdkService.getClient()); |
| 69 | + |
| 70 | + String contractId = ""; |
| 71 | + if (receipt.status == Status.SUCCESS && receipt.contractId != null) { |
| 72 | + contractId = receipt.contractId.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + return new ContractResponse(contractId, receipt.status); |
| 76 | + } |
| 77 | +} |
0 commit comments