I am trying to call a public payable function from my smart contract (^0.8.0) in a ropsten testnet

    function purchaseCardPack() public payable {
        require(msg.value == cardPackFee, "VALUE SENT NOT EQUAL TO CARD FEE");

using web3.js

  const buyCardPack = async () => {
    try {
      await NFTContract.methods.purchaseCardPack().send({from: account, value: web3.utils.toWei('0.015', 'ether')});
    } catch(err) {

and for some reason seeing the error "Transaction Error. Exception thrown in contract code." in MetaMask before I can even send the transaction.

Please let me know if I am doing anything wrong or if there is any other information I can give. Thanks in advance! This is the error I am seeing

  • It might be that _createCardPack is causing an error.
    – Ismael
    Sep 23, 2021 at 6:09
  • You were right! I was dividing by 0 (an array with length of 0 to be specific) in a function elsewhere in the contract I that _createCardPack was using. Removing this and replacing with a different uint fixed it
    – Steve
    Sep 23, 2021 at 12:11

1 Answer 1


The problem was with _createCardPack(). I was dividing by 0 (an array of length 0, specifically) in a function that it was referencing. Replacing that with a uint did the trick for me.

Takeaway There was nothing wrong with the payable setup, it was an error upstream in the contract. Hope this helps others :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.