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.

7
  • I'm not familiar with web3, but shouldn't you use await deposit(depositAmount.value)? Commented Jun 3, 2022 at 22:17
  • @IłyaBursov It throws an error if I put await in front of it Commented Jun 3, 2022 at 22:19
  • @brt88 did you initialize web3? did you pass a provider or if you use metamask did you connect the wallet? Commented Jun 3, 2022 at 22:55
  • @JacopoMosconi Yes, I did all that Commented Jun 3, 2022 at 23:27
  • it prints any error? @brt88 Commented Jun 3, 2022 at 23:38

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can use with gasLimit param.

await web3.contract.methods.Deposit(depositAmount).send({from: this.account, value:
   ethers, gasLimit: "21000"})

1 Comment

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.
0

import React, { useEffect, useState } from "react";
import { ethers } from "ethers";
import { useWeb3React } from "@web3-react/core";
import { CONTRACT_ADDRESS } from "../../Utils/constants";
import { contractAbi } from "../../Contracts/arrue-contract-abi";



 const signer = library.getSigner(account).connectUnchecked();
      const contract = new ethers.Contract(
        CONTRACT_ADDRESS,
        contractAbi,
        signer
      );
      const isActivatedBorrowersValue = await contract.activatedBorrowers();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.