【问题标题】:Withdrawing ether from smart contract to a specific account using web3使用 web3 从智能合约中提取以太币到特定账户
【发布时间】:2020-11-16 18:44:39
【问题描述】:

我的智能合约中有如下功能:

function transferToaddress(address payable addressToSend) public payable{
    addressToSend.transfer(0.5 ether);
}

从我的前端,我调用上面的函数如下:

var response = await this.contract.transferToaddress(vm.userAccountAddress,{
   from: vm.userAccountAddress,
   gas: vm.gas
})

交易失败。我的合同有一些余额。单击按钮后,我希望将一些以太币(在本例中为 0.5)从我的智能合约转移到传递给函数的地址。 知道我做错了什么吗?

【问题讨论】:

    标签: blockchain solidity smartcontracts web3 ether


    【解决方案1】:
    var response = await this.contract.transferToaddress(vm.userAccountAddress)
    .send({ from: vm.userAccountAddress, gas: vm.gas })
    

    您错过了发送关键字

    【讨论】:

      【解决方案2】:

      由于用户正在获得以太币并且它不提供以太币,因此不应支付合同。

      function transferToaddress(address payable addressToSend) public{
          addressToSend.transfer(0.5 ether);
      }
      
      var response = await this.contract.transferToaddress(vm.userAccountAddress)
          .send({ from: vm.userAccountAddress, gas: vm.gas })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-29
        • 2021-12-18
        • 1970-01-01
        • 2019-10-17
        相关资源
        最近更新 更多