简体   繁体   English

如何使用 balanceOf() 在合同上创建铸币限制

[英]How to create a limit in minting using balanceOf() over contract

I'm new to solidity and learning hardhat also, having a hard time on limit minting for NFT.我也是 solidity 和学习 hardhat 的新手,在 NFT 的限制铸币方面遇到了困难。

My contract is coded like:我的合同编码如下:

>     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);
>         }
>     }

and my minting.js is code我的 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)
            }
        }
    }

this limit the address to mint an specific number but when the user repeat the process it overrides the maxPerWallet.这限制了地址铸造一个特定的数字,但是当用户重复这个过程时,它会覆盖 maxPerWallet。 I'm thinking if I can do like我在想我是否能做到

if(balanceOf("address") > 2){
console.log('you already mint 2', response);
}
else{
console.log('you can mint 2', response);
}

how can i translate this into my js?我如何将其翻译成我的 js?

---------------------- Answered------------- ---------------------- 已回答------------

I just added我刚刚添加

mapping(address => uint256) public walletMints;

and I still can't get the output.而且我仍然无法获得输出。 My js was written like this:我的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);
                }

        }
    }

If you use the wallet balance you may not be able to correctly check the amount of mints made by a wallet, as the balance only tells you how many tokens the wallet currently has.如果您使用钱包余额,您可能无法正确检查钱包制作的薄荷糖数量,因为余额仅告诉您钱包当前拥有多少代币。

I think you should create a mapping to store the amount of mints of each wallet.我认为您应该创建一个映射来存储每个钱包的薄荷糖数量。 In your smart contract create a public mapping of mint amount of each wallet:在您的智能合约中创建每个钱包的铸币金额的公共映射:

mapping(address => uint) public walletMints;

And then when the user mints you need add this amount to the address:然后当用户铸币时,您需要将此金额添加到地址:

walletMints[msg.sender] = walletMints[msg.sender] + quantity_

When you create a public mapping the smart contract automatically creates a getter of this mapping that you can use in your javascript code and check the amount of mints made by a wallet.当您创建公共映射时,智能合约会自动创建此映射的 getter,您可以在 javascript 代码中使用它并检查钱包制作的铸币厂数量。

Using ethers, you can call this function in your javascript code passing the wallet address as parameter:使用 ethers,您可以在 javascript 代码中调用此函数,并将钱包地址作为参数传递:

const amount = await contract.walletMints(signer.address)

And verify if the amount is permitted as you are planning:并验证金额是否符合您的计划:

if(amount > 2){
    console.log('you already mint 2');
}
else{
   console.log('you can mint 2');
}

You should use:你应该使用:

walletMints[msg.sender] += quantity_;
require(walletMints[msg.sender] <= maxPerWallet, "exceed max wallet");

no more change after your update.更新后不再更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM