- 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.
推薦閱讀
- Cisco OSPF命令與配置手冊
- 物聯(lián)網(wǎng)短距離無線通信技術(shù)應(yīng)用與開發(fā)
- Building E-commerce Sites with VirtueMart Cookbook
- Hands-On Industrial Internet of Things
- Learning QGIS 2.0
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- 城市治理一網(wǎng)統(tǒng)管
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項目化教程
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- C/C++串口通信:典型應(yīng)用實例編程實踐
- Bonita Open Solution 5.x Essentials
- 網(wǎng)絡(luò)設(shè)計與應(yīng)用(第2版)
- 6G無線網(wǎng)絡(luò)空口關(guān)鍵技術(shù)
- 網(wǎng)絡(luò)利他行為研究:積極心理學(xué)的視角
- Dart Cookbook