-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
282 lines (272 loc) · 8.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { useState, useEffect, useRef } from 'react';
import { updateNftIndex } from '../../Redux/nftAction';
import { useDispatch, useSelector } from 'react-redux';
import { ethers, BigNumber } from 'ethers';
import {
Container,
MintBg,
MintContent,
MintImg,
Nav,
NavbarContainer,
NavLogo,
MintLabelDiv,
MintLabel,
MintSelect,
MintOption,
BtnWrapper,
MintStatus,
LoadingDiv,
LoadingAnim,
LoadingMessage,
LoadingMessageDiv,
OutsideContainer,
NavLogoImg,
} from './MintElements';
import { ButtonButt, ButtonError } from '../ButtonElements';
import { useHistory } from 'react-router-dom';
import video from '../../Images/polynians_background.png';
import ImgSignature from '../../Images/Signature2_tr_W.png';
import { connectWallet } from '../../Utils/interact';
import contractAbi from '../../Ethereum/contractAbi.json';
import { network, PRICE_ONE_NFT } from '../../constants.js';
//change here the network you want your app to connect to
const { chainId, name, currency } = network.polygon;
const Mint = () => {
const dispatch = useDispatch();
const nftIndex = useSelector((state) => state.nftIndex);
const contractAddress = '0xa95841fa907c8a167b03b8ad102a25ac31b03b40';
const history = useHistory();
const [isWalletConnected, setIsWalletConnected] = useState(false);
const [numberNFTs, setNumberNFTs] = useState(1);
const [walletAddress, setWalletAddress] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [message, setMessage] = useState('test');
const [status, setStatus] = useState('');
const [isError, setIsError] = useState(false);
const firstLoadWallet = useRef(false);
const firstLoadNetwork = useRef(false);
const firstLoadGoToPostMint = useRef(false);
//update UI depending on wallet connected
useEffect(() => {
async function fetchWallet() {
console.log('UseEffect is happening :)');
const { address, status } = await connectWallet();
setWalletAddress(address);
setStatus(status);
if (address != 0) {
setIsWalletConnected(true);
}
}
fetchWallet();
}, []);
useEffect(() => {
async function updateWallet() {
if (firstLoadWallet.current) {
console.log('UseEffectAccount is happening :)');
addWalletListener();
} else {
console.log('correct path Account:)');
firstLoadWallet.current = true;
}
}
updateWallet();
}, [walletAddress]);
useEffect(() => {
async function updateNetwork() {
console.log(nftIndex);
if (firstLoadNetwork.current) {
console.log('UseEffectNetwork is happening :)');
getCurrentNetworkConnected();
} else {
console.log('correct path Network:)');
firstLoadNetwork.current = true;
}
}
updateNetwork();
}, [status]);
useEffect(() => {
async function goToPostMint() {
if (firstLoadGoToPostMint.current) {
console.log('GoToPostMint is happening :)');
console.log(nftIndex);
setIsLoading(false);
history.push('/postmint');
} else {
console.log('correct path goToPostMint :)');
}
}
goToPostMint();
}, [nftIndex]);
const addWalletListener = () => {
if (window.ethereum) {
window.ethereum.on('accountsChanged', (accounts) => {
if (accounts.length > 0) {
setWalletAddress(accounts[0]);
} else {
setWalletAddress('');
setStatus('🦊 Connect to Metamask using the top right button.');
}
});
} else {
setStatus(
<p>
{' '}
🦊{' '}
<a
target='_blank'
href={`https://metamask.io/download.html`}
rel='noreferrer'
>
Please install Metamask, a virtual Ethereum wallet, in your browser.
</a>
</p>
);
}
};
const getCurrentNetworkConnected = () => {
if (window.ethereum) {
window.ethereum.on('chainChanged', (_chainId) => {
if (parseInt(_chainId) == chainId) {
setStatus(`🌎 Connected to ${name}`);
} else {
setStatus(`🦊 Please connect to ${name}`);
}
});
}
};
const onChange = (e) => {
setNumberNFTs(parseInt(e.target.value));
};
const onConnectWallet = async () => {
console.log('calling onConnectWallet');
const { address, status } = await connectWallet();
setWalletAddress(address);
setStatus(status);
};
const onMint = async () => {
//e.preventDefault();
if (window.ethereum) {
const provider = new ethers.providers.Web3Provider(
window.ethereum,
chainId
);
const signer = provider.getSigner();
const contract = new ethers.Contract(
contractAddress,
contractAbi.abi,
signer
);
setMessage('');
setIsLoading(true);
try {
setMessage('Waiting for the transaction to be mined...');
const tx = await contract.mint(BigNumber.from(numberNFTs), {
value: ethers.utils.parseEther(
(PRICE_ONE_NFT * numberNFTs).toString()
),
});
const receipt = await tx.wait();
console.log(receipt);
if (numberNFTs === 1) {
const indexNFT = Array.of(parseInt(receipt.events[1].args[3]._hex));
dispatch(updateNftIndex(indexNFT));
} else {
const indexNFT = receipt.events[1].args[3].map((a) =>
parseInt(a._hex)
);
dispatch(updateNftIndex(indexNFT));
}
firstLoadGoToPostMint.current = true;
} catch (error) {
onError(error);
console.log(error);
}
}
};
const onError = async (e) => {
setMessage(e.message);
setIsError(true);
setTimeout(() => setIsLoading(false), 5000);
setTimeout(() => setIsError(false), 5000);
};
const onDivClick = () => {
isError && setIsError(false);
};
return (
<>
<OutsideContainer>
<Container isLoading={isLoading || isError}>
<Nav>
<NavbarContainer>
<NavLogo to='/'>
<NavLogoImg src={ImgSignature} alt='signature' />
</NavLogo>
<BtnWrapper>
<ButtonButt
onClick={onConnectWallet}
disabled={isWalletConnected}
>
{walletAddress.length > 0
? `Connected: ${String(walletAddress).slice(
0,
6
)}...${String(walletAddress).slice(38)}`
: `Connect Wallet`}
</ButtonButt>
</BtnWrapper>
</NavbarContainer>
</Nav>
<MintBg>
<MintImg src={video} />
</MintBg>
<MintContent isStatus={status}>
{status === `🌎 Connected to ${name}` ? (
<MintLabelDiv>
<div>
<MintLabel>How many NFTs do you want ? </MintLabel>
<MintSelect name='number' onChange={onChange}>
<MintOption value='1'>1</MintOption>
<MintOption value='2'>2</MintOption>
<MintOption value='3'>3</MintOption>
<MintOption value='4'>4</MintOption>
<MintOption value='5'>5</MintOption>
</MintSelect>
</div>
<BtnWrapper>
<ButtonButt onClick={onMint}>
Mint {numberNFTs} Polynian{numberNFTs === 1 ? `` : `s`}
</ButtonButt>
</BtnWrapper>
<MintLabel>
{`Your minting cost is: ${
numberNFTs * PRICE_ONE_NFT
} ${currency}`}
</MintLabel>
<MintStatus>{status}</MintStatus>
</MintLabelDiv>
) : (
<ButtonError>{status}</ButtonError>
)}
</MintContent>
</Container>
{(isLoading || isError) && (
<LoadingDiv onClick={onDivClick}>
{isLoading && <LoadingAnim></LoadingAnim>}
<LoadingMessageDiv isError={isError}>
<LoadingMessage>
{isError
? `${message}
Please ensure you have at least ${
PRICE_ONE_NFT * numberNFTs
} ${currency} in your wallet to pay for the mint`
: message}
</LoadingMessage>
</LoadingMessageDiv>
</LoadingDiv>
)}
</OutsideContainer>
</>
);
};
export default Mint;