Skip to content

Commit 955361b

Browse files
0xivanovemiliyank
andauthored
chore: Add TCK Endpoints (#2616)
Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com> Signed-off-by: emiliyank <e.kadiyski@gmail.com> Co-authored-by: emiliyank <e.kadiyski@gmail.com>
1 parent d175a2a commit 955361b

11 files changed

Lines changed: 513 additions & 6 deletions

File tree

sdk/src/main/java/com/hedera/hashgraph/sdk/TokenNftAllowance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class TokenNftAllowance {
4040
* approval on an NFT serial to another spender.
4141
*/
4242
@Nullable
43-
AccountId delegatingSpender;
43+
public final AccountId delegatingSpender;
4444

4545
/**
4646
* The list of serial numbers that the spender is permitted to transfer.

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
import com.hedera.hashgraph.tck.methods.sdk.response.AccountAllowanceResponse;
1111
import com.hedera.hashgraph.tck.methods.sdk.response.AccountBalanceResponse;
1212
import com.hedera.hashgraph.tck.methods.sdk.response.AccountResponse;
13+
import com.hedera.hashgraph.tck.methods.sdk.response.GetAccountInfoResponse;
1314
import com.hedera.hashgraph.tck.util.QueryBuilders;
1415
import com.hedera.hashgraph.tck.util.TransactionBuilders;
16+
import java.time.Duration;
17+
import java.util.HashMap;
18+
import java.util.List;
1519
import java.util.Map;
20+
import java.util.stream.Collectors;
1621

1722
/**
1823
* AccountService for account related methods
@@ -107,6 +112,19 @@ public AccountAllowanceResponse deleteAllowance(final AccountAllowanceParams par
107112
return new AccountAllowanceResponse(transactionReceipt.status);
108113
}
109114

115+
@JSONRPC2Method("getAccountInfo")
116+
public GetAccountInfoResponse getAccountInfo(final GetAccountInfoParams params) throws Exception {
117+
Client client = sdkService.getClient(params.getSessionId());
118+
AccountInfoQuery query = new AccountInfoQuery().setGrpcDeadline(Duration.ofSeconds(10L));
119+
120+
if (params.getAccountId() != null) {
121+
query.setAccountId(AccountId.fromString(params.getAccountId()));
122+
}
123+
124+
AccountInfo accountInfo = query.execute(client);
125+
return mapAccountInfoResponse(accountInfo);
126+
}
127+
110128
/**
111129
* Transfers cryptocurrency between accounts
112130
*
@@ -220,4 +238,112 @@ private static void processNftTransfer(TransferTransaction tx, NftTransferParams
220238
});
221239
});
222240
}
241+
242+
/**
243+
* Map AccountInfo from SDK to GetAccountInfoResponse for JSON-RPC
244+
*/
245+
private static GetAccountInfoResponse mapAccountInfoResponse(AccountInfo info) {
246+
return new GetAccountInfoResponse(
247+
info.accountId.toString(),
248+
info.contractAccountId,
249+
info.isDeleted,
250+
info.proxyAccountId != null ? info.proxyAccountId.toString() : null,
251+
String.valueOf(info.proxyReceived.toTinybars()),
252+
info.key != null ? info.key.toString() : null,
253+
String.valueOf(info.balance.toTinybars()),
254+
String.valueOf(info.sendRecordThreshold.toTinybars()),
255+
String.valueOf(info.receiveRecordThreshold.toTinybars()),
256+
info.isReceiverSignatureRequired,
257+
info.expirationTime.toString(),
258+
String.valueOf(info.autoRenewPeriod.getSeconds()),
259+
mapLiveHashes(info.liveHashes),
260+
mapTokenRelationships(info.tokenRelationships),
261+
info.accountMemo,
262+
String.valueOf(info.ownedNfts),
263+
String.valueOf(info.maxAutomaticTokenAssociations),
264+
info.aliasKey != null ? info.aliasKey.toString() : null,
265+
info.ledgerId != null ? info.ledgerId.toString() : null,
266+
mapHbarAllowances(info.hbarAllowances),
267+
mapTokenAllowances(info.tokenAllowances),
268+
mapNftAllowances(info.tokenNftAllowances),
269+
String.valueOf(info.ethereumNonce),
270+
mapStakingInfo(info.stakingInfo));
271+
}
272+
273+
private static List<GetAccountInfoResponse.LiveHashResponse> mapLiveHashes(List<LiveHash> liveHashes) {
274+
return liveHashes.stream()
275+
.map(lh -> new GetAccountInfoResponse.LiveHashResponse(
276+
lh.accountId.toString(),
277+
java.util.Base64.getEncoder().encodeToString(lh.hash.toByteArray()),
278+
lh.keys.stream().map(key -> key.toString()).collect(Collectors.toList()),
279+
String.valueOf(lh.duration.getSeconds())))
280+
.collect(Collectors.toList());
281+
}
282+
283+
private static Map<String, GetAccountInfoResponse.TokenRelationshipInfo> mapTokenRelationships(
284+
Map<TokenId, TokenRelationship> rels) {
285+
Map<String, GetAccountInfoResponse.TokenRelationshipInfo> result = new HashMap<>();
286+
for (Map.Entry<TokenId, TokenRelationship> entry : rels.entrySet()) {
287+
TokenRelationship tr = entry.getValue();
288+
result.put(
289+
entry.getKey().toString(),
290+
new GetAccountInfoResponse.TokenRelationshipInfo(
291+
tr.tokenId.toString(),
292+
tr.symbol,
293+
String.valueOf(tr.balance),
294+
tr.kycStatus,
295+
tr.freezeStatus,
296+
tr.automaticAssociation));
297+
}
298+
return result;
299+
}
300+
301+
private static List<GetAccountInfoResponse.HbarAllowanceResponse> mapHbarAllowances(
302+
List<HbarAllowance> allowances) {
303+
return allowances.stream()
304+
.map(a -> new GetAccountInfoResponse.HbarAllowanceResponse(
305+
a.ownerAccountId != null ? a.ownerAccountId.toString() : null,
306+
a.spenderAccountId != null ? a.spenderAccountId.toString() : null,
307+
a.amount != null ? String.valueOf(a.amount.toTinybars()) : null))
308+
.collect(Collectors.toList());
309+
}
310+
311+
private static List<GetAccountInfoResponse.TokenAllowanceResponse> mapTokenAllowances(
312+
List<TokenAllowance> allowances) {
313+
return allowances.stream()
314+
.map(a -> new GetAccountInfoResponse.TokenAllowanceResponse(
315+
a.tokenId != null ? a.tokenId.toString() : null,
316+
a.ownerAccountId != null ? a.ownerAccountId.toString() : null,
317+
a.spenderAccountId != null ? a.spenderAccountId.toString() : null,
318+
String.valueOf(a.amount)))
319+
.collect(Collectors.toList());
320+
}
321+
322+
private static List<GetAccountInfoResponse.TokenNftAllowanceResponse> mapNftAllowances(
323+
List<TokenNftAllowance> allowances) {
324+
return allowances.stream()
325+
.map(a -> new GetAccountInfoResponse.TokenNftAllowanceResponse(
326+
a.tokenId != null ? a.tokenId.toString() : null,
327+
a.ownerAccountId != null ? a.ownerAccountId.toString() : null,
328+
a.spenderAccountId != null ? a.spenderAccountId.toString() : null,
329+
a.serialNumbers != null
330+
? a.serialNumbers.stream().map(String::valueOf).collect(Collectors.toList())
331+
: null,
332+
a.allSerials,
333+
a.delegatingSpender != null ? a.delegatingSpender.toString() : null))
334+
.collect(Collectors.toList());
335+
}
336+
337+
private static GetAccountInfoResponse.StakingInfoResponse mapStakingInfo(StakingInfo info) {
338+
if (info == null) {
339+
return null;
340+
}
341+
return new GetAccountInfoResponse.StakingInfoResponse(
342+
info.declineStakingReward,
343+
info.stakePeriodStart != null ? info.stakePeriodStart.toString() : null,
344+
info.pendingReward != null ? String.valueOf(info.pendingReward.toTinybars()) : null,
345+
info.stakedToMe != null ? String.valueOf(info.stakedToMe.toTinybars()) : null,
346+
info.stakedAccountId != null ? info.stakedAccountId.toString() : null,
347+
info.stakedNodeId != null ? String.valueOf(info.stakedNodeId) : null);
348+
}
223349
}

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import com.hedera.hashgraph.tck.methods.sdk.param.contract.CreateContractParams;
1111
import com.hedera.hashgraph.tck.methods.sdk.param.contract.DeleteContractParams;
1212
import com.hedera.hashgraph.tck.methods.sdk.param.contract.ExecuteContractParams;
13+
import com.hedera.hashgraph.tck.methods.sdk.param.contract.InfoQueryContractParams;
1314
import com.hedera.hashgraph.tck.methods.sdk.param.contract.UpdateContractParams;
1415
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse;
16+
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse.ContractInfoQueryResponse;
17+
import com.hedera.hashgraph.tck.methods.sdk.response.ContractResponse.ContractInfoQueryResponse.StakingInfoResponse;
1518
import com.hedera.hashgraph.tck.util.KeyUtils;
1619
import java.time.Duration;
1720
import org.bouncycastle.util.encoders.Hex;
@@ -184,4 +187,74 @@ public ContractResponse deleteContract(final DeleteContractParams params) throws
184187

185188
return new ContractResponse(null, receipt.status);
186189
}
190+
191+
@JSONRPC2Method("contractInfoQuery")
192+
public ContractInfoQueryResponse contractInfoQuery(final InfoQueryContractParams params) throws Exception {
193+
ContractInfoQuery query = new ContractInfoQuery().setGrpcDeadline(DEFAULT_GRPC_DEADLINE);
194+
Client client = sdkService.getClient(params.getSessionId());
195+
196+
params.getContractId().ifPresent(contractIdStr -> query.setContractId(ContractId.fromString(contractIdStr)));
197+
198+
params.getQueryPayment()
199+
.ifPresent(
200+
queryPaymentStr -> query.setQueryPayment(Hbar.fromTinybars(Long.parseLong(queryPaymentStr))));
201+
202+
params.getMaxQueryPayment()
203+
.ifPresent(maxQueryPaymentStr ->
204+
query.setMaxQueryPayment(Hbar.fromTinybars(Long.parseLong(maxQueryPaymentStr))));
205+
206+
ContractInfo result = query.execute(client);
207+
return mapContractInfo(result);
208+
}
209+
210+
private static ContractInfoQueryResponse mapContractInfo(ContractInfo result) {
211+
return new ContractInfoQueryResponse(
212+
toStringOrNull(result.contractId),
213+
toStringOrNull(result.accountId),
214+
emptyToNull(result.contractAccountId),
215+
toStringOrNull(result.adminKey),
216+
epochSecondsOrNull(result.expirationTime),
217+
durationSecondsOrNull(result.autoRenewPeriod),
218+
toStringOrNull(result.autoRenewAccountId),
219+
Long.toString(result.storage),
220+
emptyToNull(result.contractMemo),
221+
hbarToTinybarsOrNull(result.balance),
222+
result.isDeleted,
223+
"0",
224+
toStringOrNull(result.ledgerId),
225+
mapStakingInfo(result.stakingInfo));
226+
}
227+
228+
private static StakingInfoResponse mapStakingInfo(StakingInfo stakingInfo) {
229+
if (stakingInfo == null) {
230+
return null;
231+
}
232+
return new StakingInfoResponse(
233+
stakingInfo.declineStakingReward,
234+
epochSecondsOrNull(stakingInfo.stakePeriodStart),
235+
hbarToTinybarsOrNull(stakingInfo.pendingReward),
236+
hbarToTinybarsOrNull(stakingInfo.stakedToMe),
237+
toStringOrNull(stakingInfo.stakedAccountId),
238+
stakingInfo.stakedNodeId != null ? stakingInfo.stakedNodeId.toString() : null);
239+
}
240+
241+
private static String toStringOrNull(Object value) {
242+
return value != null ? value.toString() : null;
243+
}
244+
245+
private static String epochSecondsOrNull(java.time.Instant instant) {
246+
return instant != null ? Long.toString(instant.getEpochSecond()) : null;
247+
}
248+
249+
private static String durationSecondsOrNull(Duration duration) {
250+
return duration != null ? Long.toString(duration.getSeconds()) : null;
251+
}
252+
253+
private static String hbarToTinybarsOrNull(Hbar hbar) {
254+
return hbar != null ? Long.toString(hbar.toTinybars()) : null;
255+
}
256+
257+
private static String emptyToNull(String value) {
258+
return value == null || value.isEmpty() ? null : value;
259+
}
187260
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.hedera.hashgraph.tck.methods.sdk;
33

