【问题标题】:How to withdraw the current balance of the smart contract using web3.py?如何使用 web3.py 提取智能合约的当前余额?
【发布时间】:2021-11-30 17:45:48
【问题描述】:

我已经创建了一个函数来实现这一点,但是在前端,我希望能够调用该函数并将以太币从合约转移到一个帐户。这是来自solidity的代码。

    function withdraw() public payable onlyOwner {
        payable(msg.sender).transfer(address(this).balance);
    }

到目前为止,为了调用涉及状态更改的其他函数,我使用了来自 web3.py 的构建事务。为了将以太币转移到合约中,我已经这样做了 -

booking_contract = w3.eth.contract(address=self.receipt.contractAddress, abi=abi)
        nonce = w3.eth.getTransactionCount(address)
        booking_transaction = booking_contract.functions.book(machine_name, start_time, end_time).buildTransaction(
            {
                "chainId": chain_id,
                "gasPrice": w3.eth.gas_price,
                "from": address,
                "nonce": nonce,
                "value": 100
            }
        )
        signed_txn = w3.eth.account.sign_transaction(booking_transaction, private_key=key)
        txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

有没有办法将这里发送到合约的以太币转移到我想要的任何账户?

【问题讨论】:

    标签: solidity smartcontracts web3py


    【解决方案1】:

    有什么方法可以将这里发送到合约的以太币转移到我想要的任何账户?

    您可以在withdraw() 函数参数中指定收件人,然后将其传递给payable() 函数。

    function withdraw(address _recipient) public payable onlyOwner {
        payable(_recipient).transfer(address(this).balance);
    }
    

    不要忘记在 .py 脚本中指定收件人:

    booking_contract.functions.withdraw(recipient_address)
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2020-08-26
      • 2021-11-30
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2022-08-14
      • 2022-11-15
      • 2021-09-20
      相关资源
      最近更新 更多