首页 > 解决方案 > 我收到一个错误,一个声明错误,但它指向我不明白的引号中的内容

问题描述

我读到的错误

DeclarationError: 未声明的标识符。
--> SharedWallet.sol:17:17:
   |
17 | require(isOwner() || 津贴[msg.sender] >= _amount, "你不被允许!");
   | ^^^^^^^
pragma solidity ^0.8.6;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract Allowance is Ownable {
    mapping(address => uint256) public allowance;

    function addAllowance(address _who, uint256 _amount) public onlyOwner {
        allowance[_who] = _amount;
    }

    modifier ownerOrAllowed(uint256 _amount) {
        require(
            isOwner() || allowance[msg.sender] >= _amount,
            "You are not allowed!"
        );
        _;
    }

    function reduceAllowance(address _who, uint256 _amount)
        internal
        ownerOrAllowed(_amount)
    {
        allowance[_who] -= _amount;
    }
}

contract SharedWallet is Allowance {
    uint256 public balanceReceived;

    function sendEther() public payable {
        balanceReceived += msg.value;
    }

    function isOwner() internal view returns (bool) {
        return owner() == msg.sender;
    }

    function withdrawMoney(address payable _to, uint256 _amount)
        public
        ownerOrAllowed(_amount)
    {
        require(
            _amount <= address(this).balance,
            "not enough funds in contract"
        );
        if (!isOwner()) {
            reduceAllowance(msg.sender, _amount);
        }
        _to.transfer(_amount);
    }

    fallback() external payable {}
}

标签: solidity

解决方案


pragma solidity ^0.8.6;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract Allowance is Ownable {
    mapping(address => uint256) public allowance;

    // This will fix compile error, or, need use inheritance to prevent duplicate code.
    function isOwner() internal view returns (bool) {
        return owner() == msg.sender;
    }

    function addAllowance(address _who, uint256 _amount) public onlyOwner {
        allowance[_who] = _amount;
    }

    modifier ownerOrAllowed(uint256 _amount) {
        require(
            isOwner() || allowance[msg.sender] >= _amount,
            "You are not allowed!"
        );
        _;
    }

    function reduceAllowance(address _who, uint256 _amount)
        internal
        ownerOrAllowed(_amount)
    {
        allowance[_who] -= _amount;
    }
}

contract SharedWallet is Allowance {
    uint256 public balanceReceived;

    function sendEther() public payable {
        balanceReceived += msg.value;
    }

    function isOwner() internal view returns (bool) {
        return owner() == msg.sender;
    }

    function withdrawMoney(address payable _to, uint256 _amount)
        public
        ownerOrAllowed(_amount)
    {
        require(
            _amount <= address(this).balance,
            "not enough funds in contract"
        );
        if (!isOwner()) {
            reduceAllowance(msg.sender, _amount);
        }
        _to.transfer(_amount);
    }

    fallback() external payable {}
}

推荐阅读