4+
import com.google.protobuf.ByteString;
45
import com.hedera.hashgraph.sdk.*;
56
import com.hedera.hashgraph.tck.annotation.JSONRPC2Method;
67
import com.hedera.hashgraph.tck.annotation.JSONRPC2Service;
78
import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service;
89
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileAppendParams;
10+
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileContentsParams;
911
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileCreateParams;
1012
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileDeleteParams;
1113
import com.hedera.hashgraph.tck.methods.sdk.param.file.FileUpdateParams;
14+
import com.hedera.hashgraph.tck.methods.sdk.response.FileContentsResponse;
1215
import com.hedera.hashgraph.tck.methods.sdk.response.FileResponse;
16+
import com.hedera.hashgraph.tck.util.QueryBuilders;
1317
import com.hedera.hashgraph.tck.util.TransactionBuilders;
1418
import java.time.Duration;
1519

@@ -86,4 +90,17 @@ public FileResponse appendFile(final FileAppendParams params) throws Exception {
8690

8791
return new FileResponse("", receipt.status);
8892
}
93+
94+
@JSONRPC2Method("getFileContents")
95+
public FileContentsResponse getFileContents(final FileContentsParams params) throws Exception {
96+
FileContentsQuery query = QueryBuilders.FileBuilder.buildFileContents(params);
97+
Client client = sdkService.getClient(params.getSessionId());
98+
99+
ByteString response = query.execute(client);
100+
101+
// Convert ByteString to string
102+
String contents = response.toStringUtf8();
103+
104+
return new FileContentsResponse(contents);
105+
}
89106
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.param.account;
3+
4+
import com.hedera.hashgraph.tck.methods.JSONRPC2Param;
5+
import com.hedera.hashgraph.tck.util.JSONRPCParamParser;
6+
import java.util.Map;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
/**
12+
* GetAccountInfoParams for account info query method
13+
*/
14+
@Getter
15+
@AllArgsConstructor
16+
@NoArgsConstructor
17+
public class GetAccountInfoParams extends JSONRPC2Param {
18+
private String sessionId;
19+
private String accountId;
20+
21+
@Override
22+
public GetAccountInfoParams parse(Map<String, Object> jrpcParams) {
23+
return new GetAccountInfoParams(
24+
JSONRPCParamParser.parseSessionId(jrpcParams), (String) jrpcParams.get("accountId"));
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.Optional;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
12+
/**
13+
* InfoQueryContractParams for contract info query method
14+
*/
15+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
16+
@Getter
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public class InfoQueryContractParams extends JSONRPC2Param {
20+
private Optional<String> contractId;
21+
private Optional<String> queryPayment;
22+
private Optional<String> maxQueryPayment;
23+
private String sessionId;
24+
25+
@Override
26+
public InfoQueryContractParams parse(Map<String, Object> jrpcParams) throws Exception {
27+
var parsedContractId = Optional.ofNullable((String) jrpcParams.get("contractId"));
28+
var parsedQueryPayment = Optional.ofNullable((String) jrpcParams.get("queryPayment"));
29+
var parsedMaxQueryPayment = Optional.ofNullable((String) jrpcParams.get("maxQueryPayment"));
30+
31+
return new InfoQueryContractParams(
32+
parsedContractId,
33+
parsedQueryPayment,
34+
parsedMaxQueryPayment,
35+
JSONRPCParamParser.parseSessionId(jrpcParams));
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.param.file;
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.Optional;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
12+
/**
13+
* GetFileContentsParams for get file contents method
14+
*/
15+
@Getter
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
public class FileContentsParams extends JSONRPC2Param {
19+
private String fileId;
20+
private Optional<String> queryPayment;
21+
private Optional<String> maxQueryPayment;
22+
private String sessionId;
23+
24+
@Override
25+
public FileContentsParams parse(Map<String, Object> jrpcParams) throws Exception {
26+
var parsedFileId = (String) jrpcParams.get("fileId");
27+
var parsedQueryPayment = Optional.ofNullable((String) jrpcParams.get("queryPayment"));
28+
var parsedMaxQueryPayment = Optional.ofNullable((String) jrpcParams.get("maxQueryPayment"));
29+
30+
return new FileContentsParams(
31+
parsedFileId, parsedQueryPayment, parsedMaxQueryPayment, JSONRPCParamParser.parseSessionId(jrpcParams));
32+
}
33+
}

0 commit comments

Comments
 (0)