Skip to content

Commit a077c80

Browse files
committed
extend associateNft() & associateToken() to work with multiple token
Signed-off-by: Manish Dait <[email protected]>
1 parent ecd14fd commit a077c80

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/FungibleTokenClient.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,29 @@ default void associateToken(@NonNull TokenId tokenId, @NonNull Account account)
232232
associateToken(tokenId, account.accountId(), account.privateKey());
233233
}
234234

235+
/**
236+
* Associate an account with token.
237+
*
238+
* @param tokenIds list of the ID of the token
239+
* @param accountId the accountId
240+
* @param accountKey the account privateKey
241+
* @throws HieroException if the account could not be associated with the token
242+
*/
243+
void associateToken(@NonNull List<TokenId> tokenIds, @NonNull AccountId accountId, @NonNull PrivateKey accountKey)
244+
throws HieroException;
245+
246+
/**
247+
* Associate an account with token.
248+
*
249+
* @param tokenIds list of the ID of the token
250+
* @param account the account
251+
* @throws HieroException if the account could not be associated with the token
252+
*/
253+
default void associateToken(@NonNull List<TokenId> tokenIds, @NonNull Account account) throws HieroException {
254+
Objects.requireNonNull(account, "accountId must not be null");
255+
associateToken(tokenIds, account.accountId(), account.privateKey());
256+
};
257+
235258
/**
236259
* Dissociate an account with token.
237260
*
@@ -294,7 +317,6 @@ default void dissociateToken(@NonNull List<TokenId> tokenIds, @NonNull Account a
294317
dissociateToken(tokenIds, account.accountId(), account.privateKey());
295318
};
296319

297-
298320
/**
299321
* Mint a Token.
300322
*

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/NftClient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,31 @@ default void associateNft(@NonNull TokenId tokenId, @NonNull Account account) th
205205
associateNft(tokenId, account.accountId(), account.privateKey());
206206
}
207207

208+
/**
209+
* Associate an account with an NFT type. If an account is associated with an NFT type, the account can hold NFTs of
210+
* that type. Otherwise, the account cannot hold NFTs of that type and tranfer NFTs of that type will fail.
211+
*
212+
* @param tokenIds the List of ID for NFT type
213+
* @param accountId the accountId
214+
* @param accountKey the account privateKey
215+
* @throws HieroException if the account could not be associated with the NFT type
216+
*/
217+
void associateNft(@NonNull List<TokenId> tokenIds, @NonNull AccountId accountId, @NonNull PrivateKey accountKey)
218+
throws HieroException;
219+
220+
/**
221+
* Associate an account with an NFT type. If an account is associated with an NFT type, the account can hold NFTs of
222+
* that type. Otherwise, the account cannot hold NFTs of that type and tranfer NFTs of that type will fail.
223+
*
224+
* @param tokenIds the List of ID for NFT type
225+
* @param account the account
226+
* @throws HieroException if the account could not be associated with the NFT type
227+
*/
228+
default void associateNft(@NonNull List<TokenId> tokenIds, @NonNull Account account) throws HieroException {
229+
Objects.requireNonNull(account, "accountId must not be null");
230+
associateNft(tokenIds, account.accountId(), account.privateKey());
231+
};
232+
208233
/**
209234
* Dissociate an account with an NFT type.
210235
*

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/FungibleTokenClientImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public void associateToken(@NonNull TokenId tokenId, @NonNull AccountId accountI
6767
client.executeTokenAssociateTransaction(request);
6868
}
6969

70+
@Override
71+
public void associateToken(@NonNull List<TokenId> tokenIds, @NonNull AccountId accountId, @NonNull PrivateKey accountKey) throws HieroException {
72+
Objects.requireNonNull(tokenIds, "tokenIds must not be null");
73+
Objects.requireNonNull(accountId, "accountId must not be null");
74+
Objects.requireNonNull(accountKey, "accountKey must not be null");
75+
if (tokenIds.isEmpty()) {
76+
throw new IllegalArgumentException("tokenIds must not be empty");
77+
}
78+
final TokenAssociateRequest request = TokenAssociateRequest.of(tokenIds, accountId, accountKey);
79+
client.executeTokenAssociateTransaction(request);
80+
}
81+
7082
@Override
7183
public void dissociateToken(@NonNull TokenId tokenId, @NonNull AccountId accountId, @NonNull PrivateKey accountKey) throws HieroException {
7284
Objects.requireNonNull(tokenId, "tokenId must not be null");

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/NftClientImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public void associateNft(@NonNull final TokenId tokenId, @NonNull final AccountI
6868
client.executeTokenAssociateTransaction(request);
6969
}
7070

71+
@Override
72+
public void associateNft(@NonNull List<TokenId> tokenIds, @NonNull AccountId accountId, @NonNull PrivateKey accountKey) throws HieroException {
73+
Objects.requireNonNull(tokenIds, "tokenIds must not be null");
74+
Objects.requireNonNull(accountId, "accountId must not be null");
75+
Objects.requireNonNull(accountKey, "accountKey must not be null");
76+
if (tokenIds.isEmpty()) {
77+
throw new IllegalArgumentException("tokenIds must not be empty");
78+
}
79+
final TokenAssociateRequest request = TokenAssociateRequest.of(tokenIds, accountId, accountKey);
80+
client.executeTokenAssociateTransaction(request);
81+
}
82+
7183
@Override
7284
public void dissociateNft(@NonNull TokenId tokenId, @NonNull AccountId accountId, @NonNull PrivateKey accountKey) throws HieroException {
7385
Objects.requireNonNull(tokenId, "tokenId must not be null");

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/ProtocolLayerClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public TokenAssociateResult executeTokenAssociateTransaction(@NonNull final Toke
455455
final TokenAssociateTransaction transaction = new TokenAssociateTransaction()
456456
.setMaxTransactionFee(request.maxTransactionFee())
457457
.setTransactionValidDuration(request.transactionValidDuration())
458-
.setTokenIds(List.of(request.tokenId()))
458+
.setTokenIds(request.tokenIds())
459459
.setAccountId(request.accountId());
460460
sign(transaction, request.accountPrivateKey());
461461
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/protocol/data/TokenAssociateRequest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55
import com.hedera.hashgraph.sdk.PrivateKey;
66
import com.hedera.hashgraph.sdk.TokenId;
77
import java.time.Duration;
8+
import java.util.List;
89
import java.util.Objects;
910
import org.jspecify.annotations.NonNull;
1011

1112
public record TokenAssociateRequest(@NonNull Hbar maxTransactionFee,
1213
@NonNull Duration transactionValidDuration,
13-
@NonNull TokenId tokenId,
14+
@NonNull List<TokenId> tokenIds,
1415
@NonNull AccountId accountId,
1516
@NonNull PrivateKey accountPrivateKey) implements TransactionRequest {
1617

1718
public TokenAssociateRequest {
18-
Objects.requireNonNull(tokenId, "tokenId must not be null");
19-
Objects.requireNonNull(accountId, "newAccountId must not be null");
19+
Objects.requireNonNull(tokenIds, "tokenIds must not be null");
20+
Objects.requireNonNull(accountId, "accountId must not be null");
2021
Objects.requireNonNull(accountPrivateKey, "accountPrivateKey must not be null");
2122
}
2223

2324
public static TokenAssociateRequest of(@NonNull final TokenId tokenId, @NonNull final AccountId accountId,
25+
@NonNull final PrivateKey accountPrivateKey) {
26+
return of(List.of(tokenId), accountId, accountPrivateKey);
27+
}
28+
29+
public static TokenAssociateRequest of(@NonNull final List<TokenId> tokenIds, @NonNull final AccountId accountId,
2430
@NonNull final PrivateKey accountPrivateKey) {
2531
return new TokenAssociateRequest(TransactionRequest.DEFAULT_MAX_TRANSACTION_FEE,
26-
TransactionRequest.DEFAULT_TRANSACTION_VALID_DURATION, tokenId, accountId, accountPrivateKey);
32+
TransactionRequest.DEFAULT_TRANSACTION_VALID_DURATION, tokenIds, accountId, accountPrivateKey);
2733
}
28-
2934
}

0 commit comments

Comments
 (0)