/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable no-nested-ternary */ import { coin } from '@cosmjs/proto-signing' import { Alert } from 'components/Alert' import { Button } from 'components/Button' import { Conditional } from 'components/Conditional' import { ContractPageHeader } from 'components/ContractPageHeader' import { FormControl } from 'components/FormControl' import { FormGroup } from 'components/FormGroup' import { AddressList } from 'components/forms/AddressList' import { useAddressListState } from 'components/forms/AddressList.hooks' import { NumberInput } from 'components/forms/FormInput' import { useNumberInputState } from 'components/forms/FormInput.hooks' import { InputDateTime } from 'components/InputDateTime' import { JsonPreview } from 'components/JsonPreview' import { LinkTabs } from 'components/LinkTabs' import { whitelistLinkTabs } from 'components/LinkTabs.data' import { type WhitelistFlexMember, WhitelistFlexUpload } from 'components/WhitelistFlexUpload' import { WhitelistUpload } from 'components/WhitelistUpload' import { vendingMinterList } from 'config/minter' import type { TokenInfo } from 'config/token' import { stars, tokensList } from 'config/token' import { useContracts } from 'contexts/contracts' import { useGlobalSettings } from 'contexts/globalSettings' import type { InstantiateResponse } from 'contracts/sg721' import type { NextPage } from 'next' import { NextSeo } from 'next-seo' import { type FormEvent, useEffect, useState } from 'react' import { toast } from 'react-hot-toast' import { FaAsterisk } from 'react-icons/fa' import { useMutation } from 'react-query' import { isValidAddress } from 'utils/isValidAddress' import { withMetadata } from 'utils/layout' import { links } from 'utils/links' import { useWallet } from 'utils/wallet' import { WHITELIST_CODE_ID, WHITELIST_FLEX_CODE_ID } from '../../../utils/constants' const WhitelistInstantiatePage: NextPage = () => { const wallet = useWallet() const { whitelist: contract } = useContracts() const { timezone } = useGlobalSettings() const [startDate, setStartDate] = useState(undefined) const [endDate, setEndDate] = useState(undefined) const [adminsMutable, setAdminsMutable] = useState(true) const [whitelistType, setWhitelistType] = useState<'standard' | 'flex'>('standard') const [whitelistStandardArray, setWhitelistStandardArray] = useState([]) const [whitelistFlexArray, setWhitelistFlexArray] = useState([]) const [selectedMintToken, setSelectedMintToken] = useState(stars) const unitPriceState = useNumberInputState({ id: 'unit-price', name: 'unitPrice', title: 'Unit Price', subtitle: 'Price of each tokens in collection', placeholder: '500', }) const memberLimitState = useNumberInputState({ id: 'member-limit', name: 'memberLimit', title: 'Member Limit', subtitle: 'Limit of the whitelisted members', placeholder: '1000', }) const perAddressLimitState = useNumberInputState({ id: 'per-address-limit', name: 'perAddressLimit', title: 'Per Address Limit', subtitle: 'Limit of tokens per address', placeholder: '5', }) const whaleCapState = useNumberInputState({ id: 'whale-cap', name: 'whaleCap', title: 'Whale Cap (optional)', subtitle: 'Maximum number of tokens a single address can mint', }) const addressListState = useAddressListState() const { data, isLoading, mutate } = useMutation( async (event: FormEvent): Promise => { event.preventDefault() if (!contract) { throw new Error('Smart contract connection failed') } if (!startDate) { throw new Error('Start date is required') } if (!endDate) { throw new Error('End date is required') } const standardMsg = { members: whitelistStandardArray, start_time: (startDate.getTime() * 1_000_000).toString(), end_time: (endDate.getTime() * 1_000_000).toString(), mint_price: coin(String(Number(unitPriceState.value) * 1000000), selectedMintToken?.denom || 'ustars'), per_address_limit: perAddressLimitState.value, member_limit: memberLimitState.value, admins: [ ...new Set( addressListState.values .map((a) => a.address.trim()) .filter((address) => address !== '' && isValidAddress(address.trim()) && address.startsWith('stars')), ), ] || [wallet.address], admins_mutable: adminsMutable, } const flexMsg = { members: whitelistFlexArray, start_time: (startDate.getTime() * 1_000_000).toString(), end_time: (endDate.getTime() * 1_000_000).toString(), mint_price: coin(String(Number(unitPriceState.value) * 1000000), selectedMintToken?.denom || 'ustars'), whale_cap: whaleCapState.value || undefined, member_limit: memberLimitState.value, admins: [ ...new Set( addressListState.values .map((a) => a.address.trim()) .filter((address) => address !== '' && isValidAddress(address.trim()) && address.startsWith('stars')), ), ] || [wallet.address], admins_mutable: adminsMutable, } return toast.promise( contract.instantiate( whitelistType === 'standard' ? WHITELIST_CODE_ID : WHITELIST_FLEX_CODE_ID, whitelistType === 'standard' ? standardMsg : flexMsg, whitelistType === 'standard' ? 'Stargaze Whitelist Contract' : 'Stargaze Whitelist Flex Contract', wallet.address, ), { loading: 'Instantiating contract...', error: 'Instantiation failed!', success: 'Instantiation success!', }, ) }, { onError: (error) => { toast.error(String(error), { style: { maxWidth: 'none' } }) }, }, ) const whitelistFileOnChange = (whitelistData: string[]) => { setWhitelistStandardArray(whitelistData) } const whitelistFlexFileOnChange = (whitelistData: WhitelistFlexMember[]) => { setWhitelistFlexArray(whitelistData) } useEffect(() => { setWhitelistStandardArray([]) setWhitelistFlexArray([]) }, [whitelistType]) return (
{ setWhitelistType('standard') }} type="radio" value="nft-storage" />
{ setWhitelistType('flex') }} type="radio" value="flex" />
Instantiate success! Here is the transaction result containing the contract address and the transaction hash.
0}> 0}>
setStartDate( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? startDate : startDate ? new Date(startDate.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } /> setEndDate( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? endDate : endDate ? new Date(endDate.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } />
) } export default withMetadata(WhitelistInstantiatePage, { center: false })