官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 桦川县| 阜新| 华宁县| 霍州市| 山东| 泽库县| 云浮市| 宁乡县| 永年县| 梧州市| 昭平县| 肥西县| 阳春市| 濮阳市| 阿尔山市| 同心县| 太湖县| 隆昌县| 东海县| 申扎县| 偏关县| 平果县| 德令哈市| 黎城县| 三门县| 喜德县| 望谟县| 万年县| 新安县| 拉萨市| 当阳市| 屏山县| 米泉市| 昌都县| 邵东县| 兰考县| 买车| 顺平县| 阿图什市| 麻栗坡县| 耒阳市|