stargaze-studio/pages/collections/warmup.tsx

216 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-01-19 10:30:16 +00:00
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
2023-01-20 13:57:09 +00:00
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
2023-01-19 07:34:28 +00:00
import axios from 'axios'
import { Button } from 'components/Button'
import { ContractPageHeader } from 'components/ContractPageHeader'
import { AddressInput } from 'components/forms/FormInput'
import { useInputState } from 'components/forms/FormInput.hooks'
2023-01-31 14:14:58 +00:00
import type { AppConfig } from 'config'
import { getConfig } from 'config'
2023-01-19 07:34:28 +00:00
import type { NextPage } from 'next'
2023-01-19 09:12:57 +00:00
import { useRouter } from 'next/router'
2023-01-19 07:34:28 +00:00
import { NextSeo } from 'next-seo'
2023-01-19 10:30:16 +00:00
import { useEffect, useState } from 'react'
2023-01-19 07:34:28 +00:00
import { toast } from 'react-hot-toast'
2023-01-31 14:14:58 +00:00
import { NETWORK } from 'utils/constants'
2023-01-19 07:34:28 +00:00
import { withMetadata } from 'utils/layout'
import { links } from 'utils/links'
2023-01-20 13:57:09 +00:00
interface ConfigResponse {
num_tokens: number
base_token_uri: string
}
2023-01-19 10:30:16 +00:00
2023-01-19 07:34:28 +00:00
const CollectionQueriesPage: NextPage = () => {
2023-01-20 13:57:09 +00:00
const [client, setClient] = useState<CosmWasmClient>()
const [baseUri, SetBaseUri] = useState('')
2023-01-31 14:14:58 +00:00
const [doneCount, setDoneCount] = useState(0)
2023-01-19 09:12:57 +00:00
const [errorList, setErrorList] = useState<number[]>([])
2023-01-19 07:34:28 +00:00
const [numTokens, setNumTokens] = useState(0)
2023-01-20 13:57:09 +00:00
const [percentage, setPercentage] = useState('0.00')
2023-01-31 14:14:58 +00:00
const [isLoading, setIsLoading] = useState(false)
2023-01-19 07:34:28 +00:00
const minterContractState = useInputState({
id: 'minter-contract-address',
name: 'minter-contract-address',
2023-01-19 10:30:16 +00:00
title: 'Minter Contract Address',
defaultValue: '',
placeholder: 'stars1...',
2023-01-19 07:34:28 +00:00
})
const minterContractAddress = minterContractState.value
2023-01-19 09:12:57 +00:00
const router = useRouter()
2023-01-20 13:57:09 +00:00
useEffect(() => {
async function init() {
2023-01-31 14:14:58 +00:00
const config: AppConfig = getConfig(NETWORK)
setClient(await CosmWasmClient.connect(config.rpcUrl))
2023-01-20 13:57:09 +00:00
}
void init()
}, [])
useEffect(() => {
async function get() {
if (client && minterContractAddress) {
const res: ConfigResponse = await client.queryContractSmart(minterContractAddress, {
config: {},
})
setNumTokens(res.num_tokens)
SetBaseUri(res.base_token_uri)
setPercentage('0.00')
2023-01-31 14:14:58 +00:00
setDoneCount(0)
2023-01-20 13:57:09 +00:00
setErrorList([])
}
}
void get()
}, [minterContractAddress, client])
2023-01-19 09:12:57 +00:00
useEffect(() => {
2023-01-19 10:30:16 +00:00
if (minterContractAddress.length > 0) {
2023-01-19 09:12:57 +00:00
void router.replace({ query: { minterContractAddress } })
}
2023-01-19 10:30:16 +00:00
}, [minterContractAddress])
2023-01-19 09:12:57 +00:00
useEffect(() => {
const initial = new URL(document.URL).searchParams.get('minterContractAddress')
if (initial && initial.length > 0) minterContractState.onChange(initial)
}, [])
2023-01-19 07:34:28 +00:00
useEffect(() => {
2023-01-31 14:14:58 +00:00
if (doneCount === numTokens) setIsLoading(false)
if (numTokens !== 0) setPercentage(((doneCount / numTokens) * 100).toFixed(2))
else setPercentage('0.00')
}, [doneCount, numTokens])
2023-01-19 07:34:28 +00:00
function chooseAlternate(url: string, attempt: number) {
let alternate
switch (attempt) {
case 1: {
2023-01-19 09:12:57 +00:00
alternate = url.replace('ipfs://', 'https://cf-ipfs.com/ipfs/')
2023-01-19 07:34:28 +00:00
break
}
case 2: {
alternate = url.replace('ipfs://', 'https://ipfs.io/ipfs/')
break
}
case 3: {
2023-01-19 09:12:57 +00:00
alternate = url.replace('ipfs://', 'https://ipfs-gw.stargaze-apis.com/ipfs/')
2023-01-19 07:34:28 +00:00
break
}
default: {
2023-01-19 09:12:57 +00:00
alternate = url.replace('ipfs://', 'https://ipfs.stargaze.zone/ipfs/')
2023-01-19 07:34:28 +00:00
break
}
}
return alternate
}
async function warmOne(i: number, image: string, attempt: number, totalAttempt: number) {
const link = image.replace('1', i.toString())
try {
const result = await axios.get(chooseAlternate(link, attempt))
if (result.status === 200) {
2023-01-31 14:14:58 +00:00
setDoneCount((count) => {
return count + 1
2023-01-19 07:34:28 +00:00
})
return
}
} catch (e) {
2023-01-31 14:14:58 +00:00
toast.error(e as string)
2023-01-19 07:34:28 +00:00
}
if (totalAttempt !== 4) {
void warmOne(i, image, attempt === 3 ? 0 : attempt + 1, attempt === 3 ? totalAttempt + 1 : totalAttempt)
2023-01-19 09:12:57 +00:00
} else {
setErrorList((existingItems) => {
return [i, ...existingItems]
})
2023-01-31 14:14:58 +00:00
setDoneCount((count) => {
return count + 1
2023-01-19 09:12:57 +00:00
})
2023-01-19 07:34:28 +00:00
}
}
2023-01-31 14:14:58 +00:00
async function warmingProcess(url: string, attempt: number, err: boolean) {
2023-01-19 07:34:28 +00:00
const link = `${chooseAlternate(url, attempt)}/1`
try {
const { data, status } = await axios.get(link)
if (status === 200) {
2023-01-31 14:14:58 +00:00
if (!err) {
for (let i = 1; i <= numTokens; i++) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
void warmOne(i, data.image, 0, 0)
}
} else {
const list = errorList
setErrorList([])
list.forEach((i) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
void warmOne(i, data.image, 0, 0)
})
2023-01-19 07:34:28 +00:00
}
} else if (attempt < 3) {
2023-01-31 14:14:58 +00:00
void warmingProcess(url, attempt + 1, err)
2023-01-19 07:34:28 +00:00
} else toast.error('File can not be reachable at the moment! Please try again later...')
} catch (e) {
2023-01-31 14:14:58 +00:00
toast.error(e as string)
2023-01-19 07:34:28 +00:00
}
}
return (
<section className="py-6 px-12 space-y-4">
2023-01-19 10:30:16 +00:00
<NextSeo title="Collection Warm Up" />
2023-01-19 07:34:28 +00:00
<ContractPageHeader
2023-01-19 10:30:16 +00:00
description="Here you can pre-warm your collection for improved IPFS performance."
2023-01-19 07:34:28 +00:00
link={links.Documentation}
2023-01-19 10:30:16 +00:00
title="Collection Warm Up"
2023-01-19 07:34:28 +00:00
/>
<div className="space-y-8">
<AddressInput {...minterContractState} />
</div>
<div className="flex flex-row">
<div className="flex flex-col mr-20 w-4/5 text-xl border border-stargaze">
2023-01-20 13:57:09 +00:00
<div className="flex flex-row mt-2 w-full text-center">
2023-01-19 07:34:28 +00:00
<div className="w-1/3">Total</div>
2023-01-31 14:14:58 +00:00
<div className="w-1/3">Done</div>
<div className="w-1/3">Error</div>
2023-01-19 07:34:28 +00:00
<div className="w-1/3">Percentage</div>
</div>
<div className="flex flex-row w-full text-center">
<div className="w-1/3">{numTokens}</div>
2023-01-31 14:14:58 +00:00
<div className="w-1/3">{doneCount}</div>
<div className="w-1/3">{errorList.length}</div>
2023-01-20 13:57:09 +00:00
<div className="w-1/3">{percentage}%</div>
</div>
<div className="flex justify-center w-full">
<progress className="my-5 mx-2 w-4/5 h-2 progress" max="100" value={percentage} />
2023-01-19 07:34:28 +00:00
</div>
</div>
2023-01-31 14:14:58 +00:00
<div className="flex flex-row content-center p-10">
2023-01-19 07:34:28 +00:00
<Button
2023-01-20 13:57:09 +00:00
isDisabled={baseUri === ''}
2023-01-31 14:14:58 +00:00
isLoading={isLoading}
2023-01-19 07:34:28 +00:00
onClick={() => {
2023-01-31 14:14:58 +00:00
setIsLoading(true)
if (errorList.length === 0) {
setDoneCount(0)
setErrorList([])
void warmingProcess(baseUri, 0, false)
} else {
setDoneCount(doneCount - errorList.length)
void warmingProcess(baseUri, 0, true)
}
2023-01-19 07:34:28 +00:00
}}
>
2023-01-31 14:14:58 +00:00
{(numTokens === 0 || errorList.length === 0) && <span>Start</span>}
{numTokens !== 0 && errorList.length > 0 && <span>Start</span>}
2023-01-19 07:34:28 +00:00
</Button>
</div>
</div>
</section>
)
}
export default withMetadata(CollectionQueriesPage, { center: false })