Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 1617802

Browse files
BrianBrian
authored andcommitted
Added information during minting process for better user feedback
1 parent c7ca57a commit 1617802

File tree

3 files changed

+205
-45
lines changed

3 files changed

+205
-45
lines changed

frontend/_tests_/mocks/web3.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let defaultAccounts = [];
2+
let defaultNetwork = '1';
3+
let accounts = defaultAccounts.slice();
4+
let network = defaultNetwork;
5+
6+
const web3Mock = {
7+
eth: {
8+
accounts: {
9+
wallet: {
10+
length: 0,
11+
},
12+
},
13+
getAccounts: (cb) => cb(null, accounts),
14+
net: {
15+
getId: (cb) => cb(null, network),
16+
},
17+
},
18+
currentProvider: {
19+
enable: () => {
20+
return Promise.resolve(accounts);
21+
},
22+
},
23+
setNetwork: (v) => (network = v),
24+
setAccounts: (v) => {
25+
accounts = v;
26+
accounts.forEach((address, i) => {
27+
web3.eth.accounts.wallet[i] = {
28+
address: address,
29+
};
30+
});
31+
web3.eth.accounts.wallet.length = accounts.length;
32+
},
33+
restore: () => {
34+
accounts = defaultAccounts.slice();
35+
network = defaultNetwork;
36+
web3.eth.accounts.wallet = { length: 0 };
37+
},
38+
version: '1.0.0-beta.20',
39+
};
40+
41+
export default web3Mock;

frontend/_tests_/pages/mint.test.jsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import testCommonLink from '../utils/testCommons';
4+
import web3Mock from '../mocks/web3';
5+
6+
import MintPage from '../../src/pages/mint';
7+
8+
// Mocks
9+
10+
// - Wallet mock
11+
12+
// Tests
13+
14+
// - Expected Negative Interactions
15+
16+
// -- User unsuccessfully connects wallet
17+
18+
// -- Browser does not have web3 support
19+
describe('Browser does not support Web3', () => {
20+
it('Web3 not defined', () => {
21+
const originalEthereum = global.window.ethereum;
22+
render(<MintPage />);
23+
expect(originalEthereum).toBeUndefined();
24+
});
25+
});
26+
// -- Insufficient funds
27+
28+
// -- Transaction failed (could not connect)
29+
30+
// -- Token already minted
31+
32+
// - Expected Positive Interactions
33+
34+
// -- When user arrives on page mint button should be disabled, connect wallet should be enabled
35+
describe('Positive Interactions', () => {
36+
it('Render page default correctly', () => {
37+
render(<MintPage />);
38+
39+
const ddaoTokenSearch = screen.getByRole('link', {
40+
name: /here/,
41+
});
42+
43+
testCommonLink(ddaoTokenSearch, 'http://ddao.ibby.dev');
44+
45+
const connectWalletButton = screen.getByRole('button', {
46+
name: /connectWalletText/,
47+
});
48+
49+
// - [Button] - Connect Wallet
50+
expect(connectWalletButton).toBeInTheDocument();
51+
});
52+
});
53+
54+
// -- User Connects Wallet Successfully
55+
56+
// -- User sucessfully starts transacion to mint token
57+
58+
// -- User successfully mints token

frontend/src/components/DirectMint/DirectMint.tsx

Lines changed: 106 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ import React, {
44
useState,
55
useEffect,
66
} from 'react';
7-
import { useToast, Button, Input, Text } from '@chakra-ui/react';
7+
import {
8+
useToast,
9+
Button,
10+
Input,
11+
Text,
12+
Modal,
13+
ModalOverlay,
14+
ModalContent,
15+
ModalHeader,
16+
ModalBody,
17+
ModalCloseButton,
18+
useDisclosure,
19+
CircularProgress,
20+
Center,
21+
Link,
22+
Stack,
23+
} from '@chakra-ui/react';
24+
import { ExternalLinkIcon } from '@chakra-ui/icons';
825
import { ethers } from 'ethers';
926
import WalletConnectProvider from '@walletconnect/web3-provider';
1027
import Web3Modal from 'web3modal';
@@ -39,6 +56,10 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
3956
const [tokenID, setTokenID] = useState(developerId ? developerId : '');
4057
const [networkError, setNetworkError] = useState(false);
4158
const [web3Modal, setWeb3Modal] = useState<Web3Modal>();
59+
const [txInProgress, setTxInProgress] = useState(false);
60+
const [txReceipt, setTxReceipt] = useState('');
61+
62+
const { isOpen, onOpen, onClose } = useDisclosure();
4263

4364
const toast = useToast();
4465

@@ -55,7 +76,8 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
5576
const _web3 = new ethers.providers.Web3Provider(_provider);
5677
_signer = _web3.getSigner();
5778
mint_contract = new ethers.Contract(
58-
DEVELOPER_DAO_CONTRACT,
79+
//DEVELOPER_DAO_CONTRACT,
80+
'0xbf114c4cbb4e70e6098589a918c84292cb40602a',
5981
MINT_CONTRACT.abi,
6082
_signer,
6183
);
@@ -70,7 +92,8 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
7092
};
7193

7294
const _checkNetwork = (chainId: number) => {
73-
if (chainId === MAINNET_NETWORK_ID) {
95+
if (chainId === 4) {
96+
//MAINNET_NETWORK_ID) {
7497
return true;
7598
}
7699
setNetworkError(true);
@@ -121,18 +144,17 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
121144
) => {
122145
try {
123146
const tx = await mint_contract.claim(tokenID);
124-
125-
toast({
126-
title: t('transactionSending'),
127-
isClosable: true,
128-
});
147+
setTxInProgress(true);
148+
onOpen();
129149

130150
const receipt = await tx.wait();
151+
setTxReceipt(receipt.transactionHash);
131152

132153
if (receipt.status === 0) {
133154
throw new Error('Transaction failed');
134155
}
135156
} catch (error: any) {
157+
setTxInProgress(false);
136158
if (error.code === ERROR_CODE_TX_REJECTED_BY_USER) {
137159
toast({
138160
title: t('userCancelTransaction'),
@@ -151,16 +173,15 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
151173
setTokenID('');
152174
return;
153175
}
154-
155-
toast({
156-
title: t('TokenMintMessage'),
157-
description: t('NFTMintSuccess'),
158-
status: 'success',
159-
isClosable: true,
160-
});
161176
setTokenID('');
162177
};
163178

179+
const modalCloseHandler = () => {
180+
setTxInProgress(false);
181+
setTxReceipt('');
182+
onClose();
183+
};
184+
164185
return (
165186
<>
166187
{!userWallet && (
@@ -175,38 +196,78 @@ const DirectMint = ({ developerId }: DirectMintProps) => {
175196
</Button>
176197
)}
177198
{userWallet && (
178-
<Input
179-
w="100%"
180-
size="md"
181-
value={tokenID}
182-
onChange={tokenNameHandler}
183-
type="text"
184-
placeholder={t('tokenIDPlaceholder')}
185-
mt="10"
186-
></Input>
187-
)}
199+
<>
200+
<Input
201+
w="100%"
202+
size="md"
203+
value={tokenID}
204+
onChange={tokenNameHandler}
205+
type="text"
206+
placeholder={t('tokenIDPlaceholder')}
207+
mt="10"
208+
></Input>
188209

189-
<Button
190-
w="100%"
191-
colorScheme="green"
192-
onClick={createTokenHandler}
193-
disabled={!userWallet || networkError}
194-
mt="10"
195-
fontSize={{ base: 's', sm: 'xl' }}
196-
>
197-
{t('mintTokenText')}
198-
</Button>
210+
<Button
211+
w="100%"
212+
colorScheme="green"
213+
onClick={createTokenHandler}
214+
disabled={networkError}
215+
mt="10"
216+
fontSize={{ base: 's', sm: 'xl' }}
217+
>
218+
{t('mintTokenText')}
219+
</Button>
199220

200-
{userWallet && (
201-
<Button
202-
w="100%"
203-
colorScheme="orange"
204-
onClick={disconnectWallet}
205-
mt="10"
206-
fontSize={{ base: 's', sm: 'xl' }}
207-
>
208-
{t('disconnectWallet')}
209-
</Button>
221+
{userWallet && (
222+
<Button
223+
w="100%"
224+
colorScheme="orange"
225+
onClick={disconnectWallet}
226+
mt="10"
227+
fontSize={{ base: 's', sm: 'xl' }}
228+
>
229+
{t('disconnectWallet')}
230+
</Button>
231+
)}
232+
</>
233+
)}
234+
235+
{txInProgress && (
236+
<Modal isOpen={isOpen} onClose={modalCloseHandler} isCentered>
237+
<ModalOverlay />
238+
<ModalContent>
239+
<ModalHeader>Minting your NFT...</ModalHeader>
240+
<ModalCloseButton />
241+
<ModalBody>
242+
<Center h="160px">
243+
{!txReceipt && (
244+
<CircularProgress
245+
isIndeterminate
246+
color="green.300"
247+
size="100px"
248+
/>
249+
)}
250+
{txReceipt && (
251+
<>
252+
<Stack spacing={6}>
253+
<Text color="green.500" fontSize="lg">
254+
Your NFT has been minted!
255+
</Text>
256+
<Link
257+
fontSize="lg"
258+
color="#3182ce"
259+
href={`https://rinkeby.etherscan.io/tx/${txReceipt}`}
260+
isExternal
261+
>
262+
View your TX on Etherscan <ExternalLinkIcon mx="2px" />
263+
</Link>
264+
</Stack>
265+
</>
266+
)}
267+
</Center>
268+
</ModalBody>
269+
</ModalContent>
270+
</Modal>
210271
)}
211272

212273
{networkError && <Text color="red">{t('ethereumNetworkPrompt')}</Text>}

0 commit comments

Comments
 (0)