// 지갑 연결
const connectWallet = async () => {
if (typeof window.ethereum !== "undefined") {
try {
await window.ethereum.request({ method: "eth_requestAccounts" });
const web3Provider = new quais.BrowserProvider(window.ethereum);
const web3Signer = await web3Provider.getSigner();
const address = await web3Signer.getAddress();
setProvider(web3Provider);
setSigner(web3Signer);
setAccount(address);
// 계약 초기화
const nftContract = new quais.Contract(
contractAddress,
contractABI,
web3Signer
);
setContract(nftContract);
// 계약 데이터 로드
await loadContractData(nftContract, address);
toast({
title: "지갑 연결됨",
description: `주소: ${address.slice(0, 6)}...${address.slice(-4)}`,
status: "success",
duration: 3000,
isClosable: true,
});
} catch (error) {
console.error("지갑 연결 실패:", error);
toast({
title: "연결 실패",
description: error.message,
status: "error",
duration: 3000,
isClosable: true,
});
}
} else {
toast({
title: "지갑을 찾을 수 없음",
description: "MetaMask나 다른 Web3 지갑을 설치해주세요",
status: "warning",
duration: 3000,
isClosable: true,
});
}
};
// 계약 데이터 로드
const loadContractData = async (nftContract, userAddress) => {
try {
const price = await nftContract.mintPrice();
const max = await nftContract.maxSupply();
const total = await nftContract.totalSupply();
const ownerAddress = await nftContract.owner();
setMintPrice(quais.formatEther(price));
setMaxSupply(max.toString());
setTotalSupply(total.toString());
setIsOwner(userAddress.toLowerCase() === ownerAddress.toLowerCase());
} catch (error) {
console.error("계약 데이터 로드 실패:", error);
}
};
// NFT 민트
const mintNFT = async () => {
if (!contract) return;
try {
const price = await contract.mintPrice();
const tx = await contract.mint({ value: price });
toast({
title: "민팅 중...",
description: "트랜잭션이 처리되고 있습니다",
status: "info",
duration: 5000,
isClosable: true,
});
await tx.wait();
toast({
title: "민트 성공!",
description: "NFT가 성공적으로 민트되었습니다",
status: "success",
duration: 5000,
isClosable: true,
});
// 총 공급량 업데이트
const total = await contract.totalSupply();
setTotalSupply(total.toString());
} catch (error) {
console.error("민트 실패:", error);
toast({
title: "민트 실패",
description: error.message,
status: "error",
duration: 5000,
isClosable: true,
});
}
};
// 민트 가격 업데이트
const updateMintPrice = async () => {
if (!contract || !newMintPrice) return;
try {
const priceInWei = quais.parseEther(newMintPrice);
const tx = await contract.updateMintPrice(priceInWei);
toast({
title: "업데이트 중...",
description: "트랜잭션이 처리되고 있습니다",
status: "info",
duration: 5000,
isClosable: true,
});
await tx.wait();
toast({
title: "가격 업데이트됨",
description: "민트 가격이 성공적으로 업데이트되었습니다",
status: "success",
duration: 5000,
isClosable: true,
});
setMintPrice(newMintPrice);
setNewMintPrice("");
} catch (error) {
console.error("가격 업데이트 실패:", error);
toast({
title: "업데이트 실패",
description: error.message,
status: "error",
duration: 5000,
isClosable: true,
});
}
};
// 최대 공급량 업데이트
const updateMaxSupply = async () => {
if (!contract || !newMaxSupply) return;
try {
const tx = await contract.updateMaxSupply(newMaxSupply);
toast({
title: "업데이트 중...",
description: "트랜잭션이 처리되고 있습니다",
status: "info",
duration: 5000,
isClosable: true,
});
await tx.wait();
toast({
title: "공급량 업데이트됨",
description: "최대 공급량이 성공적으로 업데이트되었습니다",
status: "success",
duration: 5000,
isClosable: true,
});
setMaxSupply(newMaxSupply);
setNewMaxSupply("");
} catch (error) {
console.error("공급량 업데이트 실패:", error);
toast({
title: "업데이트 실패",
description: error.message,
status: "error",
duration: 5000,
isClosable: true,
});
}
};
// 자금 출금
const withdrawFunds = async () => {
if (!contract) return;
try {
const tx = await contract.withdraw();
toast({
title: "출금 중...",
description: "트랜잭션이 처리되고 있습니다",
status: "info",
duration: 5000,
isClosable: true,
});
await tx.wait();
toast({
title: "출금 성공",
description: "자금이 성공적으로 출금되었습니다",
status: "success",
duration: 5000,
isClosable: true,
});
} catch (error) {
console.error("출금 실패:", error);
toast({
title: "출금 실패",
description: error.message,
status: "error",
duration: 5000,
isClosable: true,
});
}
};