|
| 1 | +/** |
| 2 | + * Copyright (c) Hathor Labs and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { z } from 'zod'; |
| 9 | +import { createRequestInstance } from './axiosInstance'; |
| 10 | +import { transformJsonBigIntResponse } from '../utils/bigint'; |
| 11 | + |
| 12 | +export const featureActivationSchema = z |
| 13 | + .object({ |
| 14 | + name: z.string(), |
| 15 | + state: z.string(), |
| 16 | + acceptance: z.number().nullish(), |
| 17 | + threshold: z.number(), |
| 18 | + start_height: z.number(), |
| 19 | + minimum_activation_height: z.number(), |
| 20 | + timeout_height: z.number(), |
| 21 | + lock_in_on_timeout: z.boolean(), |
| 22 | + version: z.string(), |
| 23 | + }) |
| 24 | + .passthrough(); |
| 25 | + |
| 26 | +export const getFeaturesSchema = z |
| 27 | + .object({ |
| 28 | + block_hash: z.string(), |
| 29 | + block_height: z.number().min(0), |
| 30 | + features: z.array(featureActivationSchema), |
| 31 | + }) |
| 32 | + .passthrough(); |
| 33 | + |
| 34 | +export const errorSchema = z.object({ |
| 35 | + success: z.literal(false), |
| 36 | + error: z.string(), |
| 37 | +}); |
| 38 | + |
| 39 | +export const getBlockFeatureSignalBitSchema = z |
| 40 | + .object({ |
| 41 | + bit: z.number(), |
| 42 | + signal: z.number(), |
| 43 | + feature: z.string(), |
| 44 | + feature_state: z.string(), |
| 45 | + }) |
| 46 | + .passthrough(); |
| 47 | + |
| 48 | +export const getBlockFeaturesSuccessSchema = z |
| 49 | + .object({ |
| 50 | + signal_bits: z.array(getBlockFeatureSignalBitSchema), |
| 51 | + }) |
| 52 | + .passthrough(); |
| 53 | + |
| 54 | +export const getBlockFeaturesSchema = z.union([getBlockFeaturesSuccessSchema, errorSchema]); |
| 55 | + |
| 56 | +const featuresApi = { |
| 57 | + /** |
| 58 | + * Get feature activation information |
| 59 | + */ |
| 60 | + async getFeatures(): Promise<z.output<typeof getFeaturesSchema>> { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + // @ts-expect-error XXX: createRequestInstance resolve argument is not typed correctly |
| 63 | + createRequestInstance(resolve) |
| 64 | + .get(`feature`, { |
| 65 | + transformResponse: res => transformJsonBigIntResponse(res, getFeaturesSchema), |
| 66 | + }) |
| 67 | + .then( |
| 68 | + res => { |
| 69 | + resolve(res.data); |
| 70 | + }, |
| 71 | + res => { |
| 72 | + reject(res); |
| 73 | + } |
| 74 | + ); |
| 75 | + }); |
| 76 | + }, |
| 77 | + |
| 78 | + /** |
| 79 | + * Get block features information |
| 80 | + * @param blockHash Block id encoded as hex |
| 81 | + */ |
| 82 | + async getBlockFeatures(blockHash: string): Promise<z.output<typeof getBlockFeaturesSchema>> { |
| 83 | + return new Promise((resolve, reject) => { |
| 84 | + // @ts-expect-error XXX: createRequestInstance resolve argument is not typed correctly |
| 85 | + createRequestInstance(resolve) |
| 86 | + .get(`feature`, { |
| 87 | + params: { block: blockHash }, |
| 88 | + transformResponse: res => transformJsonBigIntResponse(res, getBlockFeaturesSchema), |
| 89 | + }) |
| 90 | + .then( |
| 91 | + res => { |
| 92 | + resolve(res.data); |
| 93 | + }, |
| 94 | + res => { |
| 95 | + reject(res); |
| 96 | + } |
| 97 | + ); |
| 98 | + }); |
| 99 | + }, |
| 100 | +}; |
| 101 | + |
| 102 | +export default featuresApi; |
0 commit comments