Skip to content

Commit ec7cd91

Browse files
authored
feat(TCK): ContractCallQuery implementation (#2605)
Signed-off-by: Mustafa Uzun <mustafa.uzun@limechain.tech>
1 parent 1520c14 commit ec7cd91

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

tck/src/main/java/com/hedera/hashgraph/tck/methods/sdk/ContractService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import com.hedera.hashgraph.tck.annotation.JSONRPC2Method;
88
import com.hedera.hashgraph.tck.annotation.JSONRPC2Service;
99
import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service;
10+
import com.hedera.hashgraph.tck.methods.sdk.param.contract.ContractCallQueryParams;
1011
import com.hedera.hashgraph.tck.methods.sdk.param.contract.CreateContractParams;
1112
import com.hedera.hashgraph.tck.methods.sdk.param.contract.DeleteContractParams;
1213
import com.hedera.hashgraph.tck.methods.sdk.param.contract.ExecuteContractParams;
1314
import com.hedera.hashgraph.tck.methods.sdk.param.contract.InfoQueryContractParams;
1415
import com.hedera.hashgraph.tck.methods.sdk.param.contract.UpdateContractParams;
16+
import com.hedera.hashgraph.tck.methods.sdk.response.ContractCallResponse;
1517
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse;
1618
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse.ContractInfoQueryResponse;
1719
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse.ContractInfoQueryResponse.StakingInfoResponse;
1820
import com.hedera.hashgraph.tck.util.KeyUtils;
21+
import com.hedera.hashgraph.tck.util.QueryBuilders;
1922
import java.time.Duration;
2023
import org.bouncycastle.util.encoders.Hex;
2124

@@ -28,6 +31,26 @@ public ContractService(SdkService sdkService) {
2831
this.sdkService = sdkService;
2932
}
3033

34+
@JSONRPC2Method("contractCallQuery")
35+
public ContractCallResponse contractCallQuery(final ContractCallQueryParams params) throws Exception {
36+
ContractCallQuery query = QueryBuilders.buildContractCall(params);
37+
Client client = sdkService.getClient(params.getSessionId());
38+
39+
ContractFunctionResult result = query.execute(client);
40+
41+
return new ContractCallResponse(
42+
result.contractId.toString(),
43+
result.evmAddress,
44+
result.errorMessage,
45+
result.gasUsed,
46+
result.logs,
47+
result.gas,
48+
result.hbarAmount,
49+
result.senderAccountId,
50+
result.signerNonce,
51+
Hex.toHexString(result.asBytes()));
52+
}
53+
3154
@JSONRPC2Method("createContract")
3255
public ContractResponse createContract(final CreateContractParams params) throws Exception {
3356
ContractCreateTransaction transaction = new ContractCreateTransaction().setGrpcDeadline(DEFAULT_GRPC_DEADLINE);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.param.contract;
3+
4+
import com.hedera.hashgraph.tck.methods.JSONRPC2Param;
5+
import com.hedera.hashgraph.tck.util.JSONRPCParamParser;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
12+
@Getter
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
public class ContractCallQueryParams extends JSONRPC2Param {
16+
private String contractId;
17+
private String gas;
18+
private String functionParameters;
19+
private String maxResultSize;
20+
private String senderAccountId;
21+
private String sessionId;
22+
23+
@Override
24+
public JSONRPC2Param parse(Map<String, Object> jrpcParams) throws Exception {
25+
Objects.requireNonNull(jrpcParams, "jrpcParams must not be null");
26+
27+
var parsedContractId = (String) jrpcParams.get("contractId");
28+
var parsedGas = (String) jrpcParams.get("gas");
29+
var parsedFunctionParameters = (String) jrpcParams.get("functionParameters");
30+
var parsedMaxResultSize = (String) jrpcParams.get("maxResultSize");
31+
var parsedAccountId = (String) jrpcParams.get("senderAccountId");
32+
33+
return new ContractCallQueryParams(
34+
parsedContractId,
35+
parsedGas,
36+
parsedFunctionParameters,
37+
parsedMaxResultSize,
38+
parsedAccountId,
39+
JSONRPCParamParser.parseSessionId(jrpcParams));
40+
}
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.response;
3+
4+
import com.hedera.hashgraph.sdk.AccountId;
5+
import com.hedera.hashgraph.sdk.ContractId;
6+
import com.hedera.hashgraph.sdk.ContractLogInfo;
7+
import com.hedera.hashgraph.sdk.Hbar;
8+
import java.util.List;
9+
import javax.annotation.Nullable;
10+
import lombok.Data;
11+
12+
@Data
13+
public class ContractCallResponse {
14+
15+
/**
16+
* The ID of the contract that was invoked.
17+
*/
18+
public final String contractId;
19+
20+
/**
21+
* The contract's 20-byte EVM address
22+
*/
23+
@Nullable
24+
public final ContractId evmAddress;
25+
26+
/**
27+
* message in case there was an error during smart contract execution
28+
*/
29+
@Nullable
30+
public final String errorMessage;
31+
32+
/**
33+
* units of gas used to execute contract
34+
*/
35+
public final long gasUsed;
36+
37+
/**
38+
* the log info for events returned by the function
39+
*/
40+
public final List<ContractLogInfo> logs;
41+
42+
/**
43+
* The amount of gas available for the call, aka the gasLimit
44+
*/
45+
public final long gas;
46+
47+
/**
48+
* Number of tinybars sent (the function must be payable if this is nonzero).
49+
*/
50+
public final Hbar hbarAmount;
51+
52+
/**
53+
* The account that is the "sender." If not present it is the accountId from the transactionId.
54+
*/
55+
@Nullable
56+
public final AccountId senderAccountId;
57+
58+
/**
59+
* If not null this field specifies what the value of the signer account nonce is post transaction execution.
60+
* For transactions that don't update the signer nonce (like HAPI ContractCall and ContractCreate transactions) this field should be null.
61+
*/
62+
public final long signerNonce;
63+
64+
private final String rawResult;
65+
}

tck/src/main/java/com/hedera/hashgraph/tck/util/QueryBuilders.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
55
import com.hedera.hashgraph.sdk.AccountId;
6+
import com.hedera.hashgraph.sdk.ContractCallQuery;
67
import com.hedera.hashgraph.sdk.ContractId;
78
import com.hedera.hashgraph.sdk.FileContentsQuery;
89
import com.hedera.hashgraph.sdk.FileId;
@@ -12,10 +13,12 @@
1213
import com.hedera.hashgraph.sdk.TokenInfoQuery;
1314
import com.hedera.hashgraph.sdk.TokenNftInfoQuery;
1415
import com.hedera.hashgraph.tck.methods.sdk.param.account.AccountBalanceQueryParams;
16+
import com.hedera.hashgraph.tck.methods.sdk.param.contract.ContractCallQueryParams;
1517
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileContentsParams;
1618
import com.hedera.hashgraph.tck.methods.sdk.param.token.NftInfoQueryParams;
1719
import com.hedera.hashgraph.tck.methods.sdk.param.token.TokenInfoQueryParams;
1820
import java.time.Duration;
21+
import org.bouncycastle.util.encoders.Hex;
1922

2023
public class QueryBuilders {
2124

@@ -78,4 +81,25 @@ public static FileContentsQuery buildFileContents(FileContentsParams params) {
7881
return query;
7982
}
8083
}
84+
85+
public static ContractCallQuery buildContractCall(ContractCallQueryParams params) {
86+
ContractCallQuery query = new ContractCallQuery().setGrpcDeadline((DEFAULT_GRPC_DEADLINE));
87+
if (params.getContractId() != null) {
88+
query.setContractId(ContractId.fromString(params.getContractId()));
89+
}
90+
if (params.getGas() != null) {
91+
query.setGas(Long.parseLong(params.getGas()));
92+
}
93+
if (params.getFunctionParameters() != null) {
94+
query.setFunctionParameters(Hex.decode(params.getFunctionParameters()));
95+
}
96+
if (params.getMaxResultSize() != null) {
97+
query.setMaxResultSize(Long.parseLong(params.getMaxResultSize()));
98+
}
99+
if (params.getSenderAccountId() != null) {
100+
query.setSenderAccountId(AccountId.fromString(params.getSenderAccountId()));
101+
}
102+
103+
return query;
104+
}
81105
}

0 commit comments

Comments
 (0)