solidity - 我收到一个错误,一个声明错误,但它指向我不明白的引号中的内容
问题描述
我读到的错误
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 {}
}
解决方案
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 {}
}
推荐阅读
- c# - 从手机按下按钮时没有响应
- angular - 令牌过期时如何防止用户路由
- javascript - 关于如何在 .split 之后包含 newLine 的 javascript 代码
- c# - Xamarin Android 中的自定义应用程序类
- java - Spring 多模块:从多个 Spring Boot 应用程序构建单个战争
- angular - 如何在 Angular 6 中将 ag-grid 从版本 18.1.0 更新到 20.1.0?
- authentication - 使用 Loopback API 对其他 Loopback API 进行身份验证
- mysql - pymysql查询:无法回滚
- ruby - 生产服务器上的 Sidekiq 运行时出错
- arduino - 如何在 ENERGIA IDE 上更改 UART 配置(停止位、奇偶校验位等)?