Metamask payment button for nft contract

i already have a working code for my “mint button” but i only can accept a constant amount of crypto .
i want to know how i can make the user choose the amount of nfts to mint with the crypto charge going up as he chooses more nfts to mint. (example: 1NFT=>0.1 ETH / 2NFT=>0.2 ETH / 3NFT=>0.3 ETH …)
this is the code i’m working with right now .
`
window.addEventListener(‘load’, async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
initPayButton()
} catch (err) {
$(’#status’).html(‘User denied account access’, err)
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider)
initPayButton()
} else {
$(’#status’).html(‘No Metamask (or other Web3 Provider) installed’)
}
})

const initPayButton = () => {
  $('.bn632-hover.bn24').click(() => {
    // paymentAddress is where funds will be send to
    const paymentAddress = 'my wallet adress'
    const amountEth = 0.23

    web3.eth.sendTransaction({
      to: paymentAddress,
      value: web3.toWei(amountEth, 'ether')
    }, (err, transactionId) => {
      if  (err) {
        console.log('Payment failed', err)
        $('#status').html('Payment failed')
      } else {
        console.log('Payment successful', transactionId)
        $('#status').html('Payment successful')
      }
    })
  })
}`

Hey you’re wanting to mint, but looks like you’re just trying to send ether there to the contract, you should instead interact with contract instance to call the mint function and send the ethers value through msgValue

1 Like