From 3ec389fc39c700c9f3ec97bfb4da1a2a789be90a Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Tue, 14 Feb 2023 11:26:22 +0300 Subject: [PATCH] Update Badge Hub dashboard > Query --- contracts/badgeHub/contract.ts | 8 ++-- contracts/badgeHub/messages/query.ts | 15 ++++--- pages/contracts/badgeHub/query.tsx | 62 +++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/contracts/badgeHub/contract.ts b/contracts/badgeHub/contract.ts index 7d5a597..cd840fb 100644 --- a/contracts/badgeHub/contract.ts +++ b/contracts/badgeHub/contract.ts @@ -63,7 +63,7 @@ export interface BadgeHubInstance { getBadge: (id: number) => Promise getBadges: (start_after?: number, limit?: number) => Promise getKey: (id: number, pubkey: string) => Promise - getKeys: (id: number, start_after?: number, limit?: number) => Promise + getKeys: (id: number, start_after?: string, limit?: number) => Promise //Execute createBadge: (senderAddress: string, badge: Badge) => Promise @@ -249,14 +249,14 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res } - const getKey = async (id: number, key: string): Promise => { + const getKey = async (id: number, pubkey: string): Promise => { const res = await client.queryContractSmart(contractAddress, { - key: { id, key }, + key: { id, pubkey }, }) return res } - const getKeys = async (id: number, start_after?: number, limit?: number): Promise => { + const getKeys = async (id: number, start_after?: string, limit?: number): Promise => { const res = await client.queryContractSmart(contractAddress, { keys: { id, start_after, limit }, }) diff --git a/contracts/badgeHub/messages/query.ts b/contracts/badgeHub/messages/query.ts index 8e5f0b4..cbfb4bc 100644 --- a/contracts/badgeHub/messages/query.ts +++ b/contracts/badgeHub/messages/query.ts @@ -13,9 +13,9 @@ export interface QueryListItem { export const QUERY_LIST: QueryListItem[] = [ { id: 'config', name: 'Config', description: 'View current config' }, { id: 'getBadge', name: 'Query Badge', description: 'Query a badge by ID' }, - { id: 'getBadges', name: 'Query a list of Badges', description: 'Query the list of badges' }, - { id: 'getKey', name: 'Query Key', description: 'Query a key by ID' }, - { id: 'getKeys', name: 'Query a list of Keys', description: 'Query the list of keys' }, + { id: 'getBadges', name: 'Query Badges', description: 'Query a list of badges' }, + { id: 'getKey', name: 'Query Key', description: 'Query a key by ID to see if it's whitelisted' }, + { id: 'getKeys', name: 'Query Keys', description: 'Query the list of whitelisted keys' }, ] export interface DispatchQueryProps { @@ -23,10 +23,13 @@ export interface DispatchQueryProps { pubkey: string messages: BadgeHubInstance | undefined type: QueryType + startAfterNumber: number + startAfterString: string + limit: number } export const dispatchQuery = (props: DispatchQueryProps) => { - const { id, pubkey, messages, type } = props + const { id, pubkey, messages, type, startAfterNumber, startAfterString, limit } = props switch (type) { case 'config': { return messages?.getConfig() @@ -35,13 +38,13 @@ export const dispatchQuery = (props: DispatchQueryProps) => { return messages?.getBadge(id) } case 'getBadges': { - return messages?.getBadges() + return messages?.getBadges(startAfterNumber, limit) } case 'getKey': { return messages?.getKey(id, pubkey) } case 'getKeys': { - return messages?.getKeys(id) + return messages?.getKeys(id, startAfterString, limit) } default: { throw new Error('unknown query type') diff --git a/pages/contracts/badgeHub/query.tsx b/pages/contracts/badgeHub/query.tsx index 3e812b6..30b9df7 100644 --- a/pages/contracts/badgeHub/query.tsx +++ b/pages/contracts/badgeHub/query.tsx @@ -2,7 +2,7 @@ import clsx from 'clsx' import { Conditional } from 'components/Conditional' import { ContractPageHeader } from 'components/ContractPageHeader' import { FormControl } from 'components/FormControl' -import { AddressInput, NumberInput } from 'components/forms/FormInput' +import { AddressInput, NumberInput, TextInput } from 'components/forms/FormInput' import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks' import { JsonPreview } from 'components/JsonPreview' import { LinkTabs } from 'components/LinkTabs' @@ -20,6 +20,8 @@ import { useQuery } from 'react-query' import { withMetadata } from 'utils/layout' import { links } from 'utils/links' +import { BADGE_HUB_ADDRESS } from '../../../utils/constants' + const BadgeHubQueryPage: NextPage = () => { const { badgeHub: contract } = useContracts() const wallet = useWallet() @@ -29,6 +31,7 @@ const BadgeHubQueryPage: NextPage = () => { name: 'contract-address', title: 'Badge Hub Address', subtitle: 'Address of the Badge Hub contract', + defaultValue: BADGE_HUB_ADDRESS, }) const contractAddress = contractState.value @@ -42,22 +45,57 @@ const BadgeHubQueryPage: NextPage = () => { const pubkeyState = useInputState({ id: 'pubkey', name: 'pubkey', - title: 'Pubkey', - subtitle: 'The public key for the badge', + title: 'Public Key', + subtitle: 'The public key to check whether it can be used to mint a badge', + }) + + const startAfterNumberState = useNumberInputState({ + id: 'start-after-number', + name: 'start-after-number', + title: 'Start After (optional)', + subtitle: 'The id to start the pagination after', + }) + + const startAfterStringState = useInputState({ + id: 'start-after-string', + name: 'start-after-string', + title: 'Start After (optional)', + subtitle: 'The public key to start the pagination after', + }) + + const paginationLimitState = useNumberInputState({ + id: 'pagination-limit', + name: 'pagination-limit', + title: 'Pagination Limit (optional)', + subtitle: 'The number of items to return (max: 30)', }) const [type, setType] = useState('config') const { data: response } = useQuery( - [contractAddress, type, contract, wallet, idState.value, pubkeyState.value] as const, + [ + contractAddress, + type, + contract, + wallet, + idState.value, + pubkeyState.value, + startAfterNumberState.value, + startAfterStringState.value, + paginationLimitState.value, + ] as const, async ({ queryKey }) => { - const [_contractAddress, _type, _contract, _wallet, id, pubkey] = queryKey + const [_contractAddress, _type, _contract, _wallet, id, pubkey, startAfterNumber, startAfterString, limit] = + queryKey const messages = contract?.use(_contractAddress) const result = await dispatchQuery({ id, pubkey, messages, type, + startAfterNumber, + startAfterString, + limit, }) return result }, @@ -114,9 +152,21 @@ const BadgeHubQueryPage: NextPage = () => { ))} - + + + + + + + + + + + + +