icns-frontend/queries/icns.ts

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-12-15 10:06:44 +00:00
import { request } from "../utils/url";
import {
2022-12-15 15:43:18 +00:00
NAME_NFT_ADDRESS,
2022-12-15 10:06:44 +00:00
REGISTRAR_ADDRESS,
RESOLVER_ADDRESS,
REST_URL,
} from "../constants/icns";
import { Buffer } from "buffer/";
import {
AddressesQueryResponse,
NameByTwitterIdQueryResponse,
2022-12-15 15:43:18 +00:00
OwnerOfQueryResponse,
2022-12-15 10:06:44 +00:00
QueryError,
} from "../types";
const getCosmwasmQueryUrl = (contractAddress: string, queryMsg: string) =>
`${REST_URL}/cosmwasm/wasm/v1/contract/${contractAddress}/smart/${queryMsg}`;
export const queryRegisteredTwitterId = async (
twitterId: string,
): Promise<NameByTwitterIdQueryResponse | QueryError> => {
const msg = {
name_by_twitter_id: { twitter_id: twitterId },
};
2022-12-15 15:43:18 +00:00
2022-12-15 10:06:44 +00:00
return request<NameByTwitterIdQueryResponse>(
getCosmwasmQueryUrl(
REGISTRAR_ADDRESS,
Buffer.from(JSON.stringify(msg)).toString("base64"),
),
2022-12-15 15:43:18 +00:00
{},
2022-12-20 14:09:26 +00:00
{ isErrorIgnore: true },
2022-12-15 10:06:44 +00:00
);
};
export const queryAddressesFromTwitterName = async (
twitterUsername: string,
): Promise<AddressesQueryResponse> => {
const msg = {
addresses: { name: twitterUsername },
};
return request<AddressesQueryResponse>(
getCosmwasmQueryUrl(
RESOLVER_ADDRESS,
Buffer.from(JSON.stringify(msg)).toString("base64"),
),
);
};
2022-12-15 15:43:18 +00:00
export const queryOwnerOfTwitterName = async (
twitterUsername: string,
): Promise<OwnerOfQueryResponse> => {
const msg = {
owner_of: { token_id: twitterUsername },
};
return request<OwnerOfQueryResponse>(
getCosmwasmQueryUrl(
NAME_NFT_ADDRESS,
Buffer.from(JSON.stringify(msg)).toString("base64"),
),
{},
2022-12-20 14:09:26 +00:00
{ isErrorIgnore: true },
2022-12-15 15:43:18 +00:00
);
};