Skip to content

Commit 16ebc17

Browse files
authored
feat(1717): mirror node getexchangerate response validation (#1751)
Signed-off-by: rozekmichal <michal.rozek@blockydevs.com>
1 parent 98e9954 commit 16ebc17

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/core/services/mirrornode/__tests__/unit/mocks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ export const createMockExchangeRateResponse = (
205205
): ExchangeRateResponse => ({
206206
current_rate: {
207207
cent_equivalent: 12,
208-
expiration_time: '2024-01-01T12:00:00.000Z',
208+
expiration_time: 1774962000,
209209
hbar_equivalent: 1,
210210
},
211211
next_rate: {
212212
cent_equivalent: 12,
213-
expiration_time: '2024-01-01T12:00:00.000Z',
213+
expiration_time: 1774965600,
214214
hbar_equivalent: 1,
215215
},
216-
timestamp: '2024-01-01T12:00:00.000Z',
216+
timestamp: '1774958462.794936000',
217217
...overrides,
218218
});
219219

src/core/services/mirrornode/hedera-mirrornode-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { handleMirrorNodeErrorResponse } from '@/core/utils/handle-mirror-node-e
3737
import {
3838
AccountAPIResponseSchema,
3939
ContractInfoSchema,
40+
ExchangeRateResponseSchema,
4041
GetAccountsAPIResponseSchema,
4142
NftInfoSchema,
4243
TokenAirdropsResponseSchema,
@@ -567,7 +568,11 @@ export class HederaMirrornodeServiceDefaultImpl implements HederaMirrornodeServi
567568
);
568569
}
569570

570-
return (await response.json()) as ExchangeRateResponse;
571+
return parseWithSchema(
572+
ExchangeRateResponseSchema,
573+
await response.json(),
574+
'Mirror Node GET /network/exchangerate',
575+
);
571576
} catch (error) {
572577
if (error instanceof CliError) throw error;
573578
throw new NetworkError('Failed to fetch exchange rate', {

src/core/services/mirrornode/schemas.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type AccountListItemBalance,
99
type AccountListItemTokenBalance,
1010
type ContractInfo,
11+
type ExchangeRateResponse,
1112
type GetAccountsAPIResponse,
1213
MirrorNodeKeyType,
1314
type NftInfo,
@@ -231,6 +232,19 @@ export const ContractInfoSchema: z.ZodType<ContractInfo> = z.object({
231232
stake_period_start: nullableStringKey,
232233
});
233234

235+
const exchangeRateBandSchema = z.object({
236+
cent_equivalent: z.number(),
237+
expiration_time: z.number(),
238+
hbar_equivalent: z.number(),
239+
});
240+
241+
export const ExchangeRateResponseSchema: z.ZodType<ExchangeRateResponse> =
242+
z.object({
243+
current_rate: exchangeRateBandSchema,
244+
next_rate: exchangeRateBandSchema,
245+
timestamp: z.string(),
246+
});
247+
234248
const transactionTransferItemSchema: z.ZodType<TransactionTransferItem> =
235249
z.object({
236250
account: z.string(),

src/core/services/mirrornode/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ export interface TokenAirdropsResponse {
244244
export interface ExchangeRateResponse {
245245
current_rate: {
246246
cent_equivalent: number;
247-
expiration_time: string;
247+
expiration_time: number;
248248
hbar_equivalent: number;
249249
};
250250
next_rate: {
251251
cent_equivalent: number;
252-
expiration_time: string;
252+
expiration_time: number;
253253
hbar_equivalent: number;
254254
};
255255
timestamp: string;

0 commit comments

Comments
 (0)