How Can I convert the value of the input form to Towei unit with web3

go-ethereumreactweb3js

I Have a form with an input and a button with a type of submit, I want to submit a number from the input and convert it toWei unit using web3 that will be passed into a function that will allow the user to stake some DaiToken, I've tried this method web3.utils.toWei(amount, "ether") that I found on web3 documentation but when I clicked submit it gave me this error "Unhandled Rejection (TypeError): address.toLowerCase is not a function" and metaMask doesn't pop up to confirm the transaction of staking;

I've spent a lot of time researching but I got no answer. Thank you

state value of the input

const [inputValue, setInputValue] = useState("");

submit function

const stackHandler = (e) => {
        e.preventDefault();

        const amount = web3.utils.toWei(inputValue);
        stakeTokens(amount);
    }

Form

<form className="mb-3" onSubmit={stackHandler}>

                <div className="balance-section">
                 
                <div className="input-group mb-4">
                  <input
                    type="text"
                    className="form-control form-control-lg"
                    placeholder="0.0"
                    required
                    value={inputValue}
                    onChange={(e)=> setInputValue(e.target.value)}/>

                  <div className="input-group-append">
                    <div className="input-group-text">
                      <img src={dai} height='32' alt=""/>
                      &nbsp;&nbsp;&nbsp; mDAI
                    </div>
                  </div>

                </div>
                <button type="submit" className="btn btn-primary btn-block btn-lg">STAKE!</button>
              </form>

stakeTokens Function

const stakeTokens = async (amount) => {
    await dTokens.methods.approve(fTokens._address, amount)
    .send({ from : investorAccount})
    .on("transitionsHash", (hash)=> {
      fTokens.methods.stakeTokens(amount).send({from : investorAccount})
    }).on("transitionsHash", (hash)=> {
      console.log("stacking has been validated")
    });
};

Best Answer

The error is probably the address you pass to the approve method. If fTokens is a web3js contract you need to use fTokens.options.address to get the address (see https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#options-address)

It is not a direct answer to the usage of toWei but that one looks correct and the error indicates that an invalid address is passed somewhere.

Related Topic