Skip to content

Commit db2ef08

Browse files
feat(TCK): implement TokenInfoQuery endpoint (#3593)
Signed-off-by: Mario Dimitrov <mario.dimitrov@limechain.tech>
1 parent f102572 commit db2ef08

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

tck/methods/token.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
PendingAirdropId,
2424
TokenRejectTransaction,
2525
TokenCancelAirdropTransaction,
26+
TokenInfoQuery,
27+
Hbar,
2628
} from "@hiero-ledger/sdk";
2729
import Long from "long";
2830

@@ -35,6 +37,7 @@ import {
3537
configureTokenManagementTransaction,
3638
createCustomFees,
3739
executeTokenManagementTransaction,
40+
mapTokenInfoResponse,
3841
} from "../utils/helpers/token";
3942

4043
import { applyCommonTransactionParams } from "../params/common-tx-params";
@@ -54,12 +57,14 @@ import {
5457
AirdropCancelTokenParams,
5558
AirdropClaimTokenParams,
5659
RejectTokenParams,
60+
GetTokenInfoParams,
5761
} from "../params/token";
5862

5963
import {
6064
TokenResponse,
6165
TokenBurnResponse,
6266
TokenMintResponse,
67+
TokenInfoQueryResponse,
6368
} from "../response/token";
6469

6570
// buildCreateToken builds a TokenCreateTransaction from parameters
@@ -860,3 +865,38 @@ export const cancelAirdrop = async ({
860865
status: receipt.status.toString(),
861866
};
862867
};
868+
869+
export const getTokenInfo = async ({
870+
tokenId,
871+
queryPayment,
872+
maxQueryPayment,
873+
getCost,
874+
sessionId,
875+
}: GetTokenInfoParams): Promise<TokenInfoQueryResponse> => {
876+
const client = sdk.getClient(sessionId);
877+
const query = new TokenInfoQuery().setGrpcDeadline(DEFAULT_GRPC_DEADLINE);
878+
879+
if (tokenId != null) {
880+
query.setTokenId(tokenId);
881+
}
882+
883+
if (queryPayment != null) {
884+
query.setQueryPayment(Hbar.fromTinybars(queryPayment));
885+
}
886+
887+
if (maxQueryPayment != null) {
888+
query.setMaxQueryPayment(Hbar.fromTinybars(maxQueryPayment));
889+
}
890+
891+
if (getCost) {
892+
const cost = await query.getCost(client);
893+
894+
return {
895+
cost: cost.toTinybars().toString(),
896+
};
897+
}
898+
899+
const response = await query.execute(client);
900+
901+
return mapTokenInfoResponse(response);
902+
};

tck/params/token.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseTransactionParams } from "./base";
1+
import { BaseParams, BaseTransactionParams } from "./base";
22

33
import {
44
CustomFixedFee,
@@ -137,3 +137,10 @@ export interface RejectTokenParams extends BaseTransactionParams {
137137
readonly nftIds?: string[];
138138
readonly serialNumbers?: string[];
139139
}
140+
141+
export interface GetTokenInfoParams extends BaseParams {
142+
readonly tokenId?: string;
143+
readonly queryPayment?: string;
144+
readonly maxQueryPayment?: string;
145+
readonly getCost?: boolean;
146+
}

tck/response/token.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,35 @@ export interface TokenBurnResponse {
1515
readonly newTotalSupply?: string;
1616
readonly status: string;
1717
}
18+
19+
export interface TokenInfoQueryResponse {
20+
readonly tokenId?: string;
21+
readonly name?: string;
22+
readonly symbol?: string;
23+
readonly decimals?: number;
24+
readonly totalSupply?: string;
25+
readonly treasuryAccountId?: string;
26+
readonly adminKey?: string;
27+
readonly kycKey?: string;
28+
readonly freezeKey?: string;
29+
readonly pauseKey?: string;
30+
readonly wipeKey?: string;
31+
readonly supplyKey?: string;
32+
readonly feeScheduleKey?: string;
33+
readonly metadataKey?: string;
34+
readonly defaultFreezeStatus?: boolean | null;
35+
readonly defaultKycStatus?: boolean | null;
36+
readonly pauseStatus?: string;
37+
readonly isDeleted?: boolean;
38+
readonly autoRenewAccountId?: string;
39+
readonly autoRenewPeriod?: string;
40+
readonly expirationTime?: string;
41+
readonly tokenMemo?: string;
42+
readonly customFees?: any[];
43+
readonly tokenType?: string;
44+
readonly supplyType?: string;
45+
readonly maxSupply?: string;
46+
readonly metadata?: string;
47+
readonly ledgerId?: string;
48+
readonly cost?: string;
49+
}

tck/utils/helpers/token.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
CustomRoyaltyFee,
88
FeeAssessmentMethod,
99
Transaction,
10+
TokenInfo,
1011
} from "@hiero-ledger/sdk";
1112
import Long from "long";
1213

