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

  • 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.

主站蜘蛛池模板: 汽车| 遵化市| 金山区| 兴和县| 驻马店市| 中方县| 察雅县| 甘洛县| 迭部县| 宣汉县| 额济纳旗| 五寨县| 丰原市| 崇文区| 兴文县| 根河市| 比如县| 德格县| 宝清县| 都匀市| 嘉善县| 蒙山县| 通河县| 璧山县| 江山市| 黑山县| 资兴市| 涞水县| 东阿县| 马尔康县| 科技| 元江| 长兴县| 海城市| 舒城县| 绵竹市| 体育| 太谷县| 将乐县| 富锦市| 达州市|