0

I'm trying to call a function on web3, but it doesn't get executed on web3. It just doesn't popup Metamask wallet to ask for the transaction approval, so it doesn't execute.

Solidity function:

function Deposit(uint _amount) payable public{
    require(msg.value == _amount);
    funds[msg.sender] += _amount;
}

Function on web3

  deposit = async(depositAmount)=>{
    const web3 = window.web3
    const ethers = web3.utils.toWei(this.depositAmount.value, 'ether')
    await web3.contract.methods.Deposit(depositAmount).send({from: this.account, value: ethers})
  }

How is the function called

<form className="deposit" onSubmitCapture={(event) => {
              event.preventDefault()
              const amount = this.amount
              this.deposit(amount)
            }}>
              <input type="text" className="inputs" placeholder="Amount to deposit" 
              ref={(input)=>this.amount = input}/>
              <input type="submit" className="btn" value="DEPOSIT"/>
        </form>

I'm loading web3 and loading blockchain data correctly, and deposit function is called in a button component. Just wanted to know if it has something to do with this code, or the problem might be somewhere else. The smart contract is correctly migrated with truffle and ganache.

TylerH
  • 20,799
  • 66
  • 75
  • 101
brt88
  • 37
  • 6

2 Answers2

0

You need to initialize web3 properly. According to the docs, you should initialize as const web3 = new Web3(web3.currentProvider).

Zi Hang
  • 26
  • 5
0

You can use with gasLimit param.

await web3.contract.methods.Deposit(depositAmount).send({from: this.account, value:
   ethers, gasLimit: "21000"})
vimuth
  • 5,064
  • 33
  • 79
  • 116
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 29 '22 at 06:55