1

嗨,我是智能合约的新手,并试图创建一个在创始人之间存储和分配资金的合约,这是否接近接近,或者任何人都可以帮助我。谢谢

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.7.4;

import './Ownable.sol';

interface wallet {

    function getCut(address _from, address _to, uint _value) external returns(bool success);

    function splitFoundersCut(uint _amount, address Ceo, address Cfo, address Cto) external returns(bool success);
}

interface ERC20 {
    function totalSupply() external view returns(uint supply);

    function balanceOf(address _owner) external view returns(uint balance);

    function transfer(address _to, uint _value) external returns(bool success);

    function transferFrom(address _from, address _to, uint _value) external returns(bool success);

    function approve(address _spender, uint _value) external returns(bool success);

    function allowance(address _owner, address _spender) external view returns(uint remaining);

    function decimals() external view returns(uint digits);
    event Approval(address indexed _owner, address indexed _spender, uint _value);

    function deposit() external payable;

    function withdraw(uint256 wad) external;
}

contract FoundersWallet is Ownable {

    address payable Ceo = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;
    address payable Cto = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;
    address payable Cfo = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;

    uint256 amount = address(this).balance;

    function withdraw() internal returns(bool) {

        if(amount > 0) {
            Ceo.transfer((amount / 40));
            Cto.transfer((amount / 40));
            Cfo.transfer((amount / 20));
        }

        else {
            return false;
        }
    }
}
4

1 回答 1

0

您的代码仅在创建合同时分配值uint256 amount,而不是在余额更改时更新它。这将导致具有amountalways的值0

注意:我不计算边缘情况,例如在部署合约之前合约地址已经具有 ETH 价值,或者使用selfdestruct(). 这些都是有效的案例,但超出了这个问题的范围。

withdraw()函数应返回布尔值。但是当您不返回任何值时,调用者可能会将其解释为false. 所以我会建议return true转移是否成功。

此外,您可能希望能够在withdraw()外部调用该函数,因此将其标记为external而不是internal. 但这取决于您的用例,也许您已经从另一个未在您的问题中显示的函数在内部调用它。

pragma solidity ^0.7.4;

contract FoundersWallet is Ownable {

    address payable Ceo = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;
    address payable Cto = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;
    address payable Cfo = 0x471F9361ec8Fc947d371B95e5a5f5820C7A42A1B;

    function withdraw() external returns(bool) { // visibility `external`
        // moved the `amount` here so that it's recalculated on each function call
        uint256 amount = address(this).balance;

        if(amount > 0) {
            Ceo.transfer((amount / 40));
            Cto.transfer((amount / 40));
            Cfo.transfer((amount / 20));
            
            return true; // added the return value
        }
        
        // don't need the `else` because of the early return
        return false;
    }
}
于 2021-04-17T21:32:25.163 回答