1314
import { DEFAULT_GRPC_DEADLINE } from "../../utils/constants/config";
1415

1516
import { applyCommonTransactionParams } from "../../params/common-tx-params";
17+
import { TokenInfoQueryResponse } from "../../response/token";
1618

1719
export const executeTokenManagementTransaction = async (
1820
transaction: Transaction,
@@ -144,3 +146,103 @@ export const createCustomFees = (customFees: Array<Record<string, any>>) => {
144146

145147
return customFeeList;
146148
};
149+
150+
// Helper function to map TokenInfo to TokenInfoQueryResponse
151+
export const mapTokenInfoResponse = (
152+
info: TokenInfo,
153+
): TokenInfoQueryResponse => {
154+
// Helper function to convert pause status
155+
const getPauseStatusString = (pauseStatus: boolean | null): string => {
156+
if (pauseStatus === null) {
157+
return "NOT_APPLICABLE";
158+
}
159+
return pauseStatus ? "PAUSED" : "UNPAUSED";
160+
};
161+
162+
// Helper function to serialize custom fees
163+
const serializeCustomFees = (customFees: CustomFee[]): any[] => {
164+
return customFees.map((fee) => {
165+
if (fee instanceof CustomFixedFee) {
166+
return {
167+
feeCollectorAccountId:
168+
fee.feeCollectorAccountId?.toString(),
169+
allCollectorsAreExempt: fee.allCollectorsAreExempt,
170+
fixedFee: {
171+
amount: fee.amount?.toString(),
172+
denominatingTokenId:
173+
fee.denominatingTokenId?.toString() || null,
174+
},
175+
};
176+
} else if (fee instanceof CustomFractionalFee) {
177+
return {
178+
feeCollectorAccountId:
179+
fee.feeCollectorAccountId?.toString(),
180+
allCollectorsAreExempt: fee.allCollectorsAreExempt,
181+
fractionalFee: {
182+
numerator: fee.numerator.toString(),
183+
denominator: fee.denominator.toString(),
184+
minimumAmount: fee.min?.toString() || "0",
185+
maximumAmount: fee.max?.toString() || "0",
186+
assessmentMethod:
187+
fee.assessmentMethod?.toString().toLowerCase() ||
188+
"inclusive",
189+
},
190+
};
191+
} else if (fee instanceof CustomRoyaltyFee) {
192+
const result: any = {
193+
feeCollectorAccountId:
194+
fee.feeCollectorAccountId?.toString(),
195+
allCollectorsAreExempt: fee.allCollectorsAreExempt,
196+
royaltyFee: {
197+
numerator: fee.numerator.toString(),
198+
denominator: fee.denominator.toString(),
199+
fallbackFee: fee.fallbackFee
200+
? {
201+
amount: fee.fallbackFee.amount?.toString(),
202+
denominatingTokenId:
203+
fee.fallbackFee.denominatingTokenId?.toString() ||
204+
null,
205+
}
206+
: null,
207+
},
208+
};
209+
return result;
210+
}
211+
return fee;
212+
});
213+
};
214+
215+
return {
216+
tokenId: info.tokenId?.toString(),
217+
name: info.name,
218+
symbol: info.symbol,
219+
decimals: info.decimals,
220+
totalSupply: info.totalSupply?.toString(),
221+
treasuryAccountId: info.treasuryAccountId?.toString(),
222+
adminKey: info.adminKey?.toString(),
223+
kycKey: info.kycKey?.toString(),
224+
freezeKey: info.freezeKey?.toString(),
225+
pauseKey: info.pauseKey?.toString(),
226+
wipeKey: info.wipeKey?.toString(),
227+
supplyKey: info.supplyKey?.toString(),
228+
feeScheduleKey: info.feeScheduleKey?.toString(),
229+
metadataKey: info.metadataKey?.toString(),
230+
defaultFreezeStatus: info.defaultFreezeStatus,
231+
defaultKycStatus: info.defaultKycStatus,
232+
pauseStatus: getPauseStatusString(info.pauseStatus),
233+
isDeleted: info.isDeleted,
234+
autoRenewAccountId: info.autoRenewAccountId?.toString(),
235+
autoRenewPeriod: info.autoRenewPeriod?.seconds.toString(),
236+
expirationTime: info.expirationTime?.seconds.toString(),
237+
tokenMemo: info.tokenMemo,
238+
customFees: serializeCustomFees(info.customFees),
239+
tokenType: info.tokenType?.toString(),
240+
supplyType: info.supplyType?.toString(),
241+
maxSupply: info.maxSupply?.toString(),
242+
metadata:
243+
info.metadata && info.metadata.length > 0
244+
? Buffer.from(info.metadata).toString("hex")
245+
: "",
246+
ledgerId: info.ledgerId?.toString(),
247+
};
248+
};

0 commit comments

Comments
 (0)