- 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.
推薦閱讀
- GPS/GNSS原理與應(yīng)用(第3版)
- OpenLayers Cookbook
- Proxmox High Availability
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- Yii Application Development Cookbook(Second Edition)
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項(xiàng)目化教程
- The Kubernetes Workshop
- 夢(mèng)工廠之材質(zhì)N次方:Maya材質(zhì)手冊(cè)
- 一本書讀懂物聯(lián)網(wǎng)
- 華為HCIA-Datacom認(rèn)證指南
- 移動(dòng)互聯(lián)網(wǎng)新思維
- 工業(yè)以太網(wǎng)技術(shù):AFDX/TTE網(wǎng)絡(luò)原理、接口、互連與安全
- 走近奇妙的物聯(lián)網(wǎng)
- 走近2050:注意力、互聯(lián)網(wǎng)與人工智能
- ElasticSearch Server