如何在web3上调用函数

问题描述 投票:0回答:3

我试图在 web3 上调用一个函数,但它没有在 web3 上执行。 它只是不会弹出 Metamask 钱包来请求交易批准,因此它不会执行。

坚固功能:

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

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})
  }

函数是如何调用的

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

我正在正确加载web3并加载区块链数据,并在按钮组件中调用存款功能。只是想知道它是否与此代码有关,或者问题可能出在其他地方。智能合约已使用松露和甘纳许正确迁移。

reactjs solidity web3js truffle
3个回答
0
投票

您需要正确初始化web3。根据文档,您应该初始化为

const web3 = new Web3(web3.currentProvider).


0
投票

您可以与 gasLimit 参数一起使用。

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

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();
© www.soinside.com 2019 - 2024. All rights reserved.