如何以坚固性将()或转移()从合同地址发送到帐户地址(我的意思是从地址(this).balance扣除)

发布于 2025-02-01 23:45:36 字数 443 浏览 3 评论 0原文

部署合同时,它会有一个可以按地址调用的地址。我的合同是从一个地址收到的,这意味着我可以将()或转移()转移到本合同中,但是如果我想将此实际合同转移到任何帐户,我该怎么办?

示例代码:

    function submitTransaction(address _to,uint _value,string memory _desc) public 
    onlyOwner 
    {
        require(_value <= address(this).balance,"This wallet does not have enough balace to send");
        if(!_to.send(_value)){
             revert("doposit fail");
        }
    }

但是如何从地址(此)扣除?

When the contract is deployed it has an address that we can call by address(this). My contract is receivable from an address which means I can send() or transfer() to this contract but If I want to transfer from this actual contract to any account how can I do that?

example code:

    function submitTransaction(address _to,uint _value,string memory _desc) public 
    onlyOwner 
    {
        require(_value <= address(this).balance,"This wallet does not have enough balace to send");
        if(!_to.send(_value)){
             revert("doposit fail");
        }
    }

but how can I deduct from address(this).balance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海夜未眠 2025-02-08 23:45:36

您可以看到此智能合约代码的示例:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Bank {
    address owner;
    
    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "You're not the smart contract owner!");
        _;
    }

    event Deposited(address from, uint amount);

    function depositMoney() public payable {
        emit Deposited(msg.sender, msg.value);
    }

    // Use transfer method to withdraw an amount of money and for updating automatically the balance
    function withdrawMoney(address _to, uint _value) public onlyOwner {
        payable(_to).transfer(_value);
    }

    // Getter smart contract Balance
    function getSmartContractBalance() external view returns(uint) {
        return address(this).balance;
    }

}

建议:在这种情况下,如果您只使用转移或发送,我建议您使用transfer()方法而不是<代码> send(),因为如果传输不起作用,它会导致故障。我建议阅读此 thread 。相反,要避免重新输入攻击,您必须对呼叫方法进行转移以太。

You can see this example of smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Bank {
    address owner;
    
    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "You're not the smart contract owner!");
        _;
    }

    event Deposited(address from, uint amount);

    function depositMoney() public payable {
        emit Deposited(msg.sender, msg.value);
    }

    // Use transfer method to withdraw an amount of money and for updating automatically the balance
    function withdrawMoney(address _to, uint _value) public onlyOwner {
        payable(_to).transfer(_value);
    }

    // Getter smart contract Balance
    function getSmartContractBalance() external view returns(uint) {
        return address(this).balance;
    }

}

ADVICE: In this case, if you want use only transfer or send, I advice you to use transfer() method instead send(), because it throws on failure if transfer didn't work. I recommend to read this thread. On the contrary for avoid reentracy attack you must to call method to transfer ether.

病毒体 2025-02-08 23:45:36

不要使用安全如果您没有充分的理由,请改用Transfer传输在发生故障的情况下会丢失错误,而Safe方法只会返回false,这意味着代码的下一行将仍然可以执行,如果您不处理错误,交易将不会恢复。

function submitTransaction(address _to,uint _value,string memory _desc) public 
    onlyOwner 
    {
        require(_value <= address(this).balance,"This wallet does not have enough balace to send");
        _to.transfer(_value);
    }

Don't use safe if you don't have a good reason, use transfer instead. While transfer will throw an error in case of a failure, the safe method will just return false, which means the next lines of the code will still be executed and the transaction will not be reverted if you don't handle the error.

function submitTransaction(address _to,uint _value,string memory _desc) public 
    onlyOwner 
    {
        require(_value <= address(this).balance,"This wallet does not have enough balace to send");
        _to.transfer(_value);
    }
呢古 2025-02-08 23:45:36
 (bool success, ) = addressToTransfer.call{value: address(this).balance}("");
 require(success, "Transfer failed.");
 (bool success, ) = addressToTransfer.call{value: address(this).balance}("");
 require(success, "Transfer failed.");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文