Created
July 16, 2020 08:48
-
-
Save rahuldamodar94/bb29dc402e7303a4457081c40e62ad52 to your computer and use it in GitHub Desktop.
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
// File: LibReentrancyGuardRichErrors.sol | |
/* | |
Copyright 2019 ZeroEx Intl. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
pragma solidity ^0.5.9; | |
library LibReentrancyGuardRichErrors { | |
// bytes4(keccak256("IllegalReentrancyError()")) | |
bytes internal constant ILLEGAL_REENTRANCY_ERROR_SELECTOR_BYTES = | |
hex"0c3b823f"; | |
// solhint-disable func-name-mixedcase | |
function IllegalReentrancyError() | |
internal | |
pure | |
returns (bytes memory) | |
{ | |
return ILLEGAL_REENTRANCY_ERROR_SELECTOR_BYTES; | |
} | |
} | |
// File: LibRichErrors.sol | |
/* | |
Copyright 2019 ZeroEx Intl. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
pragma solidity ^0.5.9; | |
library LibRichErrors { | |
// bytes4(keccak256("Error(string)")) | |
bytes4 internal constant STANDARD_ERROR_SELECTOR = | |
0x08c379a0; | |
// solhint-disable func-name-mixedcase | |
/// @dev ABI encode a standard, string revert error payload. | |
/// This is the same payload that would be included by a `revert(string)` | |
/// solidity statement. It has the function signature `Error(string)`. | |
/// @param message The error string. | |
/// @return The ABI encoded error. | |
function StandardError( | |
string memory message | |
) | |
internal | |
pure | |
returns (bytes memory) | |
{ | |
return abi.encodeWithSelector( | |
STANDARD_ERROR_SELECTOR, | |
bytes(message) | |
); | |
} | |
// solhint-enable func-name-mixedcase | |
/// @dev Reverts an encoded rich revert reason `errorData`. | |
/// @param errorData ABI encoded error data. | |
function rrevert(bytes memory errorData) | |
internal | |
pure | |
{ | |
assembly { | |
revert(add(errorData, 0x20), mload(errorData)) | |
} | |
} | |
} | |
// File: ReentrancyGuard.sol | |
/* | |
Copyright 2019 ZeroEx Intl. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
pragma solidity ^0.5.9; | |
contract ReentrancyGuard { | |
// Locked state of mutex. | |
bool private _locked = false; | |
/// @dev Functions with this modifer cannot be reentered. The mutex will be locked | |
/// before function execution and unlocked after. | |
modifier nonReentrant() { | |
_lockMutexOrThrowIfAlreadyLocked(); | |
_; | |
_unlockMutex(); | |
} | |
function _lockMutexOrThrowIfAlreadyLocked() | |
internal | |
{ | |
// Ensure mutex is unlocked. | |
if (_locked) { | |
LibRichErrors.rrevert( | |
LibReentrancyGuardRichErrors.IllegalReentrancyError() | |
); | |
} | |
// Lock mutex. | |
_locked = true; | |
} | |
function _unlockMutex() | |
internal | |
{ | |
// Unlock mutex. | |
_locked = false; | |
} | |
} | |
// File: Refundable.sol | |
/* | |
Copyright 2019 ZeroEx Intl. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
pragma solidity ^0.5.9; | |
contract Refundable is | |
ReentrancyGuard | |
{ | |
// This bool is used by the refund modifier to allow for lazily evaluated refunds. | |
bool internal _shouldNotRefund; | |
modifier refundFinalBalance { | |
_; | |
_refundNonZeroBalanceIfEnabled(); | |
} | |
modifier refundFinalBalanceNoReentry { | |
_lockMutexOrThrowIfAlreadyLocked(); | |
_; | |
_refundNonZeroBalanceIfEnabled(); | |
_unlockMutex(); | |
} | |
modifier disableRefundUntilEnd { | |
if (_areRefundsDisabled()) { | |
_; | |
} else { | |
_disableRefund(); | |
_; | |
_enableAndRefundNonZeroBalance(); | |
} | |
} | |
function _refundNonZeroBalanceIfEnabled() | |
internal | |
{ | |
if (!_areRefundsDisabled()) { | |
_refundNonZeroBalance(); | |
} | |
} | |
function _refundNonZeroBalance() | |
internal | |
{ | |
uint256 balance = address(this).balance; | |
if (balance > 0) { | |
msg.sender.transfer(balance); | |
} | |
} | |
function _disableRefund() | |
internal | |
{ | |
_shouldNotRefund = true; | |
} | |
function _enableAndRefundNonZeroBalance() | |
internal | |
{ | |
_shouldNotRefund = false; | |
_refundNonZeroBalance(); | |
} | |
function _areRefundsDisabled() | |
internal | |
view | |
returns (bool) | |
{ | |
return _shouldNotRefund; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment