繁体   English   中英

如何在 web3 上调用 function

[英]How to call function on web3

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

坚固性 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})
  }

function怎么叫

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

您需要正确初始化 web3。 根据文档,您应该初始化为const web3 = new Web3(web3.currentProvider).

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM