【发布时间】:2022-12-10 12:55:10
【问题描述】:
我也是 solidity 和学习 hardhat 的新手,在 NFT 的限制铸币方面遇到了困难。
我的合同编码如下:
> function mint(uint256 quantity_) public payable{
> require(isPublicMintEnabled, 'minting not enabled');
> require(msg.value == quantity_ * mintPrice, 'wrong mint value');
> require(totalSupply + quantity_ <= maxSupply, 'sold out');
> require(walletMints[msg.sender] + quantity_ <= maxPerWallet, 'exceed max wallet');
>
> for(uint256 i = 0; i < quantity_; i++){
> uint256 newTokenId = totalSupply + 1;
> totalSupply++;
> _safeMint(msg.sender, newTokenId);
> }
> }
我的 minting .js 是代码
async function handleMint(){
if(window.ethereum){
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(
NFTAddress,
NFT.abi,
signer
);
try{
const response = await contract.mint(BigNumber.from(mintAmount), {
value: ethers.utils.parseEther((0 * mintAmount).toString()),
});
console.log('response', response);
} catch(err){
console.log("error: ", err)
}
}
}
这限制了地址铸造一个特定的数字,但是当用户重复这个过程时,它会覆盖 maxPerWallet。 我在想我是否能做到
if(balanceOf("address") > 2){
console.log('you already mint 2', response);
}
else{
console.log('you can mint 2', response);
}
我如何将其翻译成我的 js?
---------------------- 已回答------------
我刚刚添加
mapping(address => uint256) public walletMints;
而且我仍然无法获得输出。我的js是这样写的:
const MainMint = ({ accounts}) =>{
const[mintAmount] = useState(1);
const isConnected = Boolean(accounts[0]);
async function handleMint(){
if(window.ethereum){
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(
NFTAddress,
NFT.abi,
signer
);
const amount = await contract.walletMints(signer.address);
try{
if(amount < 2){
const response = await contract.mint(BigNumber.from(mintAmount), {
value: ethers.utils.parseEther((0 * mintAmount).toString()),
});
console.log('response', response);
}
else{
console.log('Mint Max');
}
}
catch(err){
console.log("error: ", err);
}
}
}
【问题讨论】:
标签: javascript reactjs solidity smartcontracts hardhat