11// SPDX-License-Identifier: Apache-2.0
22package com .hedera .hashgraph .tck .methods .sdk ;
33
4- import com .google .protobuf .InvalidProtocolBufferException ;
54import com .hedera .hashgraph .sdk .*;
65import com .hedera .hashgraph .tck .annotation .JSONRPC2Method ;
76import com .hedera .hashgraph .tck .annotation .JSONRPC2Service ;
109import com .hedera .hashgraph .tck .methods .sdk .param .transfer .*;
1110import com .hedera .hashgraph .tck .methods .sdk .response .AccountAllowanceResponse ;
1211import com .hedera .hashgraph .tck .methods .sdk .response .AccountResponse ;
13- import com .hedera .hashgraph .tck .util .KeyUtils ;
14- import java .time .Duration ;
12+ import com .hedera .hashgraph .tck .util .TransactionBuilders ;
1513import java .util .Map ;
16- import java .util .Optional ;
1714
1815/**
1916 * AccountService for account related methods
@@ -28,41 +25,7 @@ public AccountService(SdkService sdkService) {
2825
2926 @ JSONRPC2Method ("createAccount" )
3027 public AccountResponse createAccount (final AccountCreateParams params ) throws Exception {
31- AccountCreateTransaction accountCreateTransaction = new AccountCreateTransaction ();
32- params .getKey ().ifPresent (key -> {
33- try {
34- accountCreateTransaction .setKeyWithoutAlias (KeyUtils .getKeyFromString (key ));
35- } catch (InvalidProtocolBufferException e ) {
36- throw new IllegalArgumentException (e );
37- }
38- });
39-
40- params .getInitialBalance ()
41- .ifPresent (initialBalanceTinybars -> accountCreateTransaction .setInitialBalance (
42- Hbar .from (Long .parseLong (initialBalanceTinybars ), HbarUnit .TINYBAR )));
43-
44- params .getReceiverSignatureRequired ().ifPresent (accountCreateTransaction ::setReceiverSignatureRequired );
45-
46- params .getAutoRenewPeriod ()
47- .ifPresent (autoRenewPeriodSeconds -> accountCreateTransaction .setAutoRenewPeriod (
48- Duration .ofSeconds (Long .parseLong (autoRenewPeriodSeconds ))));
49-
50- params .getMemo ().ifPresent (accountCreateTransaction ::setAccountMemo );
51-
52- params .getMaxAutoTokenAssociations ()
53- .ifPresent (autoAssociations ->
54- accountCreateTransaction .setMaxAutomaticTokenAssociations (autoAssociations .intValue ()));
55-
56- params .getStakedAccountId ()
57- .ifPresent (stakedAccountId ->
58- accountCreateTransaction .setStakedAccountId (AccountId .fromString (stakedAccountId )));
59-
60- params .getStakedNodeId ()
61- .ifPresent (stakedNodeId -> accountCreateTransaction .setStakedNodeId (Long .parseLong (stakedNodeId )));
62-
63- params .getDeclineStakingReward ().ifPresent (accountCreateTransaction ::setDeclineStakingReward );
64-
65- params .getAlias ().ifPresent (accountCreateTransaction ::setAlias );
28+ AccountCreateTransaction accountCreateTransaction = TransactionBuilders .AccountBuilder .buildCreate (params );
6629
6730 params .getCommonTransactionParams ()
6831 .ifPresent (commonTransactionParams ->
@@ -81,43 +44,7 @@ public AccountResponse createAccount(final AccountCreateParams params) throws Ex
8144
8245 @ JSONRPC2Method ("updateAccount" )
8346 public AccountResponse updateAccount (final AccountUpdateParams params ) throws Exception {
84- AccountUpdateTransaction accountUpdateTransaction = new AccountUpdateTransaction ();
85-
86- params .getAccountId ()
87- .ifPresent (accountId -> accountUpdateTransaction .setAccountId (AccountId .fromString (accountId )));
88-
89- params .getKey ().ifPresent (key -> {
90- try {
91- accountUpdateTransaction .setKey (KeyUtils .getKeyFromString (key ));
92- } catch (InvalidProtocolBufferException e ) {
93- throw new IllegalArgumentException (e );
94- }
95- });
96-
97- params .getReceiverSignatureRequired ().ifPresent (accountUpdateTransaction ::setReceiverSignatureRequired );
98-
99- params .getAutoRenewPeriod ()
100- .ifPresent (autoRenewPeriodSeconds -> accountUpdateTransaction .setAutoRenewPeriod (
101- Duration .ofSeconds (Long .parseLong (autoRenewPeriodSeconds ))));
102-
103- params .getMemo ().ifPresent (accountUpdateTransaction ::setAccountMemo );
104-
105- params .getExpirationTime ()
106- .ifPresent (expirationTime ->
107- accountUpdateTransaction .setExpirationTime (Duration .ofSeconds (Long .parseLong (expirationTime ))));
108-
109- params .getMaxAutoTokenAssociations ()
110- .ifPresent (autoAssociations ->
111- accountUpdateTransaction .setMaxAutomaticTokenAssociations (autoAssociations .intValue ()));
112-
113- params .getStakedAccountId ()
114- .ifPresent (stakedAccountId ->
115- accountUpdateTransaction .setStakedAccountId (AccountId .fromString (stakedAccountId )));
116-
117- params .getStakedNodeId ()
118- .ifPresent (stakedNodeId -> accountUpdateTransaction .setStakedNodeId (Long .parseLong (stakedNodeId )));
119-
120- params .getDeclineStakingReward ().ifPresent (accountUpdateTransaction ::setDeclineStakingReward );
47+ AccountUpdateTransaction accountUpdateTransaction = TransactionBuilders .AccountBuilder .buildUpdate (params );
12148
12249 params .getCommonTransactionParams ()
12350 .ifPresent (commonTransactionParams ->
@@ -131,13 +58,7 @@ public AccountResponse updateAccount(final AccountUpdateParams params) throws Ex
13158
13259 @ JSONRPC2Method ("deleteAccount" )
13360 public AccountResponse deleteAccount (final AccountDeleteParams params ) throws Exception {
134- AccountDeleteTransaction accountDeleteTransaction = new AccountDeleteTransaction ();
135-
136- params .getDeleteAccountId ()
137- .ifPresent (accountId -> accountDeleteTransaction .setAccountId (AccountId .fromString (accountId )));
138-
139- params .getTransferAccountId ()
140- .ifPresent (accountId -> accountDeleteTransaction .setTransferAccountId (AccountId .fromString (accountId )));
61+ AccountDeleteTransaction accountDeleteTransaction = TransactionBuilders .AccountBuilder .buildDelete (params );
14162
14263 params .getCommonTransactionParams ()
14364 .ifPresent (commonTransactionParams ->
@@ -151,9 +72,7 @@ public AccountResponse deleteAccount(final AccountDeleteParams params) throws Ex
15172
15273 @ JSONRPC2Method ("approveAllowance" )
15374 public AccountAllowanceResponse approveAllowance (final AccountAllowanceParams params ) throws Exception {
154- AccountAllowanceApproveTransaction tx = new AccountAllowanceApproveTransaction ();
155-
156- params .getAllowances ().ifPresent (allowances -> allowances .forEach (allowance -> approve (tx , allowance )));
75+ AccountAllowanceApproveTransaction tx = TransactionBuilders .AccountBuilder .buildApproveAllowance (params );
15776
15877 params .getCommonTransactionParams ()
15978 .ifPresent (commonParams -> commonParams .fillOutTransaction (tx , sdkService .getClient ()));
@@ -165,9 +84,7 @@ public AccountAllowanceResponse approveAllowance(final AccountAllowanceParams pa
16584
16685 @ JSONRPC2Method ("deleteAllowance" )
16786 public AccountAllowanceResponse deleteAllowance (final AccountAllowanceParams params ) throws Exception {
168- AccountAllowanceDeleteTransaction tx = new AccountAllowanceDeleteTransaction ();
169-
170- params .getAllowances ().ifPresent (allowances -> allowances .forEach (allowance -> delete (tx , allowance )));
87+ AccountAllowanceDeleteTransaction tx = TransactionBuilders .AccountBuilder .buildDeleteAllowance (params );
17188
17289 params .getCommonTransactionParams ()
17390 .ifPresent (commonParams -> commonParams .fillOutTransaction (tx , sdkService .getClient ()));
@@ -186,10 +103,7 @@ public AccountAllowanceResponse deleteAllowance(final AccountAllowanceParams par
186103 */
187104 @ JSONRPC2Method ("transferCrypto" )
188105 public Map <String , String > transferCrypto (final TransferCryptoParams params ) throws Exception {
189- TransferTransaction transferTransaction = new TransferTransaction ();
190-
191- params .getTransfers ()
192- .ifPresent (transfers -> transfers .forEach (txParams -> processTransfer (transferTransaction , txParams )));
106+ TransferTransaction transferTransaction = TransactionBuilders .TransferBuilder .buildTransfer (params );
193107
194108 params .getCommonTransactionParams ()
195109 .ifPresent (
@@ -204,7 +118,7 @@ public Map<String, String> transferCrypto(final TransferCryptoParams params) thr
204118 /**
205119 * Process an individual transfer based on its type (Hbar, Token, or NFT)
206120 */
207- private void processTransfer (TransferTransaction tx , TransferParams txParams ) {
121+ public static void processTransfer (TransferTransaction tx , TransferParams txParams ) {
208122 boolean approved = txParams .getApproved ().orElse (false );
209123
210124 txParams .getHbar ().ifPresent (hbarParams -> processHbarTransfer (tx , hbarParams , approved ));
@@ -215,7 +129,7 @@ private void processTransfer(TransferTransaction tx, TransferParams txParams) {
215129 /**
216130 * Process an Hbar transfer
217131 */
218- private void processHbarTransfer (TransferTransaction tx , HbarTransferParams hbarParams , boolean approved ) {
132+ private static void processHbarTransfer (TransferTransaction tx , HbarTransferParams hbarParams , boolean approved ) {
219133 hbarParams .getAmount ().ifPresent (amountStr -> {
220134 Hbar amount = Hbar .fromTinybars (Long .parseLong (amountStr ));
221135
@@ -242,7 +156,8 @@ private void processHbarTransfer(TransferTransaction tx, HbarTransferParams hbar
242156 /**
243157 * Process a token transfer
244158 */
245- private void processTokenTransfer (TransferTransaction tx , TokenTransferParams tokenParams , boolean approved ) {
159+ private static void processTokenTransfer (
160+ TransferTransaction tx , TokenTransferParams tokenParams , boolean approved ) {
246161 tokenParams .getAccountId ().ifPresent (accountIdStr -> {
247162 tokenParams .getTokenId ().ifPresent (tokenIdStr -> {
248163 tokenParams .getAmount ().ifPresent (amountStr -> {
@@ -272,7 +187,7 @@ private void processTokenTransfer(TransferTransaction tx, TokenTransferParams to
272187 /**
273188 * Process an NFT transfer
274189 */
275- private void processNftTransfer (TransferTransaction tx , NftTransferParams nftParams , boolean approved ) {
190+ private static void processNftTransfer (TransferTransaction tx , NftTransferParams nftParams , boolean approved ) {
276191 nftParams .getSenderAccountId ().ifPresent (senderIdStr -> {
277192 nftParams .getReceiverAccountId ().ifPresent (receiverIdStr -> {
278193 nftParams .getTokenId ().ifPresent (tokenIdStr -> {
@@ -293,55 +208,4 @@ private void processNftTransfer(TransferTransaction tx, NftTransferParams nftPar
293208 });
294209 });
295210 }
296-
297- private void approve (AccountAllowanceApproveTransaction tx , AllowanceParams allowance ) {
298- AccountId owner = AccountId .fromString (allowance .getOwnerAccountId ().orElseThrow ());
299- AccountId spender = AccountId .fromString (allowance .getSpenderAccountId ().orElseThrow ());
300-
301- allowance
302- .getHbar ()
303- .ifPresent (hbar ->
304- tx .approveHbarAllowance (owner , spender , Hbar .fromTinybars (Long .parseLong (hbar .getAmount ()))));
305-
306- allowance
307- .getToken ()
308- .ifPresent (token -> tx .approveTokenAllowance (
309- TokenId .fromString (token .getTokenId ()), owner , spender , token .getAmount ()));
310-
311- allowance .getNft ().ifPresent (nft -> approveNFT (tx , owner , spender , nft ));
312- }
313-
314- private void delete (AccountAllowanceDeleteTransaction tx , AllowanceParams allowance ) {
315- var owner = AccountId .fromString (allowance .getOwnerAccountId ().orElseThrow ());
316- var tokenId = allowance .getTokenId ().orElseThrow ();
317-
318- if (allowance .getSerialNumbers ().isPresent ()) {
319- allowance .getSerialNumbers ().get ().forEach (serialNumber -> {
320- var nftId = new NftId (TokenId .fromString (tokenId ), Long .parseLong (serialNumber ));
321- tx .deleteAllTokenNftAllowances (nftId , owner );
322- });
323- }
324- }
325-
326- private void approveNFT (
327- AccountAllowanceApproveTransaction tx ,
328- AccountId owner ,
329- AccountId spender ,
330- AllowanceParams .TokenNftAllowance nft ) {
331- TokenId tokenId = TokenId .fromString (nft .getTokenId ());
332- Optional <String > delegateSpender = Optional .ofNullable (nft .getDelegatingSpender ());
333-
334- if (!nft .getSerialNumbers ().isEmpty ()) {
335- nft .getSerialNumbers ().forEach (serial -> {
336- NftId nftId = new NftId (tokenId , serial );
337- delegateSpender .ifPresentOrElse (
338- ds -> tx .approveTokenNftAllowance (nftId , owner , spender , AccountId .fromString (ds )),
339- () -> tx .approveTokenNftAllowance (nftId , owner , spender ));
340- });
341- } else if (nft .getAllSerials ()) {
342- tx .approveTokenNftAllowanceAllSerials (tokenId , owner , spender );
343- } else {
344- tx .deleteTokenNftAllowanceAllSerials (tokenId , owner , spender );
345- }
346- }
347211}
0 commit comments