Skip to content

Commit 9e64054

Browse files
committed
feat(modifier): add modifier on contract
add modifier on contract add modifier on contract
1 parent 4633ab0 commit 9e64054

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,74 @@ test with remix vm
239239

240240
## check execution log of vm on console
241241

242-
![execution-console-log](./execution-console-log.png)
242+
![execution-console-log](./execution-console-log.png)
243+
244+
## add permission for access check
245+
246+
```solidity
247+
pragma solidity >=0.8.2 <0.9.0;
248+
249+
contract Todo {
250+
address public owner;
251+
Task[] tasks;
252+
struct Task {
253+
string content;
254+
bool status;
255+
}
256+
constructor() {
257+
owner = msg.sender;
258+
}
259+
modifier isOwner() {
260+
require(owner == msg.sender);
261+
_;
262+
}
263+
function add(string memory _content) public isOwner {
264+
tasks.push(Task(_content, false));
265+
}
266+
function get(uint _id) public isOwner view returns (Task memory) {
267+
return tasks[_id];
268+
}
269+
function list() public isOwner view returns (Task[] memory) {
270+
return tasks;
271+
}
272+
function update(uint _id, string memory _content) public isOwner{
273+
tasks[_id].content = _content;
274+
}
275+
function remove(uint _id) public isOwner {
276+
for (uint i = _id; i < tasks.length -1; i++ ) {
277+
tasks[i] = tasks[i+1];
278+
}
279+
tasks.pop();
280+
}
281+
}
282+
```
283+
## setup owner storage
284+
```solidity
285+
address public owner;
286+
constructor() {
287+
owner = msg.sender;
288+
}
289+
```
290+
the msg.sender is special key word refer contract creator
291+
292+
owner will set to the creator of the contract
293+
## implement Modifier isOwner for check only allow owner execution
294+
295+
```solidity
296+
modifier isOwner() {
297+
require(owner == msg.sender);
298+
_;
299+
}
300+
```
301+
use the required keyword for assertion on check owner is msg.sender
302+
303+
_ the underscope is for rest of the other code
304+
305+
## success execution on modifier
306+
![success-execution-modifier](success-execution-modifier.png)
307+
308+
## failed execution on modifier
309+
310+
![fail-execution-modifier](fail-execution-modifier.png)
311+
312+
[modifier-contract](https://remix.ethereum.org/#lang=en&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.26+commit.8a97fa7a.js)

fail-execution-modifier.png

93.1 KB
Loading

success-execution-modifier.png

104 KB
Loading

0 commit comments

Comments
 (0)