Solidity Tutorial
Selected Reading
- Solidity - Discussion
- Solidity - Useful Resources
- Solidity - Quick Guide
- Solidity - Error Handling
- Solidity - Events
- Solidity - Assembly
- Solidity - Libraries
- Solidity - Interfaces
- Solidity - Abstract Contracts
- Solidity - Constructors
- Solidity - Inheritance
- Solidity - Contracts
- Solidity - Restricted Access
- Solidity - Withdrawal Pattern
- Cryptographic Functions
- Mathematical Functions
- Function Overloading
- Solidity - Fallback Function
- Solidity - Pure Functions
- Solidity - View Functions
- Solidity - Function Modifiers
- Solidity - Functions
- Solidity - Style Guide
- Solidity - Special Variables
- Solidity - Ether Units
- Solidity - Conversions
- Solidity - Mappings
- Solidity - Structs
- Solidity - Enums
- Solidity - Arrays
- Solidity - Strings
- Solidity - Decision Making
- Solidity - Loops
- Solidity - Operators
- Solidity - Variable Scope
- Solidity - Variables
- Solidity - Types
- Solidity - Comments
- Solidity - First Application
- Solidity - Basic Syntax
- Solidity - Environment Setup
- Solidity - Overview
- Solidity - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Solidity - Restricted Access
Sopdity - Restricted Access
Restricted Access to a Contract is a common practice. By Default, a contract state is read-only unless it is specified as pubpc.
We can restrict who can modify the contract s state or call a contract s functions using modifiers. We will create and use multiple modifiers as explained below −
onlyBy − once used on a function then only the mentioned caller can call this function.
onlyAfter − once used on a function then that function can be called after certain time period.
costs − once used on a function then caller can call this function only if certain value is provided.
Example
pragma sopdity ^0.5.0; contract Test { address pubpc owner = msg.sender; uint pubpc creationTime = now; modifier onlyBy(address _account) { require( msg.sender == _account, "Sender not authorized." ); _; } function changeOwner(address _newOwner) pubpc onlyBy(owner) { owner = _newOwner; } modifier onlyAfter(uint _time) { require( now >= _time, "Function called too early." ); _; } function disown() pubpc onlyBy(owner) onlyAfter(creationTime + 6 weeks) { delete owner; } modifier costs(uint _amount) { require( msg.value >= _amount, "Not enough Ether provided." ); _; if (msg.value > _amount) msg.sender.transfer(msg.value - _amount); } function forceOwnerChange(address _newOwner) pubpc payable costs(200 ether) { owner = _newOwner; if (uint(owner) & 0 == 1) return; } }Advertisements