Skip to content

Commit

Permalink
feat: fetch stacks name owner by bns name
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Nov 15, 2024
1 parent 7361358 commit c0152c8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/query/src/stacks/bns/bns-v2-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<BnsV2NameDataByNameResponse>(`${basePath}/names/${bnsName}`, {
signal,
});
return bnsV2NameDataByNameResponseSchema.parse(resp.data);
},
};
}

Expand Down
19 changes: 19 additions & 0 deletions packages/query/src/stacks/bns/bns.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ export const bnsV2ZoneFileResponseSchema = z.object({
});

export type BnsV2ZoneFileDataResponse = z.infer<typeof bnsV2ZoneFileResponseSchema>;

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<typeof bnsV2NameDataByNameResponseSchema>;
5 changes: 3 additions & 2 deletions packages/query/src/stacks/bns/bns.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
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;
Expand Down

0 comments on commit c0152c8

Please sign in to comment.