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

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.

主站蜘蛛池模板: 庆城县| 白朗县| 嫩江县| 闽清县| 师宗县| 石台县| 西宁市| 庄河市| 武平县| 中阳县| 锦屏县| 商水县| 额敏县| 仙游县| 宜城市| 光山县| 阳山县| 罗田县| 邹平县| 崇仁县| 武宁县| 通化县| 闸北区| 宝清县| 昌平区| 昌平区| 永寿县| 佛冈县| 罗山县| 杭锦后旗| 年辖:市辖区| 铜鼓县| 清苑县| 酒泉市| 南宁市| 海林市| 大厂| 兴义市| 华安县| 桐梓县| 寿宁县|