From ae38f5e6c3d0c42edd29fa6f57ff803c36a44f98 Mon Sep 17 00:00:00 2001 From: Anastasios Date: Fri, 15 Nov 2024 14:43:58 +0400 Subject: [PATCH] feat: fetch stacks name owner by bns name --- .../query/src/stacks/bns/bns-v2-client.ts | 8 ++++++++ packages/query/src/stacks/bns/bns.schemas.ts | 19 +++++++++++++++++++ .../query/src/stacks/bns/bns.utils.spec.ts | 1 + packages/query/src/stacks/bns/bns.utils.ts | 5 +++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/query/src/stacks/bns/bns-v2-client.ts b/packages/query/src/stacks/bns/bns-v2-client.ts index c7677e579..5f8409134 100644 --- a/packages/query/src/stacks/bns/bns-v2-client.ts +++ b/packages/query/src/stacks/bns/bns-v2-client.ts @@ -6,8 +6,10 @@ import { whenNetwork } from '@leather.io/utils'; import { useLeatherNetwork } from '../../leather-query-provider'; import { + BnsV2NameDataByNameResponse, BnsV2NamesByAddressResponse, BnsV2ZoneFileDataResponse, + bnsV2NameDataByNameResponseSchema, bnsV2NamesByAddressResponseSchema, bnsV2ZoneFileDataSchema, } from './bns.schemas'; @@ -33,6 +35,12 @@ export function bnsV2Client(basePath = BNS_V2_API_BASE_URL) { ); return bnsV2ZoneFileDataSchema.parse(resp.data.zonefile); }, + async getBnsNameDataByName(bnsName: string, signal?: AbortSignal) { + const resp = await axios.get(`${basePath}/names/${bnsName}`, { + signal, + }); + return bnsV2NameDataByNameResponseSchema.parse(resp.data); + }, }; } diff --git a/packages/query/src/stacks/bns/bns.schemas.ts b/packages/query/src/stacks/bns/bns.schemas.ts index b29be8cb9..b37c6b2a7 100644 --- a/packages/query/src/stacks/bns/bns.schemas.ts +++ b/packages/query/src/stacks/bns/bns.schemas.ts @@ -39,3 +39,22 @@ export const bnsV2ZoneFileResponseSchema = z.object({ }); export type BnsV2ZoneFileDataResponse = z.infer; + +export const bnsV2NameDataByNameResponseSchema = z.object({ + current_burn_block: z.number(), + status: z.string(), + data: z.object({ + name_string: z.string(), + namespace_string: z.string(), + full_name: z.string(), + owner: z.string(), + registered_at: z.string(), + renewal_height: z.string(), + stx_burn: z.string(), + revoked: z.boolean(), + imported_at: z.string().nullable(), + is_valid: z.boolean(), + }), +}); + +export type BnsV2NameDataByNameResponse = z.infer; diff --git a/packages/query/src/stacks/bns/bns.utils.spec.ts b/packages/query/src/stacks/bns/bns.utils.spec.ts index e7263ccb8..65fcbe908 100644 --- a/packages/query/src/stacks/bns/bns.utils.spec.ts +++ b/packages/query/src/stacks/bns/bns.utils.spec.ts @@ -13,6 +13,7 @@ describe('bns.utils', () => { const mockClient = { getNamesByAddress: vi.fn(), getZoneFileData: vi.fn(), + getBnsNameDataByName: vi.fn(), }; beforeEach(() => { diff --git a/packages/query/src/stacks/bns/bns.utils.ts b/packages/query/src/stacks/bns/bns.utils.ts index 360ad958a..f80943bb3 100644 --- a/packages/query/src/stacks/bns/bns.utils.ts +++ b/packages/query/src/stacks/bns/bns.utils.ts @@ -87,13 +87,14 @@ export async function fetchBtcNameOwner( } } +// For stacks names we don't need to fetch the zonefile export async function fetchStacksNameOwner( client: BnsV2Client, bnsName: string ): Promise { try { - const zoneFileData = await client.getZoneFileData(bnsName); - return zoneFileData.owner ?? null; + const zoneFileData = await client.getBnsNameDataByName(bnsName); + return zoneFileData.data.owner ?? null; } catch (error) { // Name not found or invalid zonefile return null;