import { useState } from 'react'; import type { NextPage } from 'next'; import Head from 'next/head'; import styles from '@styles/modules/mint/mint.module.css'; import NavModalBreadcrumbs from '@components/mint'; import Card from '@components/Card'; import NFTDemoContract from '@/compiled-contracts/NFTDemoContract.json'; import { web3 } from '@utils/mint'; import { AbiItem } from 'web3-utils'; import TextInput from '@components/TextInput'; import PrimaryButton from '@components/PrimaryButton'; import ReadOnly from '@components/ReadOnly'; import Link from '@components/Link'; import ChevronRight from '@components/icons/ChevronRight'; import axios, { AxiosError } from 'axios'; import { Transaction } from 'web3-core'; const Mint: NextPage = () => { const [settingsModalIsOpen, setSettingsModalIsOpen] = useState(false); const [error, setError] = useState(''); const [tokenURI, setTokenURI] = useState(''); const [isLoading, setIsLoading] = useState(false); const [transactionHash, setTransactionHash] = useState(''); const [transactionDetails, setTransactionDetails] = useState(); const submitForMinting = async (): Promise => { try { setIsLoading(true); const receipt = await performMinting(); setTransactionHash(receipt); const tx = await fetchTransaction(receipt); setTransactionDetails(tx); setIsLoading(false); } catch (err) { console.log(err); setError('Unexpected error occured, please try again.'); setIsLoading(false); } }; const performMinting = async (): Promise => { const { ethereum } = window; if (!ethereum) { throw 'We could not detect your wallet. Please download the MetaMask extension.'; } const smartContractAddress = process.env.NEXT_PUBLIC_SMART_CONTRACT_ADDRESS; await ethereum.request({ method: 'eth_requestAccounts', }); const nftContract = new web3.eth.Contract( NFTDemoContract.abi as AbiItem[], smartContractAddress ); try { const tx = { to: smartContractAddress, from: ethereum.selectedAddress, gas: '200000', data: nftContract.methods .mint(ethereum.selectedAddress, tokenURI) .encodeABI(), }; const receipt = await ethereum.request({ method: 'eth_sendTransaction', params: [tx], }); return receipt; } catch (err) { throw 'Oops, something went wrong minting your NFT.'; } }; const fetchTransaction = async (hash: string): Promise => { try { const response = await axios.post('/api/transactions/getTransaction', { transactionHash: hash, }); return response.data?.transaction; } catch (err) { const stdErr = 'Oops, something went wrong fetching the transaction details.'; if (axios.isAxiosError(err)) { const axiosErr = err as AxiosError; throw axiosErr?.response?.data.message || stdErr; } else { throw stdErr; } } }; const showSuccessView = (): boolean => { return transactionHash !== '' && transactionDetails !== undefined; }; const addTokenForm = () => { return (

Mint NFT

setTokenURI(e.currentTarget.value)} containerClassName={styles['mint__input-row']} labelClassName="bg-[#121212]" /> submitForMinting()} disabled={isLoading || tokenURI === ''} isLoading={isLoading} > Submit
); }; const mintSuccessView = () => { return (

Minting Successful

View Transaction on Etherscan View your Smart Contract on Etherscan View your NFT Collection on OpenSea
); }; return (
Mint an NFT {!showSuccessView() ? addTokenForm() : mintSuccessView()} {error &&

{error}

}
); }; export default Mint;