0

I'm trying to interface with my tested (in Remix) and working contract in a browser now but cannot understand how to send additional arguments with a payable function.

I need to pass an argument userTeamChosen with a appendUserBet() function which I both set the values for through html forms but get this error Error: cannot estimate gas; transaction may fail or may require manual gas limit and have no idea whether im sending the argument correctly or not.

index.html:

<body>
  <label for="fund">ETH Amount</label>
  <input id="ethAmount" placeholder="0.1" />
  <input id="betTeam" placeholder="Team name" />
  <button type="button" id="appendBetButton"> appendBetButton </button>
</body>

<script src="./index.js" type="module"></script>

smart contract function:

    function appendUserBet(string memory userTeamChosen) public payable {
        uint256 minimumFee = 1 * 10**17 wei;
        userList.push(payable(msg.sender));
        require(msg.value >= minimumFee);
        fee = msg.value;
        userStructs[msg.sender].betAmount += msg.value;
        userStructs[msg.sender].teamChosen = userTeamChosen;
    }

and this is where I interact with it in index.js:

async function appendUserBet() {
    const ethAmount = document.getElementById("ethAmount").value
    console.log(`Funding with ${ethAmount}...`)
    if (typeof window.ethereum !== "undefined") {
        const provider = new ethers.providers.Web3Provider(window.ethereum)
        const signer = provider.getSigner()
        const contract = new ethers.Contract(contractAddress, abi, signer)
        try {
            const transactionResponse = await contract.appendUserBet({
                value: ethers.utils.parseEther(ethAmount), userTeamChosen: "sab", gasLimit: 50000
            })
            await listenForTransactionMine(transactionResponse, provider)
        } catch (error) {
            console.log(error)
        }
    } else {
        appendBetButton.innerHTML = "Please install MetaMask"
    }
}

Can somebody please let me know how to add the needed string argument to a payable function in a js interface with ethers?

Thanks alot for your time in advance!

3
  • have you tried with the gas limit higher?
    – sola24
    Jun 21 at 16:00
  • yep, tried adding 5 zeros at the end, doesnt help :( Jun 21 at 17:43
  • Are you using hardhat by chance? Jun 21 at 17:59

1 Answer 1

0

I think you are passing the argument to the smart contract in the wrong way.

The documentation states that you have to pass the arguments before you pass the overrides https://docs.ethers.io/v5/api/contract/contract/#contract-functionsSend.

So the call according to this should look like this:

await contract.appendUserBet(
   "sab",
   {
      value: ethers.utils.parseEther(ethAmount), gasLimit: 50000
   }
)

You might be able to omit the gasLimit override if this solves your issue.

1
  • god bless your soul, this solved my problem :) Jun 21 at 22:25

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.