Skip to content

Commit 5b8e22f

Browse files
committed
implemented custom and external modifiers
1 parent 5c6cbb8 commit 5b8e22f

12 files changed

+36549
-458
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: MIT
2+
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
3+
4+
pragma solidity ^0.8.20;
5+
6+
import {Context} from "../utils/Context.sol";
7+
8+
/**
9+
* @dev Contract module which provides a basic access control mechanism, where
10+
* there is an account (an owner) that can be granted exclusive access to
11+
* specific functions.
12+
*
13+
* The initial owner is set to the address provided by the deployer. This can
14+
* later be changed with {transferOwnership}.
15+
*
16+
* This module is used through inheritance. It will make available the modifier
17+
* `onlyOwner`, which can be applied to your functions to restrict their use to
18+
* the owner.
19+
*/
20+
abstract contract Ownable is Context {
21+
address private _owner;
22+
23+
/**
24+
* @dev The caller account is not authorized to perform an operation.
25+
*/
26+
error OwnableUnauthorizedAccount(address account);
27+
28+
/**
29+
* @dev The owner is not a valid owner account. (eg. `address(0)`)
30+
*/
31+
error OwnableInvalidOwner(address owner);
32+
33+
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
34+
35+
/**
36+
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
37+
*/
38+
constructor(address initialOwner) {
39+
if (initialOwner == address(0)) {
40+
revert OwnableInvalidOwner(address(0));
41+
}
42+
_transferOwnership(initialOwner);
43+
}
44+
45+
/**
46+
* @dev Throws if called by any account other than the owner.
47+
*/
48+
modifier onlyOwner() {
49+
_checkOwner();
50+
_;
51+
}
52+
53+
/**
54+
* @dev Returns the address of the current owner.
55+
*/
56+
function owner() public view virtual returns (address) {
57+
return _owner;
58+
}
59+
60+
/**
61+
* @dev Throws if the sender is not the owner.
62+
*/
63+
function _checkOwner() internal view virtual {
64+
if (owner() != _msgSender()) {
65+
revert OwnableUnauthorizedAccount(_msgSender());
66+
}
67+
}
68+
69+
/**
70+
* @dev Leaves the contract without owner. It will not be possible to call
71+
* `onlyOwner` functions. Can only be called by the current owner.
72+
*
73+
* NOTE: Renouncing ownership will leave the contract without an owner,
74+
* thereby disabling any functionality that is only available to the owner.
75+
*/
76+
function renounceOwnership() public virtual onlyOwner {
77+
_transferOwnership(address(0));
78+
}
79+
80+
/**
81+
* @dev Transfers ownership of the contract to a new account (`newOwner`).
82+
* Can only be called by the current owner.
83+
*/
84+
function transferOwnership(address newOwner) public virtual onlyOwner {
85+
if (newOwner == address(0)) {
86+
revert OwnableInvalidOwner(address(0));
87+
}
88+
_transferOwnership(newOwner);
89+
}
90+
91+
/**
92+
* @dev Transfers ownership of the contract to a new account (`newOwner`).
93+
* Internal function without access restriction.
94+
*/
95+
function _transferOwnership(address newOwner) internal virtual {
96+
address oldOwner = _owner;
97+
_owner = newOwner;
98+
emit OwnershipTransferred(oldOwner, newOwner);
99+
}
100+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT
2+
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
3+
4+
pragma solidity ^0.8.20;
5+
6+
/**
7+
* @dev Provides information about the current execution context, including the
8+
* sender of the transaction and its data. While these are generally available
9+
* via msg.sender and msg.data, they should not be accessed in such a direct
10+
* manner, since when dealing with meta-transactions the account sending and
11+
* paying for execution may not be the actual sender (as far as an application
12+
* is concerned).
13+
*
14+
* This contract is only required for intermediate, library-like contracts.
15+
*/
16+
abstract contract Context {
17+
function _msgSender() internal view virtual returns (address) {
18+
return msg.sender;
19+
}
20+
21+
function _msgData() internal view virtual returns (bytes calldata) {
22+
return msg.data;
23+
}
24+
25+
function _contextSuffixLength() internal view virtual returns (uint256) {
26+
return 0;
27+
}
28+
}

.states/vm-cancun/state.json

Lines changed: 160 additions & 458 deletions
Large diffs are not rendered by default.

contracts/ExternalModifier.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.2 <0.9.0;
3+
4+
import "@openzeppelin/contracts/access/Ownable.sol";
5+
6+
contract ExternalModifier is Ownable{
7+
8+
constructor()Ownable(msg.sender){
9+
10+
}
11+
12+
function welcome(string memory _name) public view onlyOwner returns (string memory) {
13+
return string.concat("hi , Welcome to our world ",_name);
14+
}
15+
16+
}

contracts/Modifiers.sol

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.2 <0.9.0;
3+
4+
5+
contract Modifiers {
6+
7+
address public ownerAddress;
8+
9+
constructor(){
10+
ownerAddress=msg.sender;
11+
}
12+
13+
// function addAddress(address _address) public {
14+
// require(msg.sender==ownerAddress,"You are not the owner!");
15+
// ownerAddress=_address;
16+
// }
17+
18+
// function welcome(string memory _name) public view returns (string memory) {
19+
// require(msg.sender==ownerAddress,"You have not permission to call this!");
20+
// return string.concat("Welcome to our world ",_name);
21+
// }
22+
23+
// or we can make modifier----
24+
25+
modifier onlyOwner(){
26+
require(msg.sender==ownerAddress,"You are not the owner,You haven't access.");
27+
_; //it is like next() in express
28+
}
29+
30+
function addAddress(address _address) public onlyOwner {
31+
ownerAddress=_address;
32+
}
33+
34+
function welcome(string memory _name) public view onlyOwner returns (string memory) {
35+
return string.concat("hi , Welcome to our world ",_name);
36+
}
37+
38+
}

contracts/StackMem.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.2 <0.9.0 ;
3+
4+
contract StackMem{
5+
6+
uint[] numArr;
7+
8+
//local var will store in stack
9+
function add(uint _a,uint _b) public pure returns (uint) {
10+
uint res=_a +_b;
11+
return res;
12+
}
13+
14+
function init1(uint[] memory _numArr) public {
15+
// arrays,string etc are stored in memory
16+
for(uint i=0;i<_numArr.length;i++){
17+
numArr[i]=_numArr[i];
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)