- Truffle Quick Start Guide
- Nikhil Bhaskar
- 149字
- 2021-06-25 20:47:34
Securing your contract with modifiers
To ensure that our contract is secure, we ensure the following:
- Only an owner can call the reward function
- The owner of the contract has sufficient funds to transfer to the doer
Let's add a few modifiers, isOwner and hasSufficientFunds:
pragma solidity ^0.4.17;
contract TaskMaster {
mapping (address => uint) public balances; // balances of everyone
address public owner; // owner of the contract
function TaskMaster() public {
owner = msg.sender;
balances[msg.sender] = 10000;
}
function reward(address doer, uint rewardAmount)
public
isOwner()
hasSufficientFunds(rewardAmount)
returns(bool sufficientFunds)
{
balances[msg.sender] -= rewardAmount;
balances[doer] += rewardAmount;
return sufficientFunds;
}
modifier isOwner() {
require(msg.sender == owner);
_;
}
modifier hasSufficientFunds(uint rewardAmount) {
require(balances[msg.sender] >= rewardAmount);
_;
}
}
isOwner requires that the sender of the reward function is the owner of the contract.
hasSufficientFunds requires that the sender of the contract has enough funds to reward the doer.
推薦閱讀
- Application Development with Qt Creator(Second Edition)
- Mastering Machine Learning for Penetration Testing
- Proxmox High Availability
- 計算機網絡安全實訓教程(第二版)
- NB-IoT物聯網技術解析與案例詳解
- Building RESTful Web services with Go
- 通信原理及MATLAB/Simulink仿真
- 華為HCIA-Datacom認證指南
- 圖神經網絡前沿
- Professional Scala
- 計算機通信網絡安全
- 人際網絡
- ElasticSearch Server
- 網絡基本通信約束下的系統性能極限分析與設計
- 物聯網概論