Trying to execute the mint function of my smart contract through a button on my website

async function mint() {

var MintAmount = mintAmnt ;

var userAddy = Moralis.User.current();

var pricePrNft = 0.1 ;

var priceToPay = MintAmount*pricePrNft ;



const web3Provider = await Moralis.enableWeb3();

const web3Js = new Web3(Moralis.provider);

const ethers = Moralis.web3Library;



const provider = new ethers.providers.Web3Provider(window.ethereum)

const signer = provider.getSigner();

const ADDRESS =Address;

const ABI = abi;

const contract = new ethers.Contract(ADDRESS, ABI, signer) ;

contract.mint(userAddy,priceToPay)

}
i tried this at first, if i use web3Provider in the contract line instead of signer and i just call the name function of my contract and consolelog it i can see the contract name, so the contract is well linked but when i try to call the mint function it asks me for a signer and here is where im stuck, i googled and read evrything i could find about the signer but couldnt find an solution that would work for me.

Then i also tried this :

async function mint2(){

var userAddy = Moralis.User.current();

const web3 = await Moralis.enableWeb3();

var MintAmount = mintAmnt ;

var pricePrNft = 0.1 ;

var priceToPay = MintAmount*pricePrNft ;

const ADDRESS = Address;

const ABI = abi;

const options = {

contractAddress: ADDRESS ,

functionName: "mint" ,

abi: ABI ,

params: {

  _to: userAddy ,

  _mintAmount: MintAmount

}

}

await Moralis.executeFunction(options) ;

}

and this code above returns a huge error . Could someone help me out please ?

You can use the 2nd way, but Moralis.User.current() is an object, if you want just the address you have to use Moralis.User.current().get("ethAddress")

Also I see you set a price to pay but never use it? - watch out if you will because when working with value being sent to smart contracts you have to take into account you are working with wei

thank you man this is exactly what i was trying to fix. the button now calls the function when i click it .
now im wondering how i can set the price to be paid, Because if i just mint 1 nft right now im only paying the gas fees for the transaction im not paying the price of the nft that i set in my smart contract. If you could also help me out with this it would be awesome!

So what is the problem in the first function? You have to also await the contract.mint() first of all.

Of course! So basically, you would just set a variable to your mint price (in eth decimals, eg. const MINT_PRICE = 0.1) and then in your executeFunction call you would add a msgValue parameter and transfer the decimals to wei (using Moralis Units) like this:

const options = {
    contractAddress: ADDRESS ,
    functionName: "mint" ,
    abi: ABI ,
    msgValue: Moralis.Units.ETH((MINT_PRICE*amount).toString()) //you get amount from your form or wherever
    params: {
          _to: userAddy ,
          _mintAmount: MintAmount
    }
}

i had to add .get(‘ethAddress’) to the end of var userAddy = Moralis.User.current()
so updated line 2 would be : var userAddy = Moralis.User.current().get(‘ethAddress’)
Also i need to add await like you said on the line where i call the contract function
so updated line would be : await contract.mint(userAddy,priceToPay)
and this makes it work and pops up a metamask notification when i click the mint button .
But i realised that i was passing it the wrong var : priceToPay .
instead i need to pass it the variable MintAmount.
And that makes the button work, it calls the mint function and the user can mint as many as he choose to. the issue i still have is that even if i set the price of the NFTs in the smart contract , the amount the user needs to pay to sign the metamask transaction is only the gas fee. how can i fix it so that the user has to pay for the nfts he is minting

Sounds like an issue with your contract - can we see a snippet?

Yeah, sounds like you are missing a require statement

this works if i mint only 1 nft, as soon as i try to mint 2 or just >1 i get an error .

this here is the mint function of the smart contract :

Blockquote

function mint(address _to, uint256 _mintAmount) public payable {

    uint256 supply = totalSupply();

    require(!paused);

    require(_mintAmount > 0);

    require(_mintAmount <= maxMintAmount);

    require(supply + _mintAmount <= maxSupply);

    if (msg.sender != owner()) {

        if (whitelisted[msg.sender] != true) {

            if (presaleWallets[msg.sender] != true) {

                //general public

                require(msg.value >= cost * _mintAmount);

            } else {

                //presale

                require(msg.value >= presaleCost * _mintAmount);

            }

        }

    }

    for (uint256 i = 1; i <= _mintAmount; i++) {

        _safeMint(_to, supply + i);

    }

}

Blockquote

okayy nope nevermind my bad i had set the price per nft when i delpoyed the contract to 0.05 and the max one can buy per tx is 10 so i guess this is why i could only set the price to 0.5 max and mint only 1 nft, i guess because the contract only allowed 10 nfts to be minted at the same time so a maximum msg.value of 10*0.05 wich is 0.5 . weird it allows you to make people over pay for 1 nft and only send them 1 nft just by changing some js variable i guess this is pretty risky to use if i want to deploy it on a mainnet later on ?

I mean that’s why you would generally use == instead of >= or <= in your require statements

true true i see it now. I didnt write the smart contract that im using but now i know to look out for this.