From 877e680a9bea6a4750775997c6fcb4c90de3d411 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sat, 21 Jan 2023 23:12:58 -0800 Subject: [PATCH] setup minting --- src/App.js | 71 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/src/App.js b/src/App.js index 6c3b20a..7201046 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,14 @@ import { useState } from 'react'; -import { WagmiConfig, createClient, useAccount, useConnect, useDisconnect, useContractReads, configureChains } from 'wagmi'; +import { + WagmiConfig, createClient, useAccount, + useConnect, useDisconnect, useContractReads, + configureChains, useContractWrite, usePrepareContractWrite, + useWaitForTransaction +} from 'wagmi'; import { localhost, goerli } from 'wagmi/chains'; import { publicProvider } from 'wagmi/providers/public'; import { InjectedConnector } from 'wagmi/connectors/injected'; -import { ethers, getDefaultProvider } from 'ethers'; +import { ethers, BigNumber } from 'ethers'; import ABI from './abi.json'; import 'bulma/css/bulma.min.css'; @@ -36,12 +41,13 @@ function Contract() { phraseSet: false, mintAmount: 1, balance: 0, - hash: '' + hash: '', + txPending: false }); function handleStateChange(obj) { setOptions(preState => ({...preState , ...obj})); } - const { data, isError, isLoading } = useContractReads({ + useContractReads({ contracts: [ {..._contract, functionName: 'totalSupply'}, {..._contract, functionName: 'max'}, @@ -49,7 +55,7 @@ function Contract() { {..._contract, functionName: 'winners'}, {..._contract, functionName: 'winningAmount'}, {..._contract, functionName: 'phraseSet'}, - {..._contract, functionName: 'balanceOf'}, + {..._contract, functionName: 'balanceOf', args: [address]}, {..._contract, functionName: 'winningPhraseHash'}, ], watch: true, @@ -61,27 +67,55 @@ function Contract() { price: Number(data[2]), winners: Number(data[3]), winningAmount: Number(data[4]), - phraseSet: Number(data[5]), + phraseSet: data[5], balance: Number(data[6]), hash: data[7].toString() }); } }); - - if (isConnected) + const mintPrepare = usePrepareContractWrite({ + address: contractAddress, + abi: ABI, + enabled: isConnected && options.mintAmount > 0, + functionName: 'mint', + args: [options.mintAmount], + overrides: { + from: address, + value: BigNumber.from((options.price * options.mintAmount).toString()) + } + }); + const mintWrite = useContractWrite({ + ...mintPrepare.config, + onError(data) { + // if (data.message.startsWith('user rejected transaction')) NotificationManager.info(`tx cancelled`, 'ok', 4000); + mintWrite.reset(); + } + }); + useWaitForTransaction({ + hash: mintWrite.data?.hash, + enabled: mintWrite.status === 'success', + onSuccess() { + handleStateChange({txPending: false}); + mintWrite.reset(); + } + }); + if (isConnected && options.phraseSet) return ( <>

Price: {ethers.utils.formatEther((options.price * options.mintAmount).toString())} ETH

-
-
- { +
+
+ { if (v.target.value >= 0) handleStateChange({mintAmount: Number(v.target.value)})} }>
-
- + @@ -93,11 +127,17 @@ function Contract() {

Minted {options.totalSupply} / {options.max}

-
+
The secret phrase containing the pre-determined winners is: {options.hash}
) + if (isConnected && !options.phraseSet) + return ( + <> +

Not minting yet. Come back later.

+ + ) return } @@ -111,7 +151,8 @@ function App() { A Few Of Us Are Going To Make It.

- An NFT Lottery. Mint a token for your chance to win 1 ETH. 10 tokens are pre-determined to win. + An NFT Lottery. 10 tokens are pre-determined to win.
+ Mint one for your chance to win 1 ETH!