-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sanbir/master
Events etc
- Loading branch information
Showing
7 changed files
with
328 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-FileCopyrightText: 2024 P2P Validator <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Copy and rename of OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol) | ||
|
||
pragma solidity 0.8.27; | ||
|
||
/** | ||
* @dev Contract module which provides a basic access control mechanism, where | ||
* there is an account (an P2pOperator) that can be granted exclusive access to | ||
* specific functions. | ||
* | ||
* The initial P2pOperator is set to the address provided by the deployer. This can | ||
* later be changed with {transferP2pOperator}. | ||
* | ||
* This module is used through inheritance. It will make available the modifier | ||
* `onlyP2pOperator`, which can be applied to your functions to restrict their use to | ||
* the P2pOperator. | ||
*/ | ||
abstract contract P2pOperator { | ||
address private s_p2pOperator; | ||
|
||
/** | ||
* @dev The caller account is not authorized to perform an operation. | ||
*/ | ||
error P2pOperator__UnauthorizedAccount(address _account); | ||
|
||
/** | ||
* @dev The P2pOperator is not a valid P2pOperator account. (eg. `address(0)`) | ||
*/ | ||
error P2pOperator__InvalidP2pOperator(address _p2pOperator); | ||
|
||
event P2pOperator__P2pOperatorTransferred(address indexed _previousP2pOperator, address indexed _newP2pOperator); | ||
|
||
/** | ||
* @dev Initializes the contract setting the address provided by the deployer as the initial P2pOperator. | ||
*/ | ||
constructor(address _initialP2pOperator) { | ||
if (_initialP2pOperator == address(0)) { | ||
revert P2pOperator__InvalidP2pOperator(address(0)); | ||
} | ||
_transferP2pOperator(_initialP2pOperator); | ||
} | ||
|
||
/** | ||
* @dev Throws if called by any account other than the P2pOperator. | ||
*/ | ||
modifier onlyP2pOperator() { | ||
_checkP2pOperator(); | ||
_; | ||
} | ||
|
||
/** | ||
* @dev Returns the address of the current P2pOperator. | ||
*/ | ||
function getP2pOperator() public view virtual returns (address) { | ||
return s_p2pOperator; | ||
} | ||
|
||
/** | ||
* @dev Throws if the sender is not the P2pOperator. | ||
*/ | ||
function _checkP2pOperator() internal view virtual { | ||
if (s_p2pOperator != msg.sender) { | ||
revert P2pOperator__UnauthorizedAccount(msg.sender); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Transfers P2pOperator of the contract to a new account (`_newP2pOperator`). | ||
* Can only be called by the current P2pOperator. | ||
*/ | ||
function transferP2pOperator(address _newP2pOperator) public virtual onlyP2pOperator { | ||
if (_newP2pOperator == address(0)) { | ||
revert P2pOperator__InvalidP2pOperator(address(0)); | ||
} | ||
_transferP2pOperator(_newP2pOperator); | ||
} | ||
|
||
/** | ||
* @dev Transfers P2pOperator of the contract to a new account (`_newP2pOperator`). | ||
* Internal function without access restriction. | ||
*/ | ||
function _transferP2pOperator(address _newP2pOperator) internal virtual { | ||
address oldP2pOperator = s_p2pOperator; | ||
s_p2pOperator = _newP2pOperator; | ||
emit P2pOperator__P2pOperatorTransferred(oldP2pOperator, _newP2pOperator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-FileCopyrightText: 2024 P2P Validator <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Copy and rename of OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol) | ||
|
||
pragma solidity 0.8.27; | ||
|
||
import {P2pOperator} from "./P2pOperator.sol"; | ||
|
||
/** | ||
* @dev Contract module which provides access control mechanism, where | ||
* there is an account (a P2pOperator) that can be granted exclusive access to | ||
* specific functions. | ||
* | ||
* This extension of the {P2pOperator.sol} contract includes a two-step mechanism to transfer | ||
* P2pOperator, where the new P2pOperator must call {acceptP2pOperator} in order to replace the | ||
* old one. This can help prevent common mistakes, such as transfers of P2pOperator to | ||
* incorrect accounts, or to contracts that are unable to interact with the | ||
* permission system. | ||
* | ||
* The initial P2pOperator is specified at deployment time in the constructor for `P2pOperator.sol`. This | ||
* can later be changed with {transferP2pOperator} and {acceptP2pOperator}. | ||
* | ||
* This module is used through inheritance. It will make available all functions | ||
* from parent (P2pOperator.sol). | ||
*/ | ||
abstract contract P2pOperator2Step is P2pOperator { | ||
address private s_pendingP2pOperator; | ||
|
||
event P2pOperator2Step__P2pOperatorTransferStarted(address indexed _previousP2pOperator, address indexed _newP2pOperator); | ||
|
||
/** | ||
* @dev Returns the address of the pending P2pOperator. | ||
*/ | ||
function getPendingP2pOperator() public view virtual returns (address) { | ||
return s_pendingP2pOperator; | ||
} | ||
|
||
/** | ||
* @dev Starts the P2pOperator transfer of the contract to a new account. Replaces the pending transfer if there is one. | ||
* Can only be called by the current P2pOperator. | ||
* | ||
* Setting `_newP2pOperator` to the zero address is allowed; this can be used to cancel an initiated P2pOperator transfer. | ||
*/ | ||
function transferP2pOperator(address _newP2pOperator) public virtual override onlyP2pOperator { | ||
s_pendingP2pOperator = _newP2pOperator; | ||
emit P2pOperator2Step__P2pOperatorTransferStarted(getP2pOperator(), _newP2pOperator); | ||
} | ||
|
||
/** | ||
* @dev Transfers P2pOperator of the contract to a new account (`_newP2pOperator`) and deletes any pending P2pOperator. | ||
* Internal function without access restriction. | ||
*/ | ||
function _transferP2pOperator(address _newP2pOperator) internal virtual override { | ||
delete s_pendingP2pOperator; | ||
super._transferP2pOperator(_newP2pOperator); | ||
} | ||
|
||
/** | ||
* @dev The new P2pOperator accepts the P2pOperator transfer. | ||
*/ | ||
function acceptP2pOperator() public virtual { | ||
address sender = msg.sender; | ||
if (s_pendingP2pOperator != sender) { | ||
revert P2pOperator__UnauthorizedAccount(sender); | ||
} | ||
_transferP2pOperator(sender); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.