Fetch proof hashes for hasMember query
This commit is contained in:
parent
43fd0d7848
commit
41e8a3961a
@ -1,4 +1,5 @@
|
|||||||
/* eslint-disable eslint-comments/disable-enable-pair */
|
/* eslint-disable eslint-comments/disable-enable-pair */
|
||||||
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
||||||
/* eslint-disable no-nested-ternary */
|
/* eslint-disable no-nested-ternary */
|
||||||
|
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
@ -32,6 +33,7 @@ import { NextSeo } from 'next-seo'
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { toast } from 'react-hot-toast'
|
import { toast } from 'react-hot-toast'
|
||||||
import { useQuery } from 'react-query'
|
import { useQuery } from 'react-query'
|
||||||
|
import { WHITELIST_MERKLE_TREE_API_URL } from 'utils/constants'
|
||||||
import { useDebounce } from 'utils/debounce'
|
import { useDebounce } from 'utils/debounce'
|
||||||
import { withMetadata } from 'utils/layout'
|
import { withMetadata } from 'utils/layout'
|
||||||
import { links } from 'utils/links'
|
import { links } from 'utils/links'
|
||||||
@ -44,6 +46,7 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
const wallet = useWallet()
|
const wallet = useWallet()
|
||||||
const [exporting, setExporting] = useState(false)
|
const [exporting, setExporting] = useState(false)
|
||||||
const [whitelistType, setWhitelistType] = useState<WhitelistType>('standard')
|
const [whitelistType, setWhitelistType] = useState<WhitelistType>('standard')
|
||||||
|
const [proofHashes, setProofHashes] = useState<string[]>([])
|
||||||
|
|
||||||
const contractState = useInputState({
|
const contractState = useInputState({
|
||||||
id: 'contract-address',
|
id: 'contract-address',
|
||||||
@ -101,6 +104,47 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
})
|
})
|
||||||
const address = addressState.value
|
const address = addressState.value
|
||||||
|
|
||||||
|
const debouncedAddress = useDebounce(address, 300)
|
||||||
|
|
||||||
|
const fetchProofHashes = async (whitelistContractAddress: string, memberAddress: string): Promise<string[]> => {
|
||||||
|
if (whitelistContractAddress.length === 0 || memberAddress.length === 0)
|
||||||
|
throw new Error('Contract or member address is empty.')
|
||||||
|
const resolvedAddress = await resolveAddress(memberAddress, wallet)
|
||||||
|
const merkleRootResponse = await (
|
||||||
|
await wallet.getCosmWasmClient()
|
||||||
|
).queryContractSmart(contractAddress, { merkle_root: {} })
|
||||||
|
const proofs = await toast.promise(
|
||||||
|
fetch(`${WHITELIST_MERKLE_TREE_API_URL}/whitelist/${merkleRootResponse.merkle_root}/${resolvedAddress}`)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => data.proofs)
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e)
|
||||||
|
setProofHashes([])
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
loading: 'Fetching proof hashes...',
|
||||||
|
error: 'Error fetching proof hashes from Whitelist Merkle Tree API.',
|
||||||
|
success: 'Proof hashes fetched.',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return proofs as string[] | []
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
whitelistType === 'merkletree' &&
|
||||||
|
whitelistMerkleTreeQueryType === 'has_member' &&
|
||||||
|
debouncedAddress.length > 0
|
||||||
|
) {
|
||||||
|
void fetchProofHashes(contractAddress, debouncedAddress)
|
||||||
|
.then((proofs) => setProofHashes(proofs))
|
||||||
|
.catch((e) => {
|
||||||
|
console.log(e)
|
||||||
|
setProofHashes([])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [debouncedAddress])
|
||||||
|
|
||||||
const limit = useNumberInputState({
|
const limit = useNumberInputState({
|
||||||
id: 'limit',
|
id: 'limit',
|
||||||
name: 'limit',
|
name: 'limit',
|
||||||
@ -132,7 +176,7 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
const [whitelistMerkleTreeQueryType, setWhitelistMerkleTreeQueryType] =
|
const [whitelistMerkleTreeQueryType, setWhitelistMerkleTreeQueryType] =
|
||||||
useState<WhitelistMerkleTreeQueryType>('config')
|
useState<WhitelistMerkleTreeQueryType>('config')
|
||||||
|
|
||||||
const addressVisible = type === 'has_member'
|
const addressVisible = type === 'has_member' || whitelistMerkleTreeQueryType === 'has_member'
|
||||||
|
|
||||||
const { data: response } = useQuery(
|
const { data: response } = useQuery(
|
||||||
[
|
[
|
||||||
@ -145,6 +189,8 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
address,
|
address,
|
||||||
startAfter.value,
|
startAfter.value,
|
||||||
limit.value,
|
limit.value,
|
||||||
|
proofHashes,
|
||||||
|
whitelistType,
|
||||||
] as const,
|
] as const,
|
||||||
async ({ queryKey }) => {
|
async ({ queryKey }) => {
|
||||||
const [
|
const [
|
||||||
@ -157,6 +203,8 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
_address,
|
_address,
|
||||||
_startAfter,
|
_startAfter,
|
||||||
_limit,
|
_limit,
|
||||||
|
_proofHashes,
|
||||||
|
_whitelistType,
|
||||||
] = queryKey
|
] = queryKey
|
||||||
const messages = contract?.use(contractAddress)
|
const messages = contract?.use(contractAddress)
|
||||||
const whitelistMerkleTreeMessages = contractWhitelistMerkleTree?.use(contractAddress)
|
const whitelistMerkleTreeMessages = contractWhitelistMerkleTree?.use(contractAddress)
|
||||||
@ -169,6 +217,7 @@ const WhitelistQueryPage: NextPage = () => {
|
|||||||
address: resolvedAddress,
|
address: resolvedAddress,
|
||||||
type: whitelistMerkleTreeQueryType,
|
type: whitelistMerkleTreeQueryType,
|
||||||
limit: _limit,
|
limit: _limit,
|
||||||
|
proofHashes: _proofHashes,
|
||||||
})
|
})
|
||||||
: await dispatchQuery({
|
: await dispatchQuery({
|
||||||
messages,
|
messages,
|
||||||
|
Loading…
Reference in New Issue
Block